Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The max_connections in MySQL 5.7

I met a problem, the value of max_connction in MySQL is 214 after I set it 1000 via edit the my.cnf, just like below:

hadoop@node1:~$ mysql -V
mysql  Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using  EditLine wrapper

MySQL version: 5.7

OS version : ubuntu 16.04LTS

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

As we can see, the variable value of max_connections is 151. Then , I edit the configuration file of MySQL.

yang2@node1:~$ sudo vi /etc/mysql/my.cnf
[mysqld]

character-set-server=utf8
collation-server=utf8_general_ci
max_connections=1000

Restart MySQL service after save the configraion.

yang2@node1:~$ service mysql restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Multiple identities can be used for authentication:
 1.  yangqiang,,, (yang2)
 2.  ,,, (hadoop)
Choose identity to authenticate as (1-2): 1
Password: 
==== AUTHENTICATION COMPLETE ===
yang2@node1:~$ 

Now, we guess the max_connection is 1000, really?

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

It is 214. I do not really understand this result, who can help me? thx!

like image 261
StrongYoung Avatar asked Oct 11 '16 11:10

StrongYoung


People also ask

What is Max_connections in MySQL?

The system variable max_connections determines the number of connections which MySQL/MariaDB will accept. The default value is 151 connections, which allows 150 normal connections plus one connection from the SUPER account.

How do I permanently set a maximum connection in MySQL?

mysql> SET GLOBAL max_connections = 250; To set this value permanently, edit mysql configuration file on your server and set following variable. The configuration file location may change as per your operating system. By default you can find this at /etc/my.

How to set the max number of connections in MySQL?

Login to you mysql terminal with privileged user and execute following query. mysql> SHOW VARIABLES LIKE "max_connections"; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 150 | +-----------------+-------+ 1 row in set (0.00 sec) As per above output max_connections value is set to 150.

Why do I get too many connections errors in MySQL?

If clients encounter Too many connections errors when attempting to connect to the mysqld server, all available connections are in use by other clients. The permitted number of connections is controlled by the max_connections system variable. The default value is 151 to improve performance when MySQL is used with the Apache Web server.

What does the MySQL max_connections reached maximum error 151 mean?

Value 151 doesn’t mean that only 151 number of connections are allowed. When there is a sudden surge in MySQL requests, typically due to heavy traffic, the server will not be able to handle them. As a result, websites start showing the MySQL max_connections reached maximum error.

What is the maximum number of connections a database user can have?

The default value for this is 151. But, there is an extra default connection on top of the max_connections limit. Generally, this is reserved for the database user having SUPER privilege for diagnosing connection problems.


2 Answers

You may set the value manually, e.g.

set global max_connections=500;

however, after a restart of MySQL the value is reset to 214.

The solution depends on the (version of) OS and the MySQL version. With Ubuntu 16.04 and MySQL >= 5.7.7 following works:

systemctl edit mysql

Enter

[Service]
LimitNOFILE=8000

save, this will create a new file

/etc/systemd/system/mysql.service.d/override.conf

and restart the server:

systemctl daemon-reload
systemctl restart mysql

For other environments: Can not increase max_open_files for Mysql max-connections in Ubuntu 15

like image 191
WernerE Avatar answered Oct 01 '22 19:10

WernerE


As MySQL documentation on max_connections setting says:

Increasing this value increases the number of file descriptors that mysqld requires. If the required number of descriptors are not available, the server reduces the value of max_connections.

This means that probably your MySQL server does not have enough resources to maintain the required number of descriptors.

MySQL documentation on How MySQL Opens and Closes Tables makes it clear that:

The table_open_cache and max_connections system variables affect the maximum number of files the server keeps open. If you increase one or both of these values, you may run up against a limit imposed by your operating system on the per-process number of open file descriptors. Many operating systems permit you to increase the open-files limit, although the method varies widely from system to system. Consult your operating system documentation to determine whether it is possible to increase the limit and how to do so.

like image 21
Shadow Avatar answered Oct 01 '22 20:10

Shadow