MySQL问题too many connections之Sleep连接

问题

连接远程MySQL,出现too many connections问题,本地登上去看一下:

show full processlist;

看到如下结果:
一堆Sleep连接

原因

客户端访问mysql服务器没有主动释放连接,导致一堆连接状态是Sleep状态。

解决

修改mysql配置文件即在mysql文件中添加两个超时参数设置即可。

  • interactive_timeout:交互方式的连接的等待时间
  • wait_timeout:非交互方式的连接等待时间

交互方式其实就是人通过交互客户端去连mysql服务器;非交互方式其实就是程序通过类似jdbc等其它协议来连接mysql数据库。

查看当前配置

cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

/etc/my.cnf文件可以看出,这台mysql服务器主要配置在/etc/my.cnf.d目录:

$ cd /etc/my.cnf.d
$ ll
total 12
-rw-r--r--. 1 root root 295 Dec 10  2018 client.cnf
-rw-r--r--. 1 root root 565 Aug  7  2019 mysql-default-authentication-plugin.cnf
-rw-r--r--. 1 root root 722 Sep 27 02:44 mysql-server.cnf

从上述目录可,我们主要修改mysql-server.cnf文件,即:

sudo vim mysql-server.cnf

添加如下配置:

[mysqld]
interactive_timeout=180
wait_timeout=180
max_connections=200

然后,重启mysqld,登录mysql,再次验证配置。

验证配置

mysql> show variables like '%timeout%';
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| connect_timeout                   | 10       |
| delayed_insert_timeout            | 300      |
| have_statement_timeout            | YES      |
| innodb_flush_log_at_timeout       | 1        |
| innodb_lock_wait_timeout          | 50       |
| innodb_rollback_on_timeout        | OFF      |
| interactive_timeout               | 180      |
| lock_wait_timeout                 | 31536000 |
| mysqlx_connect_timeout            | 30       |
| mysqlx_idle_worker_thread_timeout | 60       |
| mysqlx_interactive_timeout        | 28800    |
| mysqlx_port_open_timeout          | 0        |
| mysqlx_read_timeout               | 30       |
| mysqlx_wait_timeout               | 28800    |
| mysqlx_write_timeout              | 60       |
| net_read_timeout                  | 30       |
| net_write_timeout                 | 60       |
| rpl_stop_slave_timeout            | 31536000 |
| slave_net_timeout                 | 60       |
| wait_timeout                      | 180      |
+-----------------------------------+----------+

从上面查询可知,wait_timeoutinteractive_timeout从原来的默认值8小时改成180秒。

查看最大连接数现在

show variables like '%max_connections%';

修改最大连接数据限制

set GLOBAL max_connections = 200;

查询历史最高连接数

show global status like 'Max_used_connections';

总结

以后自己手动安装完mysql后,记得配置mysql超时设置,避免一堆sleep连接。

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值