今天,启动MySQL服务器失败,如下所示:
[root@spark01 ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[FAILED]
根据提示,分别使用systemctl status mysqld.service和journalctl -xe查看服务启动失败的原因
[root@spark01 ~]# systemctl status mysqld.service
?.mysqld.service - SYSV: MySQL database server.
Loaded: loaded (/etc/rc.d/init.d/mysqld)
Active: failed (Result: exit-code) since Wed -- :: CST; 40s ago
Docs: man:systemd-sysv-generator()
Process: ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=/FAILURE) Jan :: spark01 systemd[]: Starting SYSV: MySQL database server....
Jan :: spark01 mysqld[]: MySQL Daemon failed to start.
Jan :: spark01 mysqld[]: Starting mysqld: [FAILED]
Jan :: spark01 systemd[]: mysqld.service: control process exited, code=exited status=
Jan :: spark01 systemd[]: Failed to start SYSV: MySQL database server..
Jan :: spark01 systemd[]: Unit mysqld.service entered failed state.
Jan :: spark01 systemd[]: mysqld.service failed.
[root@spark01 ~]# journalctl -xe
--
-- Unit session-.scope has begun starting up.
Jan :: spark01 sshd[]: pam_unix(sshd:session): session opened for user spark by (uid=)
Jan :: spark01 su[]: (to root) spark on pts/
Jan :: spark01 su[]: pam_unix(su-l:session): session opened for user root by spark(uid=)
Jan :: spark01 polkitd[]: Registered Authentication Agent for unix-process:: (system bus name :1.25
Jan :: spark01 systemd[]: Starting SYSV: MySQL database server....
-- Subject: Unit mysqld.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysqld.service has begun starting up.
Jan :: spark01 mysqld[]: MySQL Daemon failed to start.
Jan :: spark01 mysqld[]: Starting mysqld: [FAILED]
Jan :: spark01 systemd[]: mysqld.service: control process exited, code=exited status=
Jan :: spark01 systemd[]: Failed to start SYSV: MySQL database server..
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysqld.service has failed.
--
-- The result is failed.
Jan :: spark01 systemd[]: Unit mysqld.service entered failed state.
Jan :: spark01 systemd[]: mysqld.service failed.
Jan :: spark01 polkitd[]: Unregistered Authentication Agent for unix-process:: (system bus name :.
但,可惜的时,这些信息并不能提供服务启动失败的真正原因。
这时候,不妨打开MySQL的告警日志,毕竟,只要MySQL服务启动,告警日志都会有输出信息的,果然
--20T10::.935771Z [ERROR] /usr/sbin/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 2 - No such file or directory)
--20T10::.935795Z [ERROR] Can't start server: can't create PID file: No such file or directory
:: mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
MySQL服务在启动的时候,不能创建pid文件。
在终端看一下该目录是否存在,果然,不存在。
于是,创建了/var/run/mysqld/目录,重启MySQL服务
[root@spark01 ~]# mkdir -p /var/run/mysqld/
[root@spark01 ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[FAILED]
依旧报错,重新查看告警日志,有以下输出
--20T10::.183387Z [ERROR] /usr/sbin/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 13 - Permission denied)
--20T10::.183431Z [ERROR] Can't start server: can't create PID file: Permission denied
:: mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
:: mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
原来,/var/run/mysqld/的属主和属组还是root,mysql并不能在其中创建文件,后修改该目录的属主和属组,启动OK。
[root@spark01 ~]# ls -ld /var/run/mysqld/
drwxr-xr-x root root Jan : /var/run/mysqld/
[root@spark01 ~]# chown mysql.mysql /var/run/mysqld/
[root@spark01 ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl): [ OK ]
总结:
以前在玩Kubernetes的时候,常遇到启动失败的情况,根据systemctl的提示,通过systemctl status mysqld.service和journalctl -xe命令查看服务启动失败的原因往往并不如人意,反而给了一种错误的暗示,以为这个跟系统有关。其实,通过查看服务的日志,往往更能清晰的知道服务启动失败的原因。