【MySQL】Connections Number Setting

Posted by 西维蜀黍 on 2020-04-21, Last Modified on 2023-05-02

MySQL 当前允许最大并发连接数(max connection)

max connection is a threshold specified in MySQL: the maximum permitted number of simultaneous client connections (if exceed, apps will get “Too many connections” error from 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. To support more connections, set max_connections to a larger value.

Command-Line Format --max-connections=#
System Variable max_connections
Scope Global
Dynamic Yes
SET_VAR Hint Applies No
Type Integer
Default Value 151
Minimum Value 1
Maximum Value 100000

mysqld actually permits max_connections + 1 client connections. The extra connection is reserved for use by accounts that have the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege). By granting the privilege to administrators and not to normal users (who should not need it), an administrator can connect to the server and use SHOW PROCESSLIST to diagnose problems even if the maximum number of unprivileged clients are connected.

查看 MySQL 的最大并发连接数设置:

$ mysql –u root –p
mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+

修改

Approach 1 - via Command

You can change the setting to e.g. 200 by issuing the following command without having to restart the MySQL server (obviously it will only work if the user you are logged in as has sufficient permissions to do this):

set global max_connections = 200;

This will take effect immediately, but will be forgotten the next time MySQL is restarted. To make the change permanent you need to edit the my.cnf configuration file.

Approach 2 -Via Config

$ vi /etc/my.cnf

There will be a section that looks like this: (may be slightly different)

[mysqld]
local-infile=0
datadir=/var/lib/mysql
user=mysql
symbolic-links=0

max_connections = 100

Save the changes and type the following to restart mysqld:

$ /etc/init.d/mysqld restart

You will see:

Stopping mysql: [ OK ]
Starting mysql: [ OK ]

MySQL 同时被使用的连接数的历史最大值(max used connection)

max used connection is the maximum number of connections that have been in use simultaneously since the server started.

mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name    | Value |
+----------------------+-------+
| Max_used_connections | 2   |
+----------------------+-------+
1 row in set (0.00 sec)

MySQL 当前活跃连接数(active connection num)

mysql> show processlist;

mysql> select count(*) from information_schema.processlist;

mysql> select id,
       user,
       host,
       db,
       command,
       time,
       state,
       info
from information_schema.processlist;

# group by user names
mysql> select user, substring_index(host, ":", 1), db, count(*) from information_schema.processlist where db like 'my_company_promotion_backend%' group by user, substring_index(host, ":", 1), db;

# get active connection num by a user name
mysql> select user, substring_index(host, ":", 1), db, count(*) from information_schema.processlist where db like 'my_company_promotion_backend%' and user="<user name>" group by user, substring_index(host, ":", 1), db

mysql> show status where variable_name = 'threads_connected';

Reference

Official

Misc