MySQL中的max_connections 5.7
The max_connections in MySQL 5.7
我遇到了一个问题,MySQL中的max_connction的值是214,我通过编辑my.cnf将它设置为1000,如下所示:
hadoop@node1:~$ mysql -V
mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using EditLine wrapper
MySQL版本:5.7
OS 版本:ubuntu 16.04LTS
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
可以看到,max_connections的变量值是151,然后编辑MySQL的配置文件。
yang2@node1:~$ sudo vi /etc/mysql/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
max_connections=1000
保存配置后重启MySQL服务。
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:~$
现在,我们猜 max_connection 是 1000,真的吗?
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 214 |
+-----------------+-------+
1 row in set (0.00 sec)
是214,这个结果我不太明白,谁能帮帮我?谢谢!
正如 MySQL 关于 max_connections 设置的文档所说:
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.
这意味着您的 MySQL 服务器可能没有足够的资源来维护所需数量的描述符。
MySQL 关于 How MySQL Opens and Closes Tables 的文档明确指出:
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.
在/etc/pam.d/common-session
中添加session required pam_limits.so
(默认情况下通常不存在)。
在/etc/security/limits.conf
中你可以添加一些限制:
* hard nofile 8192
* soft nofile 4096
同时使用 ulimit -a
检查打开的文件限制。
你可以增加 ulimit -n 4096
确保最后重启。
您可以手动设置值,例如
set global max_connections=500;
但是,在 MySQL 重新启动后,该值将重置为 214。
解决方案取决于 OS 和 MySQL 的(版本)。使用 Ubuntu 16.04 和 MySQL >= 5.7.7 以下作品:
systemctl 编辑 mysql
输入
[Service]
LimitNOFILE=8000
保存,这将创建一个新文件
/etc/systemd/system/mysql.service.d/override.conf
并重启服务器:
systemctl daemon-reload
systemctl restart mysql
对于其他环境:
按照以下步骤操作:
cp /lib/systemd/system/mysql.service /etc/systemd/system/
echo -e "\r\nLimitNOFILE=infinity" >> /etc/systemd/system/mysql.service
echo "LimitMEMLOCK=infinity" >> /etc/systemd/system/mysql.service
sudo systemctl daemon-reload
sudo service mysql restart
并将以下行更改或添加到文件 /etc/mysql/mysql.conf.d/mysqld.cnf :
[mysqld]
max_connections=110
就是这个!
@Mahdi_Mohammadi
1.Edit mysql 服务文件
#sudo cat /etc/systemd/system/multi-user.target.wants/mysql.service
# MySQL systemd service file
[Unit]
Description=MySQL Community Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
User=mysql
Group=mysql
PIDFile=/run/mysqld/mysqld.pid
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
TimeoutSec=600
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
##this bellow for tuneup
LimitNOFILE=infinity
LimitMEMLOCK=infinity
2.edit /etc/mysql/mysql.conf.d/mysqld.cnf
max_connections = 99999
我遇到了一个问题,MySQL中的max_connction的值是214,我通过编辑my.cnf将它设置为1000,如下所示:
hadoop@node1:~$ mysql -V
mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using EditLine wrapper
MySQL版本:5.7
OS 版本:ubuntu 16.04LTS
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
可以看到,max_connections的变量值是151,然后编辑MySQL的配置文件。
yang2@node1:~$ sudo vi /etc/mysql/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
max_connections=1000
保存配置后重启MySQL服务。
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:~$
现在,我们猜 max_connection 是 1000,真的吗?
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 214 |
+-----------------+-------+
1 row in set (0.00 sec)
是214,这个结果我不太明白,谁能帮帮我?谢谢!
正如 MySQL 关于 max_connections 设置的文档所说:
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.
这意味着您的 MySQL 服务器可能没有足够的资源来维护所需数量的描述符。
MySQL 关于 How MySQL Opens and Closes Tables 的文档明确指出:
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.
在/etc/pam.d/common-session
中添加session required pam_limits.so
(默认情况下通常不存在)。
在/etc/security/limits.conf
中你可以添加一些限制:
* hard nofile 8192
* soft nofile 4096
同时使用 ulimit -a
检查打开的文件限制。
你可以增加 ulimit -n 4096
确保最后重启。
您可以手动设置值,例如
set global max_connections=500;
但是,在 MySQL 重新启动后,该值将重置为 214。
解决方案取决于 OS 和 MySQL 的(版本)。使用 Ubuntu 16.04 和 MySQL >= 5.7.7 以下作品:
systemctl 编辑 mysql
输入
[Service]
LimitNOFILE=8000
保存,这将创建一个新文件
/etc/systemd/system/mysql.service.d/override.conf
并重启服务器:
systemctl daemon-reload
systemctl restart mysql
对于其他环境:
按照以下步骤操作:
cp /lib/systemd/system/mysql.service /etc/systemd/system/
echo -e "\r\nLimitNOFILE=infinity" >> /etc/systemd/system/mysql.service
echo "LimitMEMLOCK=infinity" >> /etc/systemd/system/mysql.service
sudo systemctl daemon-reload
sudo service mysql restart
并将以下行更改或添加到文件 /etc/mysql/mysql.conf.d/mysqld.cnf :
[mysqld]
max_connections=110
就是这个! @Mahdi_Mohammadi
1.Edit mysql 服务文件
#sudo cat /etc/systemd/system/multi-user.target.wants/mysql.service
# MySQL systemd service file
[Unit]
Description=MySQL Community Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
User=mysql
Group=mysql
PIDFile=/run/mysqld/mysqld.pid
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
TimeoutSec=600
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
##this bellow for tuneup
LimitNOFILE=infinity
LimitMEMLOCK=infinity
2.edit /etc/mysql/mysql.conf.d/mysqld.cnf max_connections = 99999