MySQL登录之socket与TCP
在一台测试服务器上部署了2个实例,一个端口是默认的3306,另一个端口是3376。MySQL的版本是5.6.35
[root@MySQL56_L1 ~]# ps -ef | grep mysql | grep -v grep
mysql : pts/ :: /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
mysql : pts/ :: /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3376/my3376.cnf
my3376.cnf的部分参数配置如下
[root@MySQL56_L1 ~]# more /etc/my.cnf [client]
port =
socket = /tmp/mysql.sock # The MySQL server
[mysqld]
# Basic
port =
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 =
socket = /tmp/mysql3376.sock # The MySQL server
[mysqld]
# Basic
port =
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
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , 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
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , 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
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , 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. -uroot -p -P3376
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , 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),不需要经过网络协议栈,不需要打包拆包、计算校验和、维护序号和应答等,只是将应用层数据从一个进程拷贝到另外一个进程,消息既不会丢失也不会顺序错乱。可用于两个没有亲缘关系的进程,是全双工的。
以上,若有错误,请不吝指出。
MySQL登录之socket与TCP的更多相关文章
- 通过Socket实现TCP编程,用户登录之服务器相应客户端,客户端和服务端之间的通信
服务器端: 1.创建ServerSocket对象,绑定监听端口: 2.通过accept()方法监听客户端请求: 3.建立连接后通过输入流读取客户端发送的请求信息; 4.通过输出流向客户端发送响应信息; ...
- MySQL登录报错ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@pisphkdcbsql01 mysql3307]# /opt/mysql3307/bin/mysql -upisp -ppisp@ mysql: [Warning] Using a pa ...
- 深度解析mysql登录原理
使用mysql数据库的第一步必然是建立连接登录,然后在上面执行SQL命令.无论是通过mysql的客户端,还是通过C-API,JDBC标准接口连接数据库,这个过程一定少不了.今天我们聊一聊mysql登陆 ...
- Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法
我的环境:Mac 10.11.6 ,mysql 5.7.14 . mac mySql 报错ERROR 2002 (HY000): Can't connect to local MySQL serv ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib
今天在linux里安装mysql,运行时遇到这样的错误 ERROR 2002 (HY000): Can't connect to local MySQL server through socket ' ...
- 【MySQL案件】mysql登录-S失败
1.1.1. mysql登录mysql时间,-S参数失效 [环境的叙述性说明] mysql5.5.14 [问题叙述性说明] 配置多个实例 实例1 实例2 datadir /home/mysql_330 ...
- MySQL登录汇总
--MySQL登录汇总 --------------------2014/5/17 1. ERROR 1045错误ERROR 1045 (28000): Access denied for user ...
- mysql unix domain socket and network socket, ssh key
当主机填写为localhost时mysql会采用 unix domain socket连接 当主机填写为127.0.0.1时mysql会采用tcp方式连接 这是linux套接字网络的特性,win平台不 ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
有时候,当我们使用"mysql"."mysqladmin"."mysqldump"等命令管理数据库时,服务器抛出类似如下错误: 一.错误现场 ...
随机推荐
- 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】
组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n\) 个穴位,穴位的编号为 \(1\sim n\).有若干条有向的脉络连接着这些穴位. ...
- poj1182 食物链 带权并查集
题目传送门 题目大意:大家都懂. 思路: 今天给实验室的学弟学妹们讲的带权并查集,本来不想细讲的,但是被学弟学妹们的态度感动了,所以写了一下这个博客,思想在今天白天已经讲过了,所以直接上代码. 首先, ...
- [USACO12FEB]牛的IDCow IDs 一题多解(求二进制中有k个1 ,第n大的数)
题目: FJ给他的奶牛用二进制进行编号,每个编号恰好包含K 个"1" (1 <= K <= 10),且必须是1开头.FJ按升序编号,第一个编号是由K个"1&q ...
- SQL数据库查询一列数据返回一行
SQL:数据库合并列数据:遇到一个更新的问题 想要把查询到的数据某一列拼接成字符串形式返回用的是SQL数据库中的STUFF函数比如 查询到的表(u_College)如下Id Name Age Clas ...
- wamp下localhost目录Your Projects下项目无法打开解决方案
最近在学PHP,然后可能遇到各种小白问题,记录下来当做自己成长的见证吧: wamp下localhost目录Your Projects下项目无法打开,但是在url中输入项目可以访问到. 解决方案: 注意 ...
- poj1862
一.题意:两个物体m1.m2相撞后会变成一个物体,这个物体的重量会变成2*sqrt(m1*m2).有n个物体,假设只会发生两两相撞,求最后剩下的最小重量. 二.思路:简单的贪心.越大的数开越多的次方, ...
- CenctOS6 and CenctOS7 多种姿势解决忘记密码
-----linux---- 忘记密码啦!!! 忘记密码教程!!! 教你们忘记密码(我原来密码就是123456,忘记是不可能的!假装忘记的样子 0.0) 现在我们忘记密码了!对忘记密码了.我忘记密码了 ...
- maven+springboot+阿里大于
问题:maven仓库无法找到taobao-sdk-java-auto-1.0.jar包 目的:将jar包添加到maven项目中 1.在官网下载jar包 2.将jar包放在d盘 3.mvn instal ...
- 03-struts2获得servetAPI
1 原理 三个域合一的时候相同的键值对以小的域为准.ActionContext 对象创建:每次请求的时候都会创建一个与请求对应的 ActionContext 对象.ActionContext 销毁:请 ...
- TOJ 3184 Mine sweeping
描述 I think most of you are using system named of xp or vista or win7.And these system is consist of ...