Ubuntu18.04 Mysql exception problem solving (1)

Mysql 1040 Too many connections solutions

Reason analysis There
are too many requests to connect to mysql, which exceeds the maximum number of connections to the mysql database. (There may be two reasons for the excessive requests. One is that the actual request demand is really high, and the other is that the program is abnormal, which causes the resources connected to the mysql database to be unable to be released in time. It is necessary to investigate the abnormal code of the program, especially the database connection part)

Solution process
1. Use the Navicat tool to connect to the mysql database. If you can't connect, restart the mysql service

sudo service mysql restart

2. Check the number of database connections, there will be a lot of connections in sleep state. From the returned host and db, you can check who the connection subject is

SHOW PROCESSLIST;

3. View the maximum number of mysql connections

show variables like "max_connections"; 

4. Temporarily modify the maximum number of mysql connections, and restart mysql to restore the default value

set GLOBAL max_connections=1500;

5. Permanently modify the maximum number of mysql links, modify the mysql startup configuration file mysqld.cnf to set max_connections = 1500, and restart the mysql service

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

6. The "wait_timeout" data in mysql is 28800s by default, which means the number of seconds that mysql waits before automatically closing a sleeping connection. Therefore, this value can be modified so that mysql can automatically close useless connections in time.

set global wait_timeout=500;

7. Killing useless connections may cause manslaughter under normal circumstances. It is generally not recommended to do this. If you have to do it, please refer to this article: https://www.jb51.net/article/65718.htm

8. Generally speaking, the setting of the maximum number of mysql connections depends on the application and cannot be too large, otherwise it will reduce database performance

Guess you like

Origin blog.csdn.net/qq_43314560/article/details/112390551