mysql报错:Data source rejected establishment of connection, message from server: \“Too many connectio

I’m doing a stress test recently, and I need to gradually increase the number of users for verification. Each user creates a separate database for connection, so it is necessary to continuously create the database. This error is also easy to understand. The number of mysql connections is not enough.

report error

The error message is as follows:

"SQLState":"08004","vendorCode":1040,"detailMessage":
"Data source rejected establishment of connection,message from server: \"Too many connections\""

reason

The root cause is that the number of mysql connections is not enough,
but it depends on the situation why it is not enough

1. Is the max_connections attribute configuration of mysql too small?
2. Or is the session not closed for multiple insert and update operations?

If you are not sure, just keep reading

solve

We need to know that the range of the maximum number of connections that can be set is different depending on the version of mysql:
Mysql5.5~5.7: the default maximum number of connections is 151, the upper limit is: 100000
Mysql5.0 version: the default maximum number of connections The number is 100, the upper limit is 16384

Check mysql version

select version();

As follows, I am here based on mysql 5.7, so the default maximum number of connections should be 151
insert image description here

View the current mysql maximum connection limit

show variables like 'max_connections';

insert image description here
The current maximum connection limit is 151, indicating that the maximum connection limit has not been changed

Determine the cause of insufficient connections

As shown above, my current situation is that the maximum number of connections is limited to 151, and more than a dozen databases have been created before ; at the same time, I checked again by myself, and there is no unclosed session operation here.

So the reason is that the max_connections attribute configuration is too small

Modify the maximum number of connections

There are two ways to modify the maximum number of connections of the mysql client:
one is to use the command setting, and the other is to directly modify the my.cnf file

command settings

Using the command to set this method is a temporary modification, which can only cure the symptoms but not the root cause. It is available under the condition that the mysql service does not restart. If the mysql restarts, it will restore the default number of mysql connections (or restore to the configuration in the my.cnf file the maximum number of connections).

But since I'm testing, it doesn't matter, the number of connections after mysql restart

set global max_connections=1500;

insert image description here

Modify the my.cnf file

After mysql is restarted, the configuration in the my.cnf configuration file will be used first. After setting with the above command, if the mysql service is restarted, it will still return to the maximum number of connections configured in the my.cnf file (or the default value).

# 修改my.cnf文件,在文件中加入如下属性
max_connections=1500

# 然后重启mysql服务
$ service mysqld restart

Create a database connection again, no problem!

Guess you like

Origin blog.csdn.net/m0_37482190/article/details/128714147