串連MySQL資料庫的兩種方式介紹
串連MySQL操作是串連進程和MySQL資料庫執行個體進行通訊。從開發的角度來說,本質上是進程通訊,常用的進程通訊方式有管道、具名管道、命名字、TCP/IP通訊端、Unix網域名稱通訊端
TCP/IP串連:
TCP/IP通訊端串連方式是MySQL在任何平台都提供的一種串連方式,也是網路中使用最多的一種方式。這種方式在TCP/IP串連上建立一個基於網路的串連請求,一般情況下用戶端在一台伺服器上,而MySQL執行個體在另外一台伺服器上,這兩台機器通過TCP/IP網路連接
mysql> use mysql;Readingtable information for completion of table and column namesYoucan turn off this feature to get a quicker startup with -ADatabasechangedmysql>select user,host,password from user;+------+-------------------+-------------------------------------------+|user | host | password |+------+-------------------+-------------------------------------------+|root | localhost |*23AE809DDACAF96AF0FD78ED04B6A265E05AA257 ||root | server.sxkeji.com | ||root | 127.0.0.1 | || | localhost | || | server.sxkeji.com | || wu | % |*00A51F3F48415C7D4E8908980D443C29C69B60C9|+------+-------------------+-------------------------------------------+6rows in set (0.01 sec) mysql>
首先遠端連線的用戶端串連的使用者有許可權才可以被串連,我們查看到了wu這個使用者允許任何機器遠端連線
# mysql -h192.168.0.110 -uwu -pEnterpassword:Welcometo the MySQL monitor. Commands end with; or \g.YourMySQL connection id is 16Serverversion: 5.1.52 Source distribution Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,andyou are welcome to modify and redistribute it under the GPL v2 license Type'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
//顯示串連成功
Unix域通訊端串連:
在Linux和Unix環境下,還可以使用Unix域通訊端串連。Unix域通訊端其實不是網路通訊協定,所以只能使用MySQL用戶端和資料庫執行個體在同一台伺服器上的情況下使用。可以在設定檔中指定通訊端檔案路徑,如-socket=/tmp/mysql.sock。當資料庫啟動之後使用如下方法尋找通訊端
mysql>show variables like 'socket'\G***************************1. row ***************************Variable_name:socket Value: /var/lib/mysql/mysql.sock1row in set (0.00 sec)mysql>
然後就可以通過通訊端的方式串連了
# mysql -uwu -S /var/lib/mysql/mysql.sockWelcometo the MySQL monitor. Commands end with; or \g.YourMySQL connection id is 18Serverversion: 5.1.52 Source distributionCopyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,andyou are welcome to modify and redistribute it under the GPL v2 licenseType'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>