1. 程式人生 > >DBA:多連接MySQL

DBA:多連接MySQL

index.php its current end l數據庫 web process view pass

一、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.
技術分享圖片

2.
技術分享圖片

DBA:多連接MySQL