在一台测试服务器上部署了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),不须要通过网络协议栈,不须要打包拆包、计算校验和、维护序号和应答等,只是将应用层数据从一个进程拷贝到另一个进程,消息既不会丢失也不会顺序错乱。可用于两个没有亲缘关系的进程,是全双工的。
以上,如有错误,请不吝指出。