Mariadb 简明教程
MariaDB - Connection
与 MariaDB 建立连接的一种方法是使用命令提示符中的 mysql 二进制文件。
One way to establish a connection with MariaDB consists of using the mysql binary at the command prompt.
MYSQL Binary
查看下面给出的示例。
Review an example given below.
[root@host]# mysql -u root -p
Enter password:******
上面给出的代码连接到 MariaDB 并提供用于执行 SQL 命令的命令提示符。输入代码后,会显示一条欢迎消息,表明连接成功,并显示版本号。
The code given above connects to MariaDB and provides a command prompt for executing SQL commands. After entering the code, a welcome message should appear indicating a successful connection, with the version number displayed.
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 122323232
Server version: 5.5.40-MariaDB-log
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
该示例使用 root 访问,但具有权限的任何用户当然都可以访问 MariaDB 提示并执行操作。
The example uses root access, but any user with privileges can of course access the MariaDB prompt and perform operations.
通过 exit 命令断开与 MariaDB 的连接,如下所示:
Disconnect from MariaDB through the exit command as follows −
mysql> exit
PHP Connection Script
连接到 MariaDB 并断开连接的另一种方法是使用 PHP 脚本。PHP 提供 mysql_connect() 函数用于打开数据库连接。它使用五个可选参数,并在连接成功后返回 MariaDB 链接标识符,或在连接不成功时返回 false。它还提供了 mysql_close() 函数用于关闭数据库连接,该函数使用单个参数。
Another way to connect to and disconnect from MariaDB consists of employing a PHP script. PHP provides the mysql_connect() function for opening a database connection. It uses five optional parameters, and returns a MariaDB link identifier after a successful connection, or a false on unsuccessful connection. It also provides the mysql_close() function for closing database connections, which uses a single parameter.
Syntax
审阅以下 PHP 连接脚本语法 −
Review the following PHP connection script syntax −
connection mysql_connect(server,user,passwd,new_link,client_flag);
参数说明如下 −
The description of the parameters is given below −
Sr.No |
Parameter & Description |
1 |
server This optional parameter specifies the host name running the database server. Its default value is “localhost:.3036.” |
2 |
user This optional parameter specifies the username accessing the database. Its default value is the owner of the server. |
3 |
passwd This optional parameter specifies the user’s password. Its default value is blank. |
4 |
new_link This optional parameter specifies that on a second call to mysql_connect() with identical arguments, rather than a new connection, the identifier of the current connection will be returned. |
5 |
client flags This optional parameter uses a combination of the following constant values − MYSQL_CLIENT_SSL − It uses ssl encryption. MYSQL_CLIENT_COMPRESS − It uses compression protocol. MYSQL_CLIENT_IGNORE_SPACE − It permits space after function names. MYSQL_CLIENT_INTERACTIVE − It permits interactive timeout seconds of inactivity prior to closing the connection. |
审阅下面给出的 PHP 断开连接脚本语法 −
Review the PHP disconnection script syntax given below −
bool mysql_close ( resource $link_identifier );
如果您省略资源,最近打开的资源将关闭。它在成功关闭时返回 true 值,或者在失败时返回 false 值。
If you omit the resource, the most recent opened resource will close. It returns a value of true on a successful close, or false.
尝试以下示例代码以连接至 MariaDB 服务器 −
Try the following example code to connect with a MariaDB server −
<html>
<head>
<title>Connect to MariaDB Server</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest1';
$dbpass = 'guest1a';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($conn);
?>
</body>
</html>
成功连接后,您将看到以下输出 −
On successful connection, you will see the following output −
mysql> Connected successfully