在一臺測試服務器上部署了2個實例,一個端口是默認的3306,另外一個端口是3376。MySQL的版本是5.6.35mysql
[root@MySQL56_L1 ~]# ps -ef | grep mysql | grep -v grep mysql 11176 9876 0 13:31 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf mysql 11262 9876 0 13:34 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3376/my3376.cnf
my3376.cnf的部分參數配置以下sql
[root@MySQL56_L1 ~]# more /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock # The MySQL server [mysqld] # Basic port = 3306 user = mysql basedir = /usr/local/mysql datadir = /data/mysql/mysql3306/data tmpdir = /data/mysql/mysql3306/tmp socket = /tmp/mysql.sock
my3376.cnf的部分參數配置以下數據庫
[root@MySQL56_L1 ~]# more /data/mysql/mysql3376/my3376.cnf [client] port = 3376 socket = /tmp/mysql3376.sock # The MySQL server [mysqld] # Basic port = 3376 user = mysql basedir = /usr/local/mysql datadir = /data/mysql/mysql3376/data tmpdir = /data/mysql/mysql3376/tmp socket = /tmp/mysql3376.sock
兩個數據庫中的帳號及密碼以下服務器
(product)root@localhost [(none)]> select host, user, password from mysql.user ; +-----------+------+----------+ | host | user | password | +-----------+------+----------+ | localhost | root | | | 127.0.0.1 | root | | +-----------+------+----------+
當使用帳號、密碼、端口的方式方式登陸到端口爲3376的實例時,發現登陸的倒是3306端口的實例下網絡
[root@MySQL56_L1 ~]# mysql -uroot -p -P3376 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.6.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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. (product)root@localhost mysql.sock [(none)]>
請注意上面代碼塊中的紅色加粗字體,這是端口爲3306實例的socket文件,3376端口實例使用socket方式登陸以下:socket
[root@MySQL56_L1 ~]# mysql -S /tmp/mysql3376.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.6.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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. (product)root@localhost mysql3376.sock [(none)]>
在須要登陸到3376端口實例下時,已經明確指定了端口,爲何仍是登陸到3306端口實例下呢? tcp
難道是由於沒有加上「-h」?測試
[root@MySQL56_L1 ~]# mysql -hlocalhost -uroot -p -P3376 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.6.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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. (product)root@localhost mysql.sock [(none)]>
能夠看到,使用「-hlocalhost」是仍是登陸到3306端口實例下,如果修改成「-h IP」,又會怎麼樣呢? 字體
[root@MySQL56_L1 ~]# mysql -h127.0.0.1 -uroot -p -P3376 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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. (product)root@127.0.0.1 3376 [(none)]>
此次就能如願登陸到所指望的3376端口實例下了。spa
小結:安裝了多實例的狀況下,須要登陸到特定端口實例下的方法
一、使用「-S」 參數,經過指定實例下的socket文件來登陸到指定的實例
二、使用「-h」參數,注意,這裏必須是使用'TCP/IP'的方式,不能是'localhost',由於'localhost'會使用默認的socket文件登陸
那麼,爲何指定了端口「-P3376」,仍是會登陸到3306端口實例下呢?
緣由是由於沒有指定「-h」使用'TCP/IP'方式來登陸時,是默認使用socket方式登陸,問題又來了,是使用哪一個socket文件呢?
[root@MySQL56_L1 ~]# mysql --verbose --help | grep socket --protocol=name The protocol to use for connection (tcp, socket, pipe, -S, --socket=name The socket file to use for connection. The buffer size for TCP/IP and socket communication. socket /tmp/mysql.sock
這個文件是在哪裏指定呢?
[root@MySQL56_L1 ~]# mysql --verbose --help | grep my.cnf order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
注:以上的4個「my.cnf」後面的參數會覆蓋前面的。
如果產生懷疑,能夠試驗一下在「~/.my.cnf」中添加以下代碼
[root@MySQL56_L1 ~]# vi ~/.my.cnf [client] socket = /tmp/mysql3306.sock
再查看一下mysql讀取的socket文件
[root@MySQL56_L1 ~]# mysql --verbose --help | grep socket --protocol=name The protocol to use for connection (tcp, socket, pipe, -S, --socket=name The socket file to use for connection. The buffer size for TCP/IP and socket communication. socket /tmp/mysql3306.sock
從上面的測試就能夠看出是讀取socket文件的路徑
注意:測試完以後須要改回正確的socket文件名
看完上面的說明,如今應該能明白爲何爲何指定了「-P 3376」,卻登陸到了3306端口是實例下了。其實,正確的應該是這個路徑「/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf」下的socket參數對應的是哪一個端口,就會登陸到哪一個端口的實例下。
看完了這麼多,可能仍是不明白socket是什麼?
socket是用於同一臺主機的進程間通信(IPC),不須要通過網絡協議棧,不須要打包拆包、計算校驗和、維護序號和應答等,只是將應用層數據從一個進程拷貝到另一個進程,消息既不會丟失也不會順序錯亂。可用於兩個沒有親緣關係的進程,是全雙工的。
以上,如有錯誤,請不吝指出。