Mysql报Too many connections,不要乱用ulimit了,看看如何正确修改进程的最大文件数 (3)

此时再去查看:

[root@localhost ~]# netstat -nltp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 7007/nc tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6632/sshd tcp6 0 0 :::3306 :::* LISTEN 6670/mysqld tcp6 0 0 :::1234 :::* LISTEN 7007/nc tcp6 0 0 :::22 :::* LISTEN 6632/sshd [root@localhost ~]# cat /proc/7007/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 3795 3795 processes --------------------------- 1 Max open files 65535 65535 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes

注意,这里1处显示,已经变成了65535.

注意这个的限制,是设置了该值后,在此之后启动的进程才有用;而且对于开机自启的进程,应该是没什么用的。

我不建议这种方式。

ulimit [-HSTabcdefilmnpqrstuvx [limit]] Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.

如果要长久生效,可以这样:

echo ulimit -SHn 65535 >> /etc/profile

但是,这个是只针对由该shell启动的进程。比如开机启动的那些,比如mysql,应该是没法用这个控制的。

当mysql使用systemd方式开机自启时,怎么正确修改

我们这边的mysql,是使用rpm安装的,安装的时候,就用了如下命令:

sed -i '/\# End of file/i * soft nofile 65535' /etc/security/limits.conf sed -i '/\# End of file/i * hard nofile 65535' /etc/security/limits.conf echo ulimit -SHn 65535 >> /etc/profile source /etc/profile

同时,也修改了其配置文件:

/etc/my.cnf [root@localhost CAD_OneKeyDeploy]# vim /etc/my.cnf [mysqld] datadir = /var/lib/mysql socket = /var/lib/mysql/mysql.sock max_connections=10000 symbolic-links = 0

比如这里的max_connections=10000。

但是,开机重启后,使用:

[root@localhost CAD_OneKeyDeploy]# cat /proc/6677/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 3795 3795 processes // 1------------------ Max open files 5000 5000 files Max locked memory 65536 65536 bytes

上面1处,最大打开文件是5000,这是为啥呢?说明没效果啊。

为啥呢,找了半天,发现我们的mysql是通过systemd方式启动的。

[root@localhost CAD_OneKeyDeploy]# systemctl status mysqld ● mysqld.service - MySQL Server // 1 Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2020-07-18 10:58:07 CST; 44min ago Docs: man:mysqld(8) Process: 6674 ExecStart=http://www.likecs.com/usr/sbin/mysqld --daemonize --pid-file=http://www.likecs.com/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 6636 ExecStartPre=http://www.likecs.com/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 6677 (mysqld) CGroup: /system.slice/mysqld.service └─6677 /usr/sbin/mysqld --daemonize --pid-file=http://www.likecs.com/var/run/mysqld/mysqld.pid Jul 18 10:58:05 localhost.localdomain systemd[1]: Starting MySQL Server... Jul 18 10:58:07 localhost.localdomain systemd[1]: Started MySQL Server.

上面1处,指定了该service的位置:

/usr/lib/systemd/system/mysqld.service.

我们打开该文件看一下:

... [Service] User=mysql Group=mysql Type=forking PIDFile=http://www.likecs.com/var/run/mysqld/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables ExecStartPre=http://www.likecs.com/usr/bin/mysqld_pre_systemd # Start main service ExecStart=http://www.likecs.com/usr/sbin/mysqld --daemonize --pid-file=http://www.likecs.com/var/run/mysqld/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql # 1 Sets open_files_limit LimitNOFILE = 5000

注意这里的最后一行,

# 1 Sets open_files_limit LimitNOFILE = 5000

应该就是这个的问题了。

我这里改成10000,然后执行:

systemctl daemon-reload

然后重启mysql:

systemctl restart mysqld

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzggp.html