MySQL database connection failed, error: ERROR 1040 (HY000): Too many connections

MySQL database connection failed, error: ERROR 1040 (HY000): Too many connections

Problem Description:

"ERROR 1040 (HY000): Too many connections" suddenly appears when connecting to MySQL. According to the literal meaning, there should be too many terminals in the database. The following is a solution to the problem by modifying the mysql configuration file.

Cause Analysis:

The default number of MySQL connections is 151

Solution:

Temporary configuration

If the maximum number of connections appears in the production environment database, it is recommended to use temporary configuration, and then modify the configuration file and wait for the next restart.
When you cannot log in to the database, first stop the application connected to the database and wait for a few minutes to enter the database again.
1. View Current number of database connections

mysql> show variables like 'max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.00 sec)

2. Modify the number of database connections (note: restart to restore to default)

mysql> set GLOBAL max_connections=500;
Query OK, 0 rows affected (0.00 sec)

3. Check the modified maximum number of connections

mysql> show variables like 'max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 500   |
+-----------------+-------+
1 row in set (0.00 sec)

Permanent configuration

1. Open the mysql configuration file my.cnf

[mysqld]
max_connections = 500

Find the max_connections field, change the maximum number of connections to
more than 500, complete the modification, and restart the server! !

Mysql checks the number of connections (total number of connections, number of active connections, maximum number of concurrent connections)

mysql> show status like 'Threads%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 0     |
| Threads_connected | 1     |
| Threads_created   | 1     |
| Threads_running   | 2     |
+-------------------+-------+
4 rows in set (0.01 sec)

Threads_connected: This value refers to the number of open connections.

Threads_running: This value refers to the number of activated connections. This value is generally much lower than the connected value.

Threads_connected has the same result as show processlist, indicating the current number of connections. To be precise, Threads_running represents the current number of concurrent

The number of threads of the MySQL server needs to be within a reasonable range to ensure the healthy and smooth operation of the MySQL server. Threads_created indicates the number of threads created. By viewing Threads_created, you can view the process status of the MySQL server.

mysql> show global status like 'Thread%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 0     |
| Threads_connected | 1     |
| Threads_created   | 1     |
| Threads_running   | 2     |
+-------------------+-------+
4 rows in set (0.00 sec)

If we set thread_cache_size in the MySQL server configuration file, when the client disconnects, the server's thread processing this client will be cached to respond to the next client instead of being destroyed (provided that the number of caches has not reached the upper limit).

Threads_created indicates the number of threads created. If the value of Threads_created is found to be too large, it indicates that the MySQL server has been creating threads, which is also relatively resource-intensive. You can appropriately increase the thread_cache_size value in the configuration file and query the value of the server thread_cache_size:

mysql> show variables like 'thread_cache_size';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| thread_cache_size | 100    |
+-------------------+-------+
1 row in set (0.01 sec)

Command: show processlist;
if it is a root account, you can see the current connections of all users. If it is another ordinary account, you can only see the connections occupied by you.
The show processlist command only lists the first 100 items. If you want to list them all, please use show full processlist;

mysql> show processlist;
+----+-----------------+-----------+------+---------+------+------------------------+------------------+
| Id | User            | Host      | db   | Command | Time | State                  | Info             |
+----+-----------------+-----------+------+---------+------+------------------------+------------------+
|  5 | event_scheduler | localhost | NULL | Daemon  | 2405 | Waiting on empty queue | NULL             |
|  8 | root            | localhost | NULL | Query   |    0 | init                   | show processlist |
+----+-----------------+-----------+------+---------+------+------------------------+------------------+
2 rows in set (0.00 sec)

Command: show status;

mysql>show status like '%变量名%';

The variable names are as follows:
Aborted_clients The number of connections that have been abandoned because the client did not close the connection properly and has died.
Aborted_connects The number of attempts to connect to the MySQL server that have failed.
Connections Number of attempts to connect to the MySQL server.
Created_tmp_tables The number of implicit temporary tables that have been created when the statement is executed.
Delayed_insert_threads The number of delayed insertion processor threads in use.
Delayed_writes The number of rows written with INSERT DELAYED.
Delayed_errors The number of rows written with INSERT DELAYED that had some errors (possible duplicate key values).
Flush_commands The number of times FLUSH commands are executed.
Handler_delete The number of times a row was requested to be deleted from a table.
Handler_read_first requests the number of times to read the first row in the table.
Handler_read_key requests a number of rows to read based on the key.
Handler_read_next The number of times requested to read a row based on a key.
Handler_read_rnd requests the number of times to read a row based on a fixed position.
Handler_update The number of times a row in the table is requested to be updated.
Handler_write The number of times a row was requested to be inserted into the table.
Key_blocks_used Number of blocks used for key caching.
Key_read_requests The number of requests to read a key value from the cache.
Key_reads The number of times a key value is physically read from disk.
Key_write_requests The number of times a keyword block is requested to be written to the cache.
Key_writes The number of times a key-value block is physically written to disk.
Max_used_connections The maximum number of connections used simultaneously.
Not_flushed_key_blocks Key blocks that have changed in the key cache but have not yet been flushed to disk.
Not_flushed_delayed_rows The number of rows waiting to be written in the INSERT DELAY queue.
Open_tables Number of open tables.
Open_files Number of open files.
Open_streams The number of open streams (mainly used for logging)
Opened_tables The number of tables that have been opened.
Questions The number of queries sent to the server.
Slow_queries The number of queries that take longer than long_query_time.
Threads_connected The number of currently open connections.
Threads_running The number of threads that are not sleeping.
Uptime How long the server has been working, in seconds.

Guess you like

Origin blog.csdn.net/weixin_46010834/article/details/130102297