当前位置: 代码网 > it编程>数据库>Mysql > MySQL重启之后无法写入数据的问题排查及解决

MySQL重启之后无法写入数据的问题排查及解决

2024年06月10日 Mysql 我要评论
背景客户在给系统打补丁之后需要重启服务器,数据库在重启之后,read_only 的设置与标准配置 文件中不一致,导致主库在启动之后无法按照预期写入。已知并没有外部程序会对数据库做只读的设置,那么会是哪

背景

客户在给系统打补丁之后需要重启服务器,数据库在重启之后,read_only 的设置与标准配置 文件中不一致,导致主库在启动之后无法按照预期写入。

已知并没有外部程序会对数据库做只读的设置,那么会是哪里出了问题?

排查过程

首先,检查启动进程配置文件的内容,是否的确正确配置了 read_only ,有没有在重启前后对文件进行修改:

# 检查配置文件的修改状态
[root@localhost ~]# stat /usr/local/mysql/etc/my.cnf
  file: ‘/usr/local/mysql/etc/my.cnf'
  size: 6160          blocks: 16         io block: 4096   regular file
device: fd00h/64768d    inode: 591296      links: 1
access: (0644/-rw-r--r--)  uid: (   27/   mysql)   gid: (   27/   mysql)
access: 2023-12-18 03:47:45.375190686 +0800
modify: 2022-08-01 23:25:34.861953062 +0800
change: 2022-08-01 23:25:34.862953087 +0800
 birth: -

# 查看配置文件内对read_only的设置
[root@localhost ~]# cat /usr/local/mysql/etc/my.cnf |grep read_only
[root@localhost ~]# 
[root@localhost ~]# 

已知数据库版本信息为 8.0.25 ,近期没有修改过配置文件,文件也没有对 read_only 进行配置,默认配置为 off :

其次,查看所有可能读取的配置文件,并逐个排查:

# 1. 查看 mysql 配置文件默认的加载顺序
[root@localhost ~]# mysql --verbose --help | grep -a 1 'default options'
default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

# 2. 依次查看可能会存在的配置文件,及其配置的 read_only 值
[root@localhost ~]# ll /etc/my.cnf
ls: cannot access /etc/my.cnf: no such file or directory
[root@localhost ~]# ll /etc/mysql/my.cnf
ls: cannot access /etc/mysql/my.cnf: no such file or directory

# /usr/local/mysql/etc/my.cnf 为本实例启动时的指定配置文件,配置见上文,此处略

[root@localhost ~]# ll ~/.my.cnf
-r-------- 1 root root 355 aug 19  2021 /root/.my.cnf
[root@localhost ~]# cat .my.cnf |grep read_only
[root@localhost ~]#

[root@localhost ~]# ll /home/mysql/.my.cnf
ls: cannot access /etc/my.cnf: no such file or directory

# 3. 通过检查服务器上可能存在的配置文件,发现 read_only 的设置在以上文件内并不存在

以上查看配置文件 的配置并没有找到相关配置,那么还有什么办法呢?尝试看看对数据库的历史操作记录,确认是否有用户对数据库做过 read_only 配置的操作:

# 通过 /root/.mysql_history ,看到这样的历史记录:
set persist_only read_only = 1;

全局模糊搜索配置文件,发现了 mysqld-auto.cnf 文件:

[root@localhost ~]# find / -name '*my*cnf'
/root/my.cnf
/root/.my.cnf
/usr/share/mysql/readme.mysql-cnf
/usr/share/mysql/my-huge.cnf
/usr/share/mysql/my-innodb-heavy-4g.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-medium.cnf
/usr/share/mysql/my-small.cnf
/usr/local/mysql/support-files/my.cnf
/usr/local/mysql/etc/my.cnf
/data/mysql/mysqld-auto.cnf

# 查看 mysqld-auto.cnf 文件内容,以及文件的操作时间
[root@localhost ~]# cat /data/mysql/mysqld-auto.cnf
{ "version" : 1 , "mysql_server" : { "read_only" : { "value" : "on" , "metadata" : { "timestamp" : 1659045255269856 , "user" : "root" , "host" : "localhost" } } } }

# 时间戳转换为北京时间 【1659045255269856】 -- >【2022-07-29 05:54:15】
# 查看 mysqld-auto.cnf 文件的状态
[root@localhost ~]# stat /data/mysql/mysqld-auto.cnf
  file: ‘/data/mysql/mysqld-auto.cnf'
  size: 164           blocks: 8          io block: 4096   regular file
