Skip to main content

How to Change max_connections in MySQL and Keep It After a Restart

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

SET GLOBAL max_connections works right away, and a restart forgets it. SET PERSIST keeps it, with no config file to edit, which is handy in a Docker container. I set it, restarted MySQL, and checked where the value came from.

Before You Raise It​

The default is 151. If you're hitting it, first see who has the connections: a stuck app or an oversized pool will just use up a bigger limit too. Each connection also uses memory on the server, so a higher limit needs a server with room for it.

Step 1: Change It​

As an administrator:

mysql>
SHOW VARIABLES LIKE 'max_connections';
SET PERSIST max_connections = 300;
SHOW VARIABLES LIKE 'max_connections';
Output
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.002 sec)

Query OK, 0 rows affected (0.002 sec)

+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 300 |
+-----------------+-------+
1 row in set (0.001 sec)

It applies at once, to the running server. SET PERSIST also saves it:

mysql>
SELECT VARIABLE_NAME, VARIABLE_VALUE FROM performance_schema.persisted_variables;
Output
+-----------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+-----------------+----------------+
| max_connections | 300 |
+-----------------+----------------+
1 row in set (0.000 sec)

Step 2: Restart, and Check​

Terminal
docker compose restart mysql
mysql>
SHOW VARIABLES LIKE 'max_connections';
SELECT VARIABLE_NAME, VARIABLE_SOURCE, VARIABLE_PATH FROM performance_schema.variables_info WHERE VARIABLE_NAME = 'max_connections';
Output
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 300 |
+-----------------+-------+
1 row in set (0.002 sec)

+-----------------+-----------------+--------------------------------+
| VARIABLE_NAME | VARIABLE_SOURCE | VARIABLE_PATH |
+-----------------+-----------------+--------------------------------+
| max_connections | PERSISTED | /var/lib/mysql/mysqld-auto.cnf |
+-----------------+-----------------+--------------------------------+
1 row in set (0.001 sec)

max_connections at 300 after a restart, with variables_info showing its source as PERSISTED and its path as /var/lib/mysql/mysqld-auto.cnf.

It's still 300, and VARIABLE_SOURCE says why: PERSISTED, from mysqld-auto.cnf in the data folder. In Docker, that file lives in the volume, so it survives a new container as well.

Step 3: Put It Back​

mysql>
RESET PERSIST max_connections;
SET GLOBAL max_connections = 151;
SHOW VARIABLES LIKE 'max_connections';
Output
Query OK, 0 rows affected (0.002 sec)

Query OK, 0 rows affected (0.000 sec)

+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.001 sec)

RESET PERSIST only forgets the saved value, and the running value stays at 300 until you change it, so the SET GLOBAL is needed too. How to Undo SET PERSIST in MySQL goes through that.

SET PERSIST or SET GLOBAL?​

Applies nowSurvives a restart
SET GLOBALYesNo
SET PERSISTYesYes
SET PERSIST_ONLYNoYes
A my.cnf fileNoYes

Common Mistakes​

MistakeWhat happensDo this instead
Using SET GLOBAL for a lasting changeIt's gone at the next restartUse SET PERSIST
Raising it to fix a stuck appThe new limit fills up tooFind what's holding them
Forgetting what you persistedA value overrides your config file, and you can't see whyCheck persisted_variables

Troubleshooting​

SymptomLikely causeFix
ERROR 1040 right now, and you can't get inThe extra administrator connection may be freeThe fix
Clients still fail after raising itThe pool's own limitThe fix
The value isn't what my.cnf saysA persisted value winsCheck variables_info

Conclusion​

SET PERSIST max_connections changes it now and keeps it. variables_info tells you where any value came from, and RESET PERSIST forgets it.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.