MySql binary connection mode details

  • 2021-01-14 06:54:37
  • OfStack

Use mysql2 to connect

You can use MySQL2 to enter the mysql command prompt to connect to the MySQL database.

The instance

Here is a simple example of connecting to the ES11en server from the command line:


[root@host]# mysql -u root -p
Enter password:******

mysql appears after a successful login > Command prompt window, where you can execute any SQL statement.

After the above command is executed, the login results are as follows:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2854760 to server version: 5.0.9
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

In the example above, we used the root user to log in to the mysql server, although you can log in with other mysql users as well.

Any user can perform SQL operations in the command prompt window of mysql if the user permissions are sufficient.

Exit mysql > The command prompt window can use the exit command, as shown below:


mysql> exit
Bye

Connect to MySQL using the PHP script

PHP provides the mysql_connect() function to connect to the database.

This function takes five arguments and returns the connection ID on success of the link to MySQL and FALSE on failure.

grammar


connection mysql_connect(server,user,passwd,new_link,client_flag);

Parameter description:

参数 描述
server

可选。规定要连接的服务器。

可以包括端口号,例如 "hostname:port",或者到本地套接字的路径,例如对于 localhost 的 ":/path/to/socket"。

如果 PHP 指令 mysql.default_host 未定义(默认情况),则默认值是 'localhost:3306'。

user 可选。用户名。默认值是服务器进程所有者的用户名。
passwd 可选。密码。默认值是空密码。
new_link 可选。如果用同样的参数第2次调用 mysql_connect(),将不会建立新连接,而将返回已经打开的连接标识。参数 new_link 改变此行为并使 mysql_connect() 总是打开新的连接,甚至当 mysql_connect() 曾在前面被用同样的参数调用过。
client_flag

可选。client_flags 参数可以是以下常量的组合:

  • MYSQL_CLIENT_SSL - 使用 SSL 加密
  • MYSQL_CLIENT_COMPRESS - 使用压缩协议
  • MYSQL_CLIENT_IGNORE_SPACE - 允许函数名后的间隔
  • MYSQL_CLIENT_INTERACTIVE - 允许关闭连接之前的交互超时非活动时间

You can use the mysql_close() function of ES72en to disconnect from the ES75en database.

This function takes only one argument, the MySQL connection identifier returned by the mysql_connect() function after successfully creating a connection.

grammar


bool mysql_close ( resource $link_identifier );

This function closes the non-persistent connection to the ES86en server associated with the specified connection identity. If link_identifier is not specified, close the last open connection.

Tip: It is not usually necessary to use mysql_close() because open non-persistent connections are automatically closed when the script completes.

Note: mysql_close() does not close persistent connections established by mysql_pconnect().

The instance

You can try the following example to connect to your MySQL server:


<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
$dbhost = 'localhost:3306'; //mysql Server host address 
$dbuser = 'guest'; //mysql The user name 
$dbpass = 'guest123';//mysql Username password 
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($conn);
?>
</body>
</html>

The above is the site to introduce MySql2 metadecimal connection details, I hope to help you!


Related articles: