CentOS 7下mysqld服务启动失败终极解决方案

时间:2024-01-22 10:20:48

启动mysqld服务失败,报错如下:

[root@LCM01GCM01 ~]# service 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]

既然有了提示,先看下有什么帮助没有

[root@LCM01GCM01 ~]# systemctl status mysqld.service
 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@LCM01GCM01 ~]# 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自带的告警日志,路径一般在/var/log/mysqld.log下面

[root@LCM01GCM01 ~]# tail -200f  /var/log/mysqld.log
2018-12-07 T10: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)
2018-12-07T10:00:19.935795Z 0 [ERROR] Can\'t start server: can\'t create PID file: No such file or directory
181207 18:00:20 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

这个时候我们就明白了,/var/run/mysqld/这个目录不存在,wu,查找下确实没有这个文件。这里我们有两种解决方案

第一种自己创建 /var/run/mysqld/ 这个目录,重新启动mysqld服务

[root@LCM01GCM01 ~]# mkdir -p /var/run/mysqld/
[root@LCM01GCM01 ~]# service 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]

依旧报错,重新查看告警日志,有以下输出

2018-12-070T10:28:37.183387Z 0 [ERROR] /usr/sbin/mysqld: Can\'t create/write to file \'/var/run/mysqld/mysqld.pid\' (Errcode: 13 - Permission denied)
2018-12-070T10:28:37.183431Z 0 [ERROR] Can\'t start server: can\'t create PID file: Permission denied
181207 18:28:37 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
181207 18:32:06 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

原来,/var/run/mysqld/的属主和属组还是root,mysql并不能在其中创建文件,后修改该目录的属主和属组,启动OK。

[root@LCM01GCM01 ~]# ls -ld /var/run/mysqld/
drwxr-xr-x 2 root root 40 Jan 20 18:28 /var/run/mysqld/
[root@LCM01GCM01 ~]# chown mysql.mysql /var/run/mysqld/
[root@LCM01GCM01~]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

上面第一种解决方案为临时性的方案,你会发现当虚拟机reboot的话,又会启动不成功,那个目录又不删除了,因此我们修改一下存放mysqld.pid的目录,有两个配置文件

vi  /etc/my.cnf

socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/createByhujian/mysqld/mysqld.pid
vi  /etc/init.d/mysqld

get_mysql_option mysqld datadir "/var/lib/mysql"
datadir="$result"
get_mysql_option mysqld socket "$datadir/mysql.sock"
socketfile="$result"
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"
errlogfile="$result"
get_mysql_option mysqld_safe pid-file "/createByhujian/mysqld/mysqld.pid"
mypidfile="$result"

红色的部分是我自己在根目录下创建的目录 /createByhujian/mysqld

mkdir -p /createByhujian/mysqld
chown mysql.mysql /createByhujian/mysqld
service mysqld start  

补充:可能会出现还是无法启动,查看日志

Can\'t start server: can\'t create PID file: Permission denied

给前面的一个目录增加权限

chmod +755  /createByhujian

重启mysqld服务成功,一劳永逸

有什么问题可以下方留言讨论哦!