原文

https://ubiq.co/database-blog/how-to-increase-max-connections-in-mysql/

引言

有时候连贯Mysql时候咱们会遇到“Too many connections”这样的报错,当然更多可能是在生产过程中碰到这样的问题,默认状况下Mysql的整体服务器连接数设置过低。

Sometimes MySQL server may give “Too many connections” error message when you try to connect to a MySQL database. Here’s how to increase max connections in MySQL to fix this problem.

Too many connections 的影响

这意味着所有可用的连贯曾经被各种客户端应用,你的MySQL服务器不能关上任何新的连贯,直到任何现有的连贯被敞开。

By default, MySQL 5.5+ can handle up to 151 connections. This number is stored in server variable called _max_connections_. You can update max_connections variable to increase maximum supported connections in MySQL, provided your server has enough RAM to support the increased connections.

max_connections默认值

By default, MySQL 5.5+ can handle up to 151 connections. This number is stored in server variable called _max_connections_. You can update max_connections variable to increase maximum supported connections in MySQL, provided your server has enough RAM to support the increased connections.

在Mysql5.5+的版本中,这个值只有151,咱们能够通过show variables like "max_connections";查看本人的Mysql服务器最大连接数。依据以后的机器配置和理论状况,咱们能够拉大这个参数。

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

如何扩充max_connections这个值?(docker 中的Mysql为例)

上面是根底步骤。

0. 如何查看?

首先咱们须要理解如何查看以后的最大连接数。

show variables like "max_connections";

navicat 能够在命令行模式下查看查看连接数(这里为集体试验批改后后果):

有两种办法来减少MySQL连贯。上面是通过MySQL命令行工具更新最大连接数为200的命令,无需重新启动。

1. 长期批改:命令

通过Mysql连贯到客户端,能够应用上面的命令批改最大连接数:

mysql> set global max_connections = 200;

然而须要留神这种批改形式一旦重启就会生效,所以这个命令通常是在呈现相似的生产事变的时候救急应用。

如果想要永恒失效,须要批改mysql的相干配置文件。

2. 永恒失效:系统配置批改

要永恒失效须要批改系统配置,

sudo vi /etc/my.cnf

须要留神依据Linux发行版和装置类型不同,my.cnf文件可能位于以下任何地位。如果是docker,则my.cnf 位于自定义映射的地位

  • /etc/my.cnf
  • /etc/mysql/my.cnf
  • $MYSQL_HOME/my.cnf
  • ~/.my.cnf

留神这个参数要配置到 [mysqld] 上面。

[mysqld]max_connections = 10024

最初只须要重启Mysql服务即可。

重启Mysql

集体搭建的Docker只须要重启镜像即可,较为不便。

[root@localhost conf]# docker restart 58f
docker 的 ID 反对含糊匹配。也能够自定义name之后进行重启。

写在最初

心愿上述教程能帮忙你理解如何减少MySQL的最大连接数。

Hopefully, the above tutorial will help you increase max connections in MySQL.