MySQL中如何查看和更改的max_connections值

时间:2019-05-19 01:25:41  来源:igfitidea点击:

有时,在MySQL服务器可能会遇到问题,太多的连接too many connections。
要解决这个问题,可以增加mysql配置中的 max_connections值。

查看Mysql的max_connections值

Mysql 使用名为max_connections的变量存储最大连接值。
使用root用户登录到mysql终端并执行以下查询。

mysql> SHOW VARIABLES LIKE "max_connections";

+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 150   |
+-----------------+-------+
1 row in set (0.00 sec)

根据上面的输出,max_connections的值被设置为150。

修改Mysql的max_connections值

在增加这个值之前,请确保服务器有足够的资源来处理更多的查询。
现在在mysql终端执行下面的查询来临时设置这个值。
记住,这个值将在下一次mysql重新启动时重置。

mysql> SET GLOBAL max_connections = 250;

要永久设置此值,请在服务器上编辑mysql配置文件并设置以下变量。
配置文件位置可能会根据操作系统的不同而改变。

默认情况下,在CentOS和RHEL系统上 /etc/my.cnf
基于Debian系统上 /etc/mysql/my.cnf

max_connections = 250

现在重新启动mysql服务,用上面的命令再次检查值。
这次我们将看到该值被设置为250。