CentOS7下mysql最大连接数设置 1040 too many connection

最近做项目经常遇到 too many connection 的错误,虽然之前都修改过不过经过centos的升级,又发生了一系列的变化。

一、首先用最常用的方式修改,打开mysql(mariadb)的配置文件

1
vim /etc/my.cnf

 

找到[mysqld]下面的参数,没有则添加。

1
2
3
wait_timeout=120
interactive_timeout=120
max_connections=4096

 

1
以下是my.cnf文件的信息
1
2
3
4
5
6
[mysqld]
...
wait_timeout=120
interactive_timeout=120
#set max number of connections
max_connections=4096

然后接着保存my.cnf,重启mysql

1
systemctl start mariadb

使用命令查看是否有改变,进入mysql

1
mysql -uroot -p

接着输入下面查看最大连接数。

1
2
3
4
5
6
7
8
MariaDB [(none)]> show variables like '%max_connections%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| extra_max_connections | 1     |
| max_connections       | 214  |
+-----------------------+-------+
2 rows in set (0.00 sec)

 你会发现还是没有改变,这就是centos7系统限制问题。

二、开始修改mariadb.service的文件

1
vim /usr/lib/systemd/system/mariadb.service

找到下面的参数,没有则添加。

1
2
3
[Service]
LimitNOFILE=65535
LimitNPROC=65535

重新加载服务配置文件,并且重启mysql服务。

1
2
systemctl daemon-reload
systemctl restart mariadb.service

  

这时候重复一的操作。

三、上面的操作都还不行,那就是centos7系统的本身限制

现在就是修改系统配置,查看系统限制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@xxx]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3885
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 65535
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

 打开配置文件limits.conf

1
vim /etc/security/limits.conf

找到配置资源限制参数nofile,没有文件尾部加入下面的参数配置。

1
2
3
4
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

 保存修改,重启centos服务器,这时候查看所有参数是否改变。

1
2
3
ulimit -a
#或者
#ulimit -n

 

如果还是没改变可以尝试 重启supervisord服务(如果属于这个子线程)。

以上就是我所遇到1040 too many connection 问题的解决,希望对大家有所帮助!

posted @   Auler  阅读(366)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示