Connect using mysql binary ¶
You can use MySQL binary to enter the mysql command prompt to connect to the MySQL database. The following is a simple example of connecting to a mysql server from the command line: After a successful login, a mysql > command prompt window appears, on which you can execute any SQL statement. After the above command is executed, the successful login output is as follows: In the above example, we use root users to log in to the mysql server, but you can also log in with other mysql users. If the user permissions are sufficient, any user can SQL from the command prompt window of mysql. You can use the exit command to exit the mysql > command prompt window, as follows:Example ¶
[root@host]# mysql -u root -p
Enter password:******
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.
mysql> exit
Bye
Connect to MySQL using a PHP script ¶
PHP provides
mysqli_connect()
Function to connect to the database.
This function takes six parameters, returns the connection ID after a successful link to MySQL, and returns FALSE if it fails. 参数说明: Parameters. Description host Optional. Specify the hostname or IP address. username Optional. Specifies the MySQL user name. password Optional. Specify the MySQL password. dbname Optional. Specify the database to use by default. port Optional. Specifies the port number that attempts to connect to the MySQL server. socket Optional. Specifies the socket or named pipe to use. You can use PHP. Only one argument to this function is This function closes the non-persistent connection to the MySQL server associated with the specified connection identity. If not specified 提示: There is usually no need to use the You can try the following example to connect to your MySQL server: Connect MySQLGrammar ¶
mysqli_connect(host, username, password, dbname,port, socket);
mysqli_close()
Function to break the link to the MySQL database.
mysqli_connect()
The MySQL connection identifier returned after the function successfully created the connection.Grammar ¶
bool mysqli_close ( mysqli $link )
link_identifier
Closes the last open connection
mysqli_close()
Because open non-persistent connections are automatically closed after the script is executedExample ¶
<?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);?>