1.Mysql的连接方式
要了解Mysql的通讯协议,首先需要知道是以哪种连接方式去连接Mysql服务器的;Mysql的主要连接方式包括:Unix套接字,内存共享,命名管道,TCP/IP套接字等。
1.1Unix套接字
在Linux和Unix环境下,可以使用Unix套接字进行Mysql服务器的连接;Unix套接字其实不是一个网络协议,只能在客户端和Mysql服务器在同一台电脑上才可以使用,使用方式也很简单:
root@root ~]# mysql -uroot -prootmysql> show variables like 'socket';+---------------+---------------------------+| Variable_name | Value |+---------------+---------------------------+| socket | /var/lib/mysql/mysql.sock |+---------------+---------------------------+1 row in set (0.00 sec) |
以上命令查询Unix套接字文件的位置;
1.2命名管道和内存共享
在window系统中客户端和Mysql服务器在同一台电脑上,可以使用命名管道和共享内存的方式,
命名管道开启:–shared-memory=on/off;
共享内存开启:–enable-named-pipe=on/off;
1.3TCP/IP套接字
在任何系统下都可以使用的方式,也是使用最多的连接方式,本文要介绍的通讯协议也是基于此连接方式的,下面通过tcpdump对TCP/IP套接字有一个初步的了解:
服务器端:
[root@root ~]# tcpdump port 3306tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on venet0, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes |
服务器端监听3306端口(也就是Mysql的端口);
客户端:
C:/Users/hui.zhao>mysql -h64.xxx.xxx.xxx -uroot -prootmysql> exitBye |
客户端连接服务器,然后断开连接,这时候观察服务器的监听结果日志:
[root@root ~]# tcpdump port 3306tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on venet0, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes02:06:25.442472 IP 153.3.251.202.33876 > root.mysql: Flags [S], seq 27289263, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 002:06:25.442763 IP root.mysql > 153.3.251.202.33876: Flags [S.], seq 2014324548, ack 27289264, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 002:06:25.617449 IP 153.3.251.202.33876 > root.mysql: Flags [.], ack 1, win 256, length 002:06:29.812946 IP root.mysql > 153.3.251.202.33876: Flags [P.], seq 1:57, ack 1, win 115, length 5602:06:29.992362 IP 153.3.251.202.33876 > root.mysql: Flags [P.], seq 1:63, ack 57, win 256, length 6202:06:29.992411 IP root.mysql > 153.3.251.202.33876: Flags [.], ack 63, win 115, length 002:06:29.992474 IP root.mysql > 153.3.251.202.33876: Flags [P.], seq 57:68, ack 63, win 115, length 1102:06:30.166992 IP 153.3.251.202.33876 > root.mysql: Flags [P.], seq 63:100, ack 68, win 256, length 3702:06:30.167109 IP root.mysql > 153.3.251.202.33876: Flags [P.], seq 68:158, ack 100, win 115, length 9002:06:30.536298 IP 153.3.251.202.33876 > root.mysql: Flags [.], ack 158, win 256, length 002:06:34.568611 IP 153.3.251.202.33876 > root.mysql: Flags [P.], seq 100:105, ack 158, win 256, length 502:06:34.568620 IP 153.3.251.202.33876 > root.mysql: Flags [F.], seq 105, ack 158, win 256, length 002:06:34.568751 IP root.mysql > 153.3.251.202.33876: Flags [F.], seq 158, ack 106, win 115, length 002:06:34.743815 IP 153.3.251.202.33876 > root.mysql: Flags [.], ack 159, win 256, length 0 |