Article directory
1. Reproduce the error
After starting the project today, the console reported the following error:
java.sql.SQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1643)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1709)
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2813)
ie Data source rejected establishment of connection, message from server: "Too many connections"
.
2. Analysis errors
I am catching up with the recent ChatGPT
fire, and I use him to solve my mistakes:
ChatGPT
It is said that this is a database connection problem, which may be caused by too many connections.
So, check the number of connections with the following command:
mysql> show global status like 'Thread%';
+-------------------+-----------+
| Variable_name | Value |
+-------------------+-----------+
| Threads_cached | 4 |
| Threads_connected | 1 |
| Threads_created | 5 |
| Threads_running | 160 |
+-------------------+-----------+
4 rows in set (0.01 sec)
-
Threads_cached
:MySQL
How many resources can be reused in the managed thread pool. -
Threads_connected
: This value refers to the number of open connections. -
Threads_created
: Indicates the number of created threads.
If Threads_created
the value is found to be too large, it means that MySQL
the server has been creating threads, which is also relatively resource-intensive. You can appropriately increase the value in the configuration file thread_cache_size
, as shown in the following code:
mysql> set global thread_cache_size=60;
Query OK, 0 rows affected (0.00 sec)
Threads_running
: This value refers to the number of active connections, which is generally much lower thanconnected
the value.
Threads_connected
Same as show processlist
the result, indicating the current number of connections.
As follows show processlist;
, the execution result is as follows:
mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 62 | root | localhost:56520 | NULL | Query | 0 | starting | show processlist |
+----+------+-----------------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)
Threads_connected
The value of is 1, and show processlist
1 piece of data is detected.
Therefore, the same Threads_connected
as show processlist
the result.
To be precise, Threads_running
it represents the current number of concurrency, as can be seen from the above, the number of concurrency is 160 .
Therefore, query the maximum number of connections set by the database, as shown in the following code:
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set, 1 warning (0.04 sec)
The concurrent number of the database is 160
, and the maximum number of connections is 151
.
It can be seen that the number of concurrent databases exceeds the maximum number of connections. Therefore, we need to modify the maximum number of connections.
In addition to the maximum number of connections, you also need to check the connection duration, as shown in the following code:
mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
The connection duration is 28800
, that is, the default is 8
hours.
3. Solve the problem
When we modify the maximum number of connections, we also need to modify the connection duration.
- Modify the maximum number of connections
Because the number of concurrent databases exceeds the maximum number of connections, we can modify the maximum number of connections as follows:
mysql> set GLOBAL max_connections=1000;
Query OK, 0 rows affected (0.04 sec)
Re-query the maximum number of connections to verify whether the setting is successful:
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+
1 row in set, 1 warning (0.01 sec)
At this point, it can be Too many connections
a problem.
In addition to setting the maximum number of connections ( max_connections
), we also need to modify mysql
the duration of the number of connections.
- Modify connection duration
We can use the following command to modify the connection duration and automatically kill the thread.
mysql> set global wait_timeout=300;
Query OK, 0 rows affected (0.00 sec)
Use the following command to check whether the modification is successful:
mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 300 |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
This method is only a temporary modification, and mysql
it will be invalid after restarting.
Thus, the modified mysql
configuration /etc/my.cnf
.
After the modification is complete, restart mysql5.7
it:
4. Supplementary Notes
For show variables like 'xxx'
more knowledge about it, you can refer to this blog post: The whole network introduces show variables like '%xxx%' in MySQL in detail