device: fd08h/64776d    inode: 6291467     links: 1
access: (0640/-rw-r-----)  uid: (   27/   mysql)   gid: (   27/   mysql)
access: 2023-12-19 11:48:42.511662204 +0800
modify: 2022-07-29 05:54:15.269682214 +0800
change: 2022-07-29 05:54:15.269682214 +0800
 birth: -

这套数据库之前由别的团队管理,根据 mysql_history 的操作记录、前后带有时间记录的业务查询、以及 mysqld-auto.cnf 文件的生成时间,这些时间在我们接管之前,接管时仅检查了当时的数据库状态、my.cnf 文件中的配置,非常怀疑是这个操作导致了启动之后 read_only 被开起来,导致业务无法按照预期写入,接下来我们对这个参数进行测试。

参数测试

在主库设置参数。

# 配置文件检查
[root@localhost etc]# cat my.cnf |grep read_only
read_only = 0
super_read_only = 0

# 参数检查:
mysql> select @@read_only,@@super_read_only;
+-------------+-------------------+
| @@read_only | @@super_read_only |
+-------------+-------------------+
|           0 |                 0 |
+-------------+-------------------+
1 row in set (0.00 sec)


# 设置参数
mysql> set persist_only read_only = 1;
query ok, 0 rows affected (0.00 sec)

查看重启后的参数变化。

# 重启数据库
[root@localhost ~]# systemctl restart mysqld_3301

# 查看参数:
mysql> select @@read_only,@@super_read_only;
+-------------+-------------------+
| @@read_only | @@super_read_only |
+-------------+-------------------+
|           1 |                 0 |
+-------------+-------------------+
1 row in set (0.00 sec)

通过 strace 来查看数据库在启动时读取了哪些配置文件,以下是相关配置片段:

15:56:34.828260 stat("/opt/mysql/etc/3301/my.cnf", {st_mode=s_ifreg|0640, st_size=5042, ...}) = 0 <0.000008>
15:56:34.829061 stat("/opt/mysql/data/3301/mysqld-auto.cnf", {st_mode=s_ifreg|0640, st_size=164, ...}) = 0 <0.000006>
15:56:35.154154 stat("/opt/mysql/data/3301/auto.cnf", {st_mode=s_ifreg|0640, st_size=56, ...}) = 0 <0.000008>
15:56:35.154228 stat("/opt/mysql/data/3301/auto.cnf", {st_mode=s_ifreg|0640, st_size=56, ...}) = 0 <0.000007>
15:56:35.172411 stat("/opt/mysql/data/3301/auto.cnf", {st_mode=s_ifreg|0640, st_size=56, ...}) = 0 <0.000007>
15:56:35.172441 stat("/opt/mysql/data/3301/auto.cnf", {st_mode=s_ifreg|0640, st_size=56, ...}) = 0 <0.000007>
15:56:35.174142 stat("/opt/mysql/data/3301/mysqld-auto.cnf", {st_mode=s_ifreg|0640, st_size=164, ...}) = 0 <0.000007>
15:56:35.174172 stat("/opt/mysql/data/3301/mysqld-auto.cnf", {st_mode=s_ifreg|0640, st_size=164, ...}) = 0 <0.000007>
15:56:35.357608 stat("/opt/mysql/data/3301/auto.cnf", {st_mode=s_ifreg|0640, st_size=56, ...}) = 0 <0.000011>
15:56:35.357643 stat("/opt/mysql/data/3301/auto.cnf", {st_mode=s_ifreg|0640, st_size=56, ...}) = 0 <0.000010>
15:56:35.360019 stat("/opt/mysql/data/3301/mysqld-auto.cnf", {st_mode=s_ifreg|0640, st_size=164, ...}) = 0 <0.000009>
15:56:35.360052 stat("/opt/mysql/data/3301/mysqld-auto.cnf", {st_mode=s_ifreg|0640, st_size=164, ...}) = 0 <0.000008>

测试结论

根据以上排查过程和测试,数据库在启动时,读取完启动时指定的配置文件,会去读 mysqld-auto.cnf 这个文件,的确是这个参数设置导致 read_only 与标准配置文件预期不符的。

到此这篇关于mysql重启之后无法写入数据的问题排查及解决的文章就介绍到这了,更多相关mysql重启后无法写入数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com