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

重启完连接测试,秒连!

问题解决。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值