Docker:MySQL连接慢问题解决

 

问题描述:

由于MySQL是使用Docker容器搭建起来的,在今天的数据库连接中,发现比平时的连接速度变慢了很多,每次连接大概延迟了10秒左右。

排查过程

1、 服务器资源
查看系统的CPU、网络等负载,无异常。

2、数据库连接池
一开始怀疑是连接数过多导致,登入MySQL后发现连接数有近200,于是kill掉一部分,发现还是连接缓慢。
排除连接数导致缓慢。

3.、网络问题
在ping服务器的时候并没有出现数据包延迟、丢包现象。
网络问题排除。

4、MySQL DNS解析
查阅了相关资料,觉得可能是MySQL的DNS解析配置。于是我从内网连接MySQL,居然也是一样慢,一下又没了头绪。

突然想起自己是使用的Docker搭建的MySQL,于是我连入容器内部连接MySQL,秒连!定位到问题所在了,就是MySQL的DNS解析配置问题。

查找了一些相关资料,这是MySQL文档关于DNS的部分说明:

How MySQL uses DNS
When a new thread connects to mysqld, mysqld will spawn a new thread to handle the request. This thread will first check if the hostname is in the hostname cache. If not the thread will call gethostbyaddr_r() and gethostbyname_r() to resolve the hostname.
If the operating system doesn’t support the above thread-safe calls, the thread will lock a mutex and call gethostbyaddr() and gethostbyname() instead. Note that in this case no other thread can resolve other hostnames that is not in the hostname cache until the first thread is ready.
You can disable DNS host lookup by starting mysqld with –skip-name-resolve. In this case you can however only use IP names in the MySQL privilege tables.
If you have a very slow DNS and many hosts, you can get more performance by either disabling DNS lookop with –skip-name-resolve or by increasing the HOST_CACHE_SIZE define (default: 128) and recompile mysqld.

大概意思就是说如果你有一个非常慢的DNS和许多主机,您可以通过使用-skip-name-resolve禁用DNS

解决过程

修改MySQL配置文件,添加skip-name-resolve

[mysqld]
skip-name-resolve

重启MySQL容器:

[root@template-centos7 /root]#docker restart mysqlN
mysqlN

重启完连接测试,秒连!

问题解决。

原文链接:

https://blog.csdn.net/bacteriumx/article/details/82792984

侵删

我虽然也遇到了链接慢的问题,但是却不同于上述问题,在容器外连接mysql服务也会延迟,但是进入容器连接mysql发现秒连,而且发现在容器外链接容器中的mysql服务使用的密码和在容器中链接mysql服务的密码不一样,应该是启动容器时设置了mysql的链接密码,进入容器后又修改了mysql的密码造成的。删除容器,重新运行容器并设置mysql的密码,再次链接,秒连成功。

猜你喜欢

转载自blog.csdn.net/GorgeousChou/article/details/86618999