在一台測試服務器上部署了2個實例,一個端口是默認的3306,另一個端口是3376。MySQL的版本是5.6.35
[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的部分參數配置如下
[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方式登錄如下:
[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端口實例下呢?
難道是因為沒有加上“-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端口實例下了。
小結:安裝了多實例的情況下,需要登錄到特定端口實例下的方法
1、使用“-S” 參數,通過指定實例下的socket文件來登錄到指定的實例
2、使用“-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),不需要經過網絡協議棧,不需要打包拆包、計算校驗和、維護序號和應答等,只是將應用層數據從一個進程拷貝到另外一個進程,消息既不會丟失也不會順序錯亂。可用於兩個沒有親緣關系的進程,是全雙工的。
以上,若有錯誤,請不吝指出。