一、mysql二进制连接

使用MySQL二进制方式进入到mysql命令提示符下连接MySQL数据库。

实例操作:

[root@mysql-linuxview ~]# mysql -uroot -p
Enter password:
#登录成功出现以下信息
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5     #显示你连接服务器的用户ID
Server version: 5.7.18-log Source distribution      #服务器的版本信息

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit                                    #退出数据库命令
Bye
[root@mysql-linuxview ~]#

二、PHP脚本连接MySQL

PHP提供了mysqli_connect()函数来连接数据库 该函数一共有6个参数,在成功连接到MySQL后返回连接标识,失败返回false

语法: mysqli_connect(host,username,password,dbname,port,socket);

参数说明:

注意:想要断开MySQL连接可以使用mysqliclose()函数来断开与MySQL服务器的连接,该函数只有一个参数为mysqliconnect()函数创建连接成功后返回的MySQL连接标识符。

语法: bool mysqli_close(mysqli $link)

本函数关闭指定的连接标识所关联到的MySQL服务器的非持久连接。如果没有指定linkidentifier,则关闭上一个打开的连接。通常不需要使用mysqliclose(),因为打开的非持久连接会在脚本执行完毕后自动关闭。

实例操作:

[root@mysql-linuxview web]# cat index.php
<?
$dbhost = 'localhost:3306';
#$dbport = '3306';
$dbuser = 'root';
$dbpass = '000000';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass);
if ( ! $conn)
{
    die('Could not connect:'. mysqli_error());
}
echo 'connect success!!!';
mysqli_close($conn);
?>

[root@mysql-linuxview web]#

网页访问: 1.