CentOS 7 安MySQL

我在部署 Django开发环境(linux) 遇到一个困境,现在安装 Django配置MySQL数据库 环境的 mysqlclient 始终编译报错。参考MariaDB官方文档 How to connect Python programs to MariaDB 看来官方已经改为 mariadb 作为Python的连接器。但是我现在不想改代码,所以我考虑重新安装原生的MySQL服务器来完成这个工作。

启动错误排查

我这里遇到一个启动失败的问题:

启动 mysqld 失败,执行 systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Wed 2023-09-20 15:05:06 CST; 9s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 39596 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=2)
  Process: 39565 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 39596 (code=exited, status=2)
   Status: "Server startup in progress"

Sep 20 15:05:05 haiguangxdn033142047230.stl systemd[1]: Failed to start MySQL Server.
Sep 20 15:05:05 haiguangxdn033142047230.stl systemd[1]: Unit mysqld.service entered failed state.
Sep 20 15:05:05 haiguangxdn033142047230.stl systemd[1]: mysqld.service failed.
Sep 20 15:05:06 haiguangxdn033142047230.stl systemd[1]: mysqld.service holdoff time over, scheduling restart.
Sep 20 15:05:06 haiguangxdn033142047230.stl systemd[1]: Stopped MySQL Server.
Sep 20 15:05:06 haiguangxdn033142047230.stl systemd[1]: start request repeated too quickly for mysqld.service
Sep 20 15:05:06 haiguangxdn033142047230.stl systemd[1]: Failed to start MySQL Server.
Sep 20 15:05:06 haiguangxdn033142047230.stl systemd[1]: Unit mysqld.service entered failed state.
Sep 20 15:05:06 haiguangxdn033142047230.stl systemd[1]: mysqld.service failed.
检查 /var/log/mysqld.log 可以看到由于 /var/lib/mysql 目录非空导致初始化失败
2023-09-20T07:10:24.426020Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.34) initializing of server in progress as process 45189
2023-09-20T07:10:24.427904Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2023-09-20T07:10:24.427928Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2023-09-20T07:10:24.427979Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-09-20T07:10:24.428277Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.34)  MySQL Community Server - GPL.
2023-09-20T07:10:24.665233Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.34) starting as process 45193
2023-09-20T07:10:24Z UTC - mysqld got signal 11 ;
Most likely, you have hit a bug, but this error can also be caused by malfunctioning hardware.
BuildID[sha1]=33c7e0aa240d478d45e6ee0a2d11828ba1deb657
Thread pointer: 0x0
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 0 thread_stack 0x100000
/usr/sbin/mysqld(my_print_stacktrace(unsigned char const*, unsigned long)+0x3d) [0x211745d]
/usr/sbin/mysqld(print_fatal_signal(int)+0x37f) [0xfd7ebf]
/usr/sbin/mysqld(handle_fatal_signal+0xa5) [0xfd7f75]
/lib64/libpthread.so.0(+0xf630) [0x7f5d7843e630]
/usr/sbin/mysqld(Delegate::Delegate(unsigned int)+0xaf) [0x1d9cb8f]
/usr/sbin/mysqld(delegates_init()+0x31) [0x1d9d511]
/usr/sbin/mysqld() [0xd5e534]
/usr/sbin/mysqld(mysqld_main(int, char**)+0x253c) [0xd66e9c]
/lib64/libc.so.6(__libc_start_main+0xf5) [0x7f5d76975555]
/usr/sbin/mysqld() [0xd48b14]
The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
information that should help you find out what is causing the crash.

我检查了一下目录,发现下面是一些证书:

/var/lib/mysql 目录下有一些证书文件导致 mysqld 初始化无法完成
total 32K
-rw------- 1 mysql mysql 1.7K Sep 20 15:24 ca-key.pem
-rw-r--r-- 1 mysql mysql 1.1K Sep 20 15:24 ca.pem
-rw-r--r-- 1 mysql mysql 1.1K Sep 20 15:24 client-cert.pem
-rw------- 1 mysql mysql 1.7K Sep 20 15:24 client-key.pem
-rw------- 1 mysql mysql 1.7K Sep 20 15:24 private_key.pem
-rw-r--r-- 1 mysql mysql  451 Sep 20 15:24 public_key.pem
-rw-r--r-- 1 mysql mysql 1.1K Sep 20 15:24 server-cert.pem
-rw------- 1 mysql mysql 1.7K Sep 20 15:24 server-key.pem

我之前安装过 MariaDB ,我尝试将 /var/lib/mysql 目录移除(会自动重建),但是依然是同样的报错

我仔细看了 /var/log/mysqld.log 日志,发现 mysqld got signal 11

搜索这个信号 11 表示: 遇到了一个bug。使用的mysql二进制文件或者它所使用的链接库之一已经损坏、构建不当或配置错误。这个错误也可能硬件故障引起,需要尝试手机一些错误诊断信息。由于这是一次崩溃并且肯定有问题,所以信息搜集过程也可能失败。

考虑到我使用的服务器是古老的CentOS 7.2(魔改aliOS),所以我推测是操作系统不兼容导致的问题。具体如何排查,在官方bug中提供了建议: 激活Audit日志来找出哪些查询触发了bug(不过我这里是启动问题,和官方文档中所说的运行时sql查询触发的bug不同)

备注

没有时间再折腾了,我最终决定还是将数据库切换到 MariaDB (Django也支持MariaDB 10.4以及更高版本)

参考