暂无图片
暂无图片
3
暂无图片
暂无图片
暂无图片

诊断MySQL8.0.36不同的启动方式导致参数不一致问题原因

601

1、问题显示

  当使用 sytemctl 命令重启MySQL8.0.36数据库的时候,数据库的部分参数老是和参数文件里边的不一样,主要是这三个参数max_open_files(1024)、max_connections(214)和table_open_cache(400),都会变成默认参数。

  如果是通过mysql用户使用mysql_safe来进行启动的话,那么数据库本身的参数就是正确的。不会让数据库自动改成默认,

  这个需要进行分析报错,要不根据systemctl自动启动的数据库进程就是白搭,还不如手动启动。

2、分析问题

2.1、通过systemctl启动的数据库会有警告

[Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 15000)
[Warning] [MY-010141] [Server] Changed limits: max_connections: 214 (requested 3000)
[Warning] [MY-010142] [Server] Changed limits: table_open_cache: 400 (requested 4000)

2.2、通过数据库工具登录查询以上数据库参数

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

mysql> show global variables like 'table_open_cache';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| table_open_cache | 400   |
+------------------+-------+
1 row in set (0.01 sec)

mysql> show variables like '%open_files%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| innodb_open_files | 400   |
| open_files_limit  | 1024  |
+-------------------+-------+
2 rows in set (0.01 sec)


2.3、查看参数文件信息

[mysql@db-mysql-03 ~]$ cat /mysql/conf/my3306.cnf | grep table_open_cache
max_connections = 3000

[mysql@db-mysql-03 ~]$ cat /mysql/conf/my3306.cnf | grep table_open_cache
table_open_cache = 4000

[mysql@db-mysql-03 ~]$ cat /mysql/conf/my3306.cnf | grep open_files
innodb_open_files = 15000
open_files_limit = 65536


2.4、查看systemctl配置的文件

[root@node1 system]# vim /usr/lib/systemd/system/mysqld.service

[Unit]
Description = MySQL Server
Documentation = man:mysqld(8)
Documentation = http://dev.mysql.com/doc/refman/en/using-systemd.html
After = network.target
After = syslog.target
[Install]
WantedBy = multi-user.target
[Service]
User=mysql
Group=mysql
Type=notify
Timeout=0
ExecStart=/mysql/app/mysql8.0.36/bin/mysqld --defaults-file=/mysql/conf/my3306.cnf $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILEE=65536
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false

主要看ExecStart 、 EnvironmentFile 和 LimitNOFILEE。

ExecStart是执行的命令。

EnvironmentFile就是上边的 $MYSQLD_OPTS,其中这个 这里的 =- 表示:可选前缀"-",表示如果文件不存在,则不会读取它,并且不会记录错误或警告消息。

LimitNOFILEE=65536 是设置的资源限制。

(其实这里有个很明显的参数写错了,但是没报错所以没注意到)


2.5、求助场外帮助

求助大佬场外帮助,所有的答案都指向一个,就是服务器用户资源设置的有问题。

而且systemctl和mysql_safe分别是root用户和mysql用户执行启动的。

##查看现在默认参数
ulimit -n
1024

##修改用户资源限制参数
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536

##这种启动不是后台进程,所以需要新开界面进行查看各项日志和参数
/mysql/app/mysql8.0.36/bin/mysqld --defaults-file=/mysql/conf/my3306.cnf

检查过后,后台报警日志无报警,数据库参数正常不是默认
也就是说这样成功了。


当时当我还是尝试使用systemctl重启数据库的时候,还是会报错,但是报错内容变了。

[Warning] [MY-010140] [Server] Could not increase number of max_open_files to more than 1024 (request: 65536)
[Warning] [MY-010141] [Server] Changed limits: max_connections: 214 (requested 3000)
[Warning] [MY-010142] [Server] Changed limits: table_open_cache: 400 (requested 4000)

根据第一行的报错百度了一下,都指向了 mysqld.service的LimitNOFILEE参数,比对了一下网上的内容和《MYSQL实战》,发现原来是参数写错了,多写了一个E。


修改参数文件

vim /usr/lib/systemd/system/mysqld.service
LimitNOFILE=65536


重启数据库生效

systemctl daemon-reload
systemctl restart mysqld



3、问题原理

大佬说看一眼官网解释,还给截了图才发现具体情况。


参数配置官方网站

https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html

MySQL 8.0 Reference Manual —— MySQL Server Admininistration —— Server System Variables


open_files_limit

On Unix, the value cannot be set greater than the value displayed by the ulimit -n command. On Linux systems using systemd, the value cannot be set greater than LimitNOFILE (this is DefaultLimitNOFILE, if LimitNOFILE is not set); otherwise, on Linux, the value of open_files_limit cannot exceed ulimit -n.

在Unix操作系统下,该值不能大于ulimit -n命令显示的值。在使用systemd的Linux系统上,该值不能大于LimitNOFILE(如果没有设置LimitNOFILE,则为DefaultLimitNOFILE);否则,在Linux操作系统下,open_files_limit 的值不能超过ulimit -n。


max_connections

The maximum permitted number of simultaneous client connections. The maximum effective value is the lesser of the effective value of open_files_limit - 810, and the value actually set for max_connections.

允许的最大客户端同时连接数。最大有效值是open_files_limit - 810有效值和max_connections实际设置的值之间的小者。


table_open_cache

The number of open tables for all threads. Increasing this value increases the number of file descriptors that mysqld requires. The effective value of this variable is the greater of the effective value of open_files_limit - 10 - the effective value of max_connections / 2, and 400; that is

所有线程打开的表的数量。增加这个值会增加mysqld所需的文件描述符的数量。该变量的有效值为open_files_limit的有效值(10)、max_connections / 2的有效值和400的有效值中较大的一个;这是

MAX((open_files_limit - 10 - max_connections) / 2,400)


计算一下就是,服务器资源限制打开文件的默认值(因为没有设置或者设置错误)就是 1024

数据库 open_files_limit = 服务器 ulimit -n = 1024

max_connections = open_files_limit(1024) - 810 = 214(或者实际设置的有效值)

table_open_cache = MAX((open_files_limit - 10 - max_connections) / 2,400) =400(意思是得出的值,如果比400大,就用大的,如果比400小,就用400.)




「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论