01-MySQL查看连接线程

2020-03-01  本文已影响0人  当当一丢丢

背景说明

本文使用MySQL 5.7进行分析,系统环境MacBook,讨论验证wait_timeout作用,MySQL默认配置文件/etc/my.cnf

一、MySQL默认配置下的线程连接情况

1、MySQL默认wait_timeout=8hour

show variables like '%wait_timeout%'
image.png

2、查看MySQL线程连接情况

2.1 背景:启动SpringBoot项目(访问DB即可)只配置默认DataSource参数,使用Tomcat8

2.2 查看线程连接情况

show full processlist
image.png

2.3 线程情况说明

二、更改MySQL server wait_timeout默认时间

1、使用配置文件方式更新

[mysqld]
wait_timeout=60
character-set-server=utf8mb4
default-time-zone = '+8:00'

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4
show global  gvariables like '%wait_timeout%'

2、重新启动SpringBoot应用

2.1 不断执行:show full processlist命令

image.png image.png

2.2 结果分析

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 355,390 milliseconds ago.  The last packet sent successfully to the server was 355,390 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
        at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:562)
        at com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:732)
        at com.mysql.cj.protocol.a.NativeProtocol.sendCommand(NativeProtocol.java:671)
        at com.mysql.cj.protocol.a.NativeProtocol.sendQueryPacket(NativeProtocol.java:986)
        at com.mysql.cj.NativeSession.execSQL(NativeSession.java:1157)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:947)
        ... 62 more
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:210)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at com.mysql.cj.protocol.ReadAheadInputStream.fill(ReadAheadInputStream.java:107)
        at com.mysql.cj.protocol.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:150)
        at com.mysql.cj.protocol.ReadAheadInputStream.read(ReadAheadInputStream.java:180)
admindeMacBook-Pro-7:~ 07$ lsof -iTCP |grep mysql
java      17752 qiankai07  166u  IPv6 0xf3d007512bb170e9      0t0  TCP localhost:52540->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  167u  IPv6 0xf3d007512bb13d29      0t0  TCP localhost:52541->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  168u  IPv6 0xf3d007512bb159e9      0t0  TCP localhost:52542->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  169u  IPv6 0xf3d0075121c60569      0t0  TCP localhost:52543->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  170u  IPv6 0xf3d0075121c610e9      0t0  TCP localhost:52544->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  171u  IPv6 0xf3d0075121c5ee69      0t0  TCP localhost:52545->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  172u  IPv6 0xf3d0075121c60b29      0t0  TCP localhost:52546->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  173u  IPv6 0xf3d007511c1cb8a9      0t0  TCP localhost:52547->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  174u  IPv6 0xf3d007511c1cbe69      0t0  TCP localhost:52548->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  175u  IPv6 0xf3d007511c1cb2e9      0t0  TCP localhost:52549->localhost:mysql (ESTABLISHED)
java      17752 qiankai07  176u  IPv6 0xf3d007511c1cdb29      0t0  TCP localhost:52550->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  177u  IPv6 0xf3d007512232ad29      0t0  TCP localhost:52551->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  178u  IPv6 0xf3d007512232e0e9      0t0  TCP localhost:52552->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  179u  IPv6 0xf3d00751201e5b29      0t0  TCP localhost:52553->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  180u  IPv6 0xf3d007511c391e69      0t0  TCP localhost:52554->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  181u  IPv6 0xf3d007511c3912e9      0t0  TCP localhost:52555->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  182u  IPv6 0xf3d007511c392fa9      0t0  TCP localhost:52556->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  183u  IPv6 0xf3d007511c3918a9      0t0  TCP localhost:52557->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  184u  IPv6 0xf3d00750ff44f9e9      0t0  TCP localhost:52558->localhost:mysql (CLOSE_WAIT)
java      17752 qiankai07  185u  IPv6 0xf3d00750ff450569      0t0  TCP localhost:52559->localhost:mysql (CLOSE_WAIT)

三、总结

就算以MySQL默认 wait_timeout值(8hour)有时候也会报wait_timeout异常** 解决wait_timeout异常

1. 可以将wait_timeout设为更高值

2. 使用阿里德鲁伊Druid作为数据库连接池

3. 使用spring.datasource.testWhileIdle=true字段

image.png

流程总结

testWhileIdel会保持连接新鲜程度

四、附-常用命令

#Mac MySQL重启
#启动MySQL服务
sudo /usr/local/[MySQL](http://lib.csdn.net/base/14 "undefined")/support-files/mysql.server start
#停止MySQL服务
sudo /usr/local/mysql/support-files/mysql.server stop
#重启MySQL服务
sudo /usr/local/mysql/support-files/mysql.server restart

#Mac取代netstat方式查看tcp,可以用grep做很多事
lsof -iTCP |grep mysql
上一篇下一篇

猜你喜欢

热点阅读