error! ! ! Too many connections (too many connections, resulting in failure to connect to the database, and business cannot be performed normally)

Too many connections (too many connections, resulting in failure to connect to the database, business can not be normal

1. Problem recovery

mysql> show variables like '%max_connection%'; 
| Variable_name   | Value | 
max_connections | 151   |  
mysql> set global max_connections=1;Query OK, 0 rows affected (0.00 sec) 
[root@node4 ~]# mysql -uzs -p123456 -h 192.168.56.132 
ERROR 1040 (00000): Too many connections 

Two, solution ideas

  • 1. First, consider whether the corresponding max_connections parameter value in our MySQL database parameter file is set too small, causing the number of client connections to exceed the maximum value that the database can withstand.
    The default size of this value is 151, we can adjust it according to the actual situation.

Three, the solution

  • set global max_connections=500

  • 1. But this adjustment will have hidden dangers, because we cannot confirm whether the database can bear such a large connection pressure. It is like that a person can only eat one steamed bun, but now he has to eat 10, and he will definitely not accept it. When reflected on the server, there may be downtime.

  • 2. Secondly, you can limit the number of concurrent processing of Innodb. If innodb_thread_concurrency = 0 (this kind of representation is not limited), you can first change it to 16 or 64 depending on the server pressure.

  • 3. If it is very large, you can change it to a lower value to let the pressure on the server come down, and then increase it slowly. According to your own business, personal suggestion can be adjusted to 16 first.

  • 4. MySQL performance will decrease with the increase in the number of connections. Before MySQL 5.7, it is necessary to let developers set up thread pool and reuse connections. After MySQL 5.7, the database has its own thread pool, and the connection number problem has been solved accordingly.

对于有的监控程序会读取 information_schema 下面的表,可以考虑关闭下面的参数:
innodb_stats_on_metadata=0
set global innodb_stats_on_metadata=0

Guess you like

Origin blog.csdn.net/weixin_45647891/article/details/114236362