May 15, 2021 MySQL
You can use the MySQL binary to enter the MySQL command prompt down to connect to the MySQL database.
Here are a simple example of connecting mySQL servers from the command line:
[root@host]# mysql -u root -p Enter password:******
After successful login, a mysql?command prompt window appears where you can execute any SQL statement.
After the above command is executed, the successful output of the login is 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 above example, we used root users to log on to the MySQL server, and of course you can use other MySQL users to log on.
If the user rights are sufficient, any user can perform SQL operations in mySQL's command prompt window.
Exiting the mysql?command prompt window can use the exit command, as follows:
mysql> exit Bye
PHP provides mysql_connect() function to connect to the database.
The function has 5 parameters, returns the connection identity after successfully linking to MySQL, and fails to return FALSE.
mysqli_connect(host,username,password,dbname,port,socket);
Description of the parameters:
Parameters | Describe |
---|---|
host | Optional. Specify the host name or IP address. |
username | Optional. The MySQL username is specified. |
password | Optional. The MySQL password is specified. |
dbname | Optional. Specifies the database that is used by default. |
port | Optional. Specifies the port number of the attempt to connect to the MySQL server. |
socket | Optional. Specify the socket or the named pipe to use. |
You can use PHP'mysql_close() function to break the link to the MySQL database.
The function has only one argument for mysql_connect() the MySQL connection identifier returned after the function was successfully created.
bool mysqli_close ( mysqli $link )
This function closes the specified connection identity for the non-persistent connection to the MySQL server. S uch as this function closes the specified connection identity associated with a non-persistent connection to the MySQL server. If no link_identifier specified, the last open connection is closed.
Tip: You typically don't need mysqli_close () because an open, non-persistent connection automatically closes after the script is executed.
You can try the following instances to connect to your MySQL server:
<?php
$dbhost = 'localhost'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo '数据库连接成功!';
mysqli_close($conn);
?>