问题
连接远程MySQL,出现too many connections问题,本地登上去看一下:
show full processlist;
看到如下结果:
原因
客户端访问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_timeout
和 interactive_timeout
从原来的默认值8小时改成180秒。
查看最大连接数现在
show variables like '%max_connections%';
修改最大连接数据限制
set GLOBAL max_connections = 200;
查询历史最高连接数
show global status like 'Max_used_connections';
总结
以后自己手动安装完mysql后,记得配置mysql超时设置,避免一堆sleep连接。