问题现象
使用 rpm 包安装 MySQl 9.0.0 数据库后,缺省数据目录为/var/lib/mysql。关闭数据库,修改配置文件,把目录数据指向/data/data。但是在修改/etc/my.cnf后,数据库却无法正常启动。
/etc/my.cnf修改前:
datadir=/var/lib/mysql
/etc/my.cnf修改后:
datadir=/data/data
启动数据库异常
[root@mysql91 /]# systemctl restart mysqld
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
查看MySQL SERVER服务
[root@mysql91 /]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2024-07-03 18:54:31 CST; 8s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 6287 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
Process: 6257 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 6287 (code=exited, status=1/FAILURE)
Status: "Server shutdown complete (with return value = 1)"
Error: 13 (Permission denied)
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.170738Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 9.0.0) starting as process 6287
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.173249Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/data/mysqld_tmp_file_case_in>
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.173254Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for >
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.174158Z 0 [ERROR] [MY-010187] [Server] Could not open file '/data/data/mysqld.log' for error loggi>
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.174223Z 0 [ERROR] [MY-010119] [Server] Aborting
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.175280Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 9.0.0) MySQL >
Jul 03 18:54:31 mysql91 mysqld[6287]: 2024-07-03T10:54:31.175286Z 0 [System] [MY-015016] [Server] MySQL Server - end.
Jul 03 18:54:31 mysql91 systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
Jul 03 18:54:31 mysql91 systemd[1]: mysqld.service: Failed with result 'exit-code'.
Jul 03 18:54:31 mysql91 systemd[1]: Failed to start MySQL Server.
--journalctl -xe输出如下:
[root@mysql91 /]# journalctl -xe
If you want to allow mysqld to have write access on the data directory
Then you need to change the label on data
Do
# semanage fcontext -a -t FILE_TYPE 'data'
where FILE_TYPE is one of the following: faillog_t, krb5_host_rcache_t, mysqld_db_t, mysqld_log_t, mysqld_tmp_>
Then execute:
restorecon -v 'data'
***** Plugin catchall (17.1 confidence) suggests **************************
If you believe that mysqld should be allowed write access on the data directory by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'mysqld' --raw | audit2allow -M my-mysqld
# semodule -X 300 -i my-mysqld.pp
Jul 03 18:41:24 mysql91 setroubleshoot[5957]: AnalyzeThread.run(): Set alarm timeout to 10
Jul 03 18:41:34 mysql91 systemd[1]: setroubleshootd.service: Succeeded.
-- Subject: Unit succeeded
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- The unit setroubleshootd.service has successfully entered the 'dead' state.
解决办法
关闭SELINUX。
临时关闭
setenforce 0
永久关闭
vi /etc/selinux/config
修改SELINUX=disabled
再次启动 MySQL SERVER 服务
[root@mysql91 data]# systemctl restart mysqld
再次查看 MySQL SERVER 服务
[root@mysql91 data]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2024-07-03 19:00:04 CST; 8s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 7116 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 7146 (mysqld)
Status: "Server is operational"
Tasks: 35 (limit: 22864)
Memory: 443.0M
CGroup: /system.slice/mysqld.service
└─7146 /usr/sbin/mysqld
Jul 03 19:00:00 mysql91 systemd[1]: Starting MySQL Server...
Jul 03 19:00:04 mysql91 systemd[1]: Started MySQL Server.
[root@mysql91 data]#
总结
做技术工作一定要注意严谨,提前做好必要的环境检查配置工作,否则很简单的问题可能会浪费我们很长的时间去排查处理。谨记~