在使用虚拟主机过程中,如果应用程序连接mysql的连接数过多,会导致网页程序报如下错误:
Can not connect to MySQL server
Error: Too many connections
此问题的原因是由于程序没有及时释放mysql数据库连接,连接数堆积、变满,导致新的连接无法连接到mysql。
1.查看数据库的最大连接数:
mysql>show variables like ‘%max_connections%‘;
+-------------------------+-----------------+
| Variable_name | Value
+-------------------------+-----------------+
| extra_max_connections | |
| max_connections | 110 |
+-------------------------+-----------------+
共返回 2 行记录,花费 121.61 ms.
2.查看连接数据库IP的连接数:
mysql>select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;
+----------------+--------------------+
| ip | count(*) |
+----------------+--------------------+
| 110.110.10.110 | 1 |
| 110.110.10.111 | 3 |
| 110.110.10.112 | 2 |
| 110.110.10.113 | 1 |
| 110.110.10.114 | 2 |
| 110.110.10.115 | 10 |
| 110.110.10.116 | 2 |
+----------------+--------------------+
共返回 7 行记录,花费 69.87 ms.
3.查看当前数据库连接状态:
mysql>show full processlist;
4.kill掉ID持续时间很长的连接
mysql>kill 35007306;