CentOS7下MySQL服务启动失败原因及解决方法

时间:2023-03-09 03:37:49
CentOS7下MySQL服务启动失败原因及解决方法

在重启阿里的CentOS7服务器后,重启MySQL 出现错误

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@djaljdw ~]# systemctl status mysqld.service

● mysqld.service - SYSV: MySQL database server.
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu -- :: CST; 1min 25s ago
Docs: man:systemd-sysv-generator()
Process: ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=/FAILURE) Aug :: izw91diu854rguz systemd[]: Starting SYSV: MySQL database server....
Aug :: izw91diu854rguz mysqld[]: MySQL Daemon failed to start.
Aug :: izw91diu854rguz mysqld[]: Starting mysqld: [FAILED]
Aug :: izw91diu854rguz systemd[]: mysqld.service: control process exited, code=exited status=
Aug :: izw91diu854rguz systemd[]: Failed to start SYSV: MySQL database server..
Aug :: izw91diu854rguz systemd[]: Unit mysqld.service entered failed state.
Aug :: izw91diu854rguz systemd[]: mysqld.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

[root@djaljdw ~]#  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日志信息(/var/log/mysqld.log)

 :: [Note] Plugin 'FEDERATED' is disabled.
:: InnoDB: The InnoDB memory heap is disabled
:: InnoDB: Mutexes and rw_locks use GCC atomic builtins
:: InnoDB: Compressed tables use zlib 1.2.
:: InnoDB: Using Linux native AIO
:: InnoDB: Initializing buffer pool, size = 128.0M
:: InnoDB: Completed initialization of buffer pool
:: InnoDB: highest supported file format is Barracuda.
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
:: InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
:: InnoDB: Waiting for the background threads to start
:: InnoDB: 5.5. started; log sequence number
:: [Note] Server hostname (bind-address): '0.0.0.0'; port:
:: [Note] - '0.0.0.0' resolves to '0.0.0.0';
:: [Note] Server socket created on IP: '0.0.0.0'.
:: [ERROR] /usr/libexec/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 2)
:: [ERROR] Can't start server: can't create PID file: No such file or directory

问题找到了:不能创建PID文件:没有这样的文件或目录

于是去查看,果然没有文件夹

[root@djaljdw ~]#  cd /var/run
[root@djaljdw ~]#  mkdir mysqld
[root@djaljdw ~]#  cd mysqld
[root@djaljdw ~]#  touch mysqld.pid
重启服务还是报错
于是又查看日志
 :: [Note] Plugin 'FEDERATED' is disabled.
:: InnoDB: The InnoDB memory heap is disabled
:: InnoDB: Mutexes and rw_locks use GCC atomic builtins
:: InnoDB: Compressed tables use zlib 1.2.
:: InnoDB: Using Linux native AIO
:: InnoDB: Initializing buffer pool, size = 128.0M
:: InnoDB: Completed initialization of buffer pool
:: InnoDB: highest supported file format is Barracuda.
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
:: InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
:: InnoDB: Waiting for the background threads to start
:: InnoDB: 5.5. started; log sequence number
:: [Note] Server hostname (bind-address): '0.0.0.0'; port:
:: [Note] - '0.0.0.0' resolves to '0.0.0.0';
:: [Note] Server socket created on IP: '0.0.0.0'.
:: [ERROR] /usr/libexec/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 13)
:: [ERROR] Can't start server: can't create PID file: Permission denied

发现:无法创建PID文件:权限被拒绝

解决办法:
修改 /var/run/mysqld/权限为mysql 
[root@djaljdw ~]#  chown -R mysql /var/run/mysqld
[root@djaljdw ~]#  chgrp -R mysql /var/run/mysqld
[root@djaljdw ~]#  chmod 777 /var/run/mysqld

重启服务完美解决

tip:看日志是解决问题的一个很好的办法!