Concurrent mysql connections

bellwood

Well-Known Member
PartnerNOC
Sep 25, 2012
92
43
143
New York
cPanel Access Level
DataCenter Provider
Would the following not work in /etc/my.cnf:

Code:
max_connections = 500
max_user_connections = 25
...where 500 would be the maximum connection limit to the daemon (whatever you deem appropriate for your deployment) and 25 would be the max connections per user:

REF: MySQL :: MySQL 5.7 Reference Manual :: 5.1.7 Server System Variables

If you didn't need this to be ubiquitous for the entire server, you could also limit per-user:

Code:
ALTER USER 'specificUser'@'localhost' WITH MAX_USER_CONNECTIONS 25;
REF: MySQL :: MySQL 5.7 Reference Manual :: 6.2.16 Setting Account Resource Limits
 

Takito

Member
Feb 14, 2017
21
1
53
UA
cPanel Access Level
Root Administrator
I have something to add, in case others will need it.

Code:
max_connections = 500
can be changed in WHM > SQL Services > Edit SQL Configuration

max_user_connections = 25, I didn't have this in /etc/my.cnf and when added the load increased dramatically after some time, MySQL started to crash. Maybe the reason was that in Cloudlinux DB governor I had lower number of connections and max_user_connections overrides Cloudlinux settings, I don't know. When I removed it everything went back to normal.

Another thing, at first I thought this command(and the same above max_user_connections) limits cpanel user, but it actually limits database user inside a cpanel account. In my case, one cpanel account had 30+ MySQL users and this thing doesn't help at all.
Code:
ALTER USER 'specificUser'@'localhost' WITH MAX_USER_CONNECTIONS 25;
In this case you need to limit each DB user separately. Here is the command to list all users in the proper format to limit them

Code:
SELECT con cat("ALTER USER '", `user`, "'@'localhost' WITH MAX_USER_CONNECTIONS 1;") FROM `mysql`.`user`
("concat" should be together, the forum won't let me use it here)

If using phphmyadmin then click "options" below, full text, go. Then copy the needed user/s and execute the command. Setting connections to 1 reduced the load significantly in my case.