Fork me on GitHub

Mysql的两种连接方式总结

目录

  • 背景
  • 第一部分 TCP/IP Socket
  • 第二部分 UNIX Domain Socket
  • 第三部分 Mysql的两种连接方式
  • 参考文献及资料

背景

我们在使用Mysql客户端和Mysql交互的时候,如果客户端是远程(非本机)那么底层是通过TCP/IPSocket方式进行交互。但是如果客户端和数据库在同一台服务器上时,Mysql支持通过UNIX Domain Socket方式交互。

Mysql客户端和Mysql数据库服务通信,不管是本机还是远程,其本质是两个计算机进程之间的通信。根据位置的不同分为:本机进程通信和网络间进程通信。

  • 本机进程通信。Linux通常有:管道、信号量、消息队列、信号、共享内存、UNIX Domain Socket套接字。
  • 网络间进程通信。 TCP/IP Socket

本文将介绍将介绍这两种交互方式。

第一部分 TCP/IP Socket

提到通信,那么首先需要解决是身份识别问题。对于本机进程,不同的进程都有操作系统分配的进程号(process ID)作为唯一标识。但是网络间进程通信时候,PID就不能作为唯一标识了,另外操作系统支持的网络协议不同。所以网络间进程通信需要解决唯一身份标识和网络协议识别问题。

这时候出现的TCP/IP协议中,IP层的ip地址可以唯一标识网络计算机身份,传输层的“协议+端口”可以唯一标识进程。这样就有”三元坐标”:(IP地址,协议,端口),这个坐标可以唯一标识网络中进程。

在网络编程中,TCP/IPSocket两个概念没有必然的联系。Socket编程接口在设计的时候,也能支持其他的网络协议。所以,socket的出现只是可以更方便的使用TCP/IP协议栈而已,其对TCP/IP进行了抽象封装,形成了几个最基本的函数接口。例如create,listen,accept,connect,read和write等等。

第二部分 UNIX Domain Socket

对于本机中进程通信,也是可以使用TCP/IP Socket方式,通过回环地址(loopback)地址 127.0.0.1,但是对于本机这是繁琐的,接着就发展出了Unix domain socket方式,又称IPC(inter-process communication进程间通信) socket。

UNIX domain socket 用于本机进程通信更有效率。不需要经过网络协议栈,不需要打包拆包、计算校验和、维护序号、路由和应答等,只是将应用层数据从一个进程拷贝到另一个进程。UNIX domain socketTCP/IP Socket相比较,在同一台主机的传输速度前者是后者的两倍。

UNIX domain socketTCP/IP Socket 最明显的不同在于地址格式不同,TCP/IP Socket 的 socket 地址是IP地址加端口号,而UNIX domain socket的地址是一个 socket 类型的文件在文件系统中的路径。

另外下面的命令可以查看当前操作系统中UNIX domain socket通信清单:

1
netstat -a -p --unix

第三部分 Mysql的两种连接方式

3.1 UNIX Domain Socket方式

客户端部署在Mysql本机,配置好环境变量,就可以使用下面的命令登录Mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@mysql ~]# mysql -uroot -P*****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.46-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

root@[(none)]>

这时候就是通过UNIX Domain Socket方式和Mysql进行交互的。只是这时候我们没有指定参数–socket,这个参数指定就是UNIX Domain Socket依赖的socket 类型的文件。Mysql默认这个参数为:–socket=/tmp/mysql.sock。如果安装Mysql时候,配置文件my.cnf中下面配置错误或文件丢失,就会报错。

1
2
[client] 
socket=/tmp/mysql.sock

报错找不到sock文件:

1
2
[root@mysql ~]# mysql -uroot -P*****
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

遇到这种报错的处理方法:

  • 使用find命令查找文件路径,调整配置,使其归位。如果文件不再配置文件指定位置,这时候需要在命令中指定具体的路径,命令如下:

    1
    [root@mysql ~]# mysql -uroot -P*****  -S /path/of/mysql.sock
  • 重新创建。可以简单地通过重启Mysql服务重新创建得到它。因为服务在启动时重新创建它。

另外可以通过查看Mysql变量信息来查看这个文件路径配置路径:

1
2
3
4
5
6
7
8
mysql> SHOW VARIABLES LIKE 'socket';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| socket | /tmp/mysql.sock |
+---------------+-----------------+
1 row in set (0.00 sec)
mysql -uroot -S/tmp/mysql.sock

3.2 TCP/IP Socket方式

在本机使用UNIX Domain Socket方式无法登陆时候,还可以使用TCP/IP Socket方式。命令需要指定IP信息,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@mysql ~]# mysql -h192.1.1.20
ERROR 2003 (HY000): Can't connect to MySQL server on '192.1.1.20' (111)

[root@mysql ~]# mysql -h192.1.1.20 -P3307
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.46-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

root@[(none)]>

那么如果本机中同时指定两个参数时候,Mysql会默认使用TCP/IP Socket的方式连接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@mysql ~]# mysql -h192.1.1.20 -P3306 -S /path/of/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.46-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

root@[(none)]>

对于和远程Mysql服务交互,需要指定远程Mysql服务监听IP和端口即可,不再赘述。

参考文献及资料

1、Linux进程间通信方式有哪些?,链接:https://zhuanlan.zhihu.com/p/63916424

2、UNIX Domain Socket IPC,链接:http://docs.linuxtone.org/ebooks/C&CPP/c/ch37s04.html

0%