今天,启动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 2016-01-20 18:26:57 CST; 40s ago Docs: man:systemd-sysv-generator(8) Process: 2979 ExecStart=/etc/rc.d/init.d/MysqLd start (code=exited,status=1/FAILURE) Jan 20 18:26:56 spark01 systemd[1]: Starting SYSV: MysqL database server.... Jan 20 18:26:57 spark01 MysqLd[2979]: MysqL Daemon Failed to start. Jan 20 18:26:57 spark01 MysqLd[2979]: Starting MysqLd: [Failed] Jan 20 18:26:57 spark01 systemd[1]: MysqLd.service: control process exited,code=exited status=1 Jan 20 18:26:57 spark01 systemd[1]: Failed to start SYSV: MysqL database server.. Jan 20 18:26:57 spark01 systemd[1]: Unit MysqLd.service entered Failed state. Jan 20 18:26:57 spark01 systemd[1]: MysqLd.service Failed.
[root@spark01 ~]# journalctl -xe
-- -- Unit session-2.scope has begun starting up. Jan 20 18:26:48 spark01 sshd[2916]: pam_unix(sshd:session): session opened for user spark by (uid=0) Jan 20 18:26:52 spark01 su[2944]: (to root) spark on pts/1 Jan 20 18:26:52 spark01 su[2944]: pam_unix(su-l:session): session opened for user root by spark(uid=1000) Jan 20 18:26:56 spark01 polkitd[909]: Registered Authentication Agent for unix-process:2974:117137 (system bus name :1.25 Jan 20 18:26:56 spark01 systemd[1]: 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 20 18:26:57 spark01 MysqLd[2979]: MysqL Daemon Failed to start. Jan 20 18:26:57 spark01 MysqLd[2979]: Starting MysqLd: [Failed] Jan 20 18:26:57 spark01 systemd[1]: MysqLd.service: control process exited,code=exited status=1 Jan 20 18:26:57 spark01 systemd[1]: 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 20 18:26:57 spark01 systemd[1]: Unit MysqLd.service entered Failed state. Jan 20 18:26:57 spark01 systemd[1]: MysqLd.service Failed. Jan 20 18:26:57 spark01 polkitd[909]: Unregistered Authentication Agent for unix-process:2974:117137 (system bus name :1.
但,可惜的时,这些信息并不能提供服务启动失败的真正原因。
这时候,不妨打开MysqL的告警日志,毕竟,只要MysqL服务启动,告警日志都会有输出信息的,果然
2016-01-20T10:00:19.935771Z 0 [ERROR] /usr/sbin/MysqLd: Can't create/write to file '/var/run/MysqLd/MysqLd.pid' (Errcode: 2 - No such file or directory) 2016-01-20T10:00:19.935795Z 0 [ERROR] Can't start server: can't create PID file: No such file or directory 160120 18:00:20 MysqLd_safe MysqLd from pid file /var/run/MysqLd/MysqLd.pid ended
在终端看一下该目录是否存在,果然,不存在。
于是,创建了/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]
依旧报错,重新查看告警日志,有以下输出
2016-01-20T10:28:37.183387Z 0 [ERROR] /usr/sbin/MysqLd: Can't create/write to file '/var/run/MysqLd/MysqLd.pid' (Errcode: 13 - Permission denied) 2016-01-20T10:28:37.183431Z 0 [ERROR] Can't start server: can't create PID file: Permission denied 160120 18:28:37 MysqLd_safe MysqLd from pid file /var/run/MysqLd/MysqLd.pid ended 160120 18:32:06 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 2 root root 40 Jan 20 18:28 /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命令查看服务启动失败的原因往往并不如人意,反而给了一种错误的暗示,以为这个跟系统有关。其实,通过查看服务的日志,往往更能清晰的知道服务启动失败的原因。