mysql重啟后報錯
信息如下
###################################################
Redirecting to /bin/systemctl start mysql.service
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
##################################################
查看服務運行信息
systemctl status mysqld.service
##################################################
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; generated)
Active: failed (thawing) (Result: exit-code) since Thu 2021-04-08 23:22:55 PDT; 6min ago
Docs: man:systemd-sysv-generator(8)
Process: 13546 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Process: 33545 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=1/FAILURE)
Apr 08 23:22:54 primary systemd[1]: Starting LSB: start and stop MySQL...
Apr 08 23:22:55 primary mysqld[33545]: Starting MySQL. ERROR! The server quit without updating PID file (/var/run/mysqld/mysqld.pid).
Apr 08 23:22:55 primary systemd[1]: mysqld.service: Control process exited, code=exited status=1
Apr 08 23:22:55 primary systemd[1]: mysqld.service: Failed with result 'exit-code'.
Apr 08 23:22:55 primary systemd[1]: Failed to start LSB: start and stop MySQL.
##################################################
情形一.異常數據文件影響服務啟動
截取錯誤日志/var/log/mysqld.log
##################################################
2021-04-09T06:38:24.889010Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-09T06:38:24.889074Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2021-04-09T06:38:24.889090Z 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.23-log) starting as process 35083 ...
mysqld: File '/usr/local/mysql/bin-log/master-bin.index' not found (Errcode: 21 - Is a directory)
2021-04-09T06:38:24.895144Z 0 [ERROR] Aborting
2021-04-09T06:38:24.895151Z 0 [Note] Binlog end
2021-04-09T06:38:24.895251Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
##################################################
日志說未找到/usr/local/mysql/bin-log/master-bin.index文件
遷移或清空對應路徑下文件master-bin.index后恢復
mv master-bin.index master-bin.index.bak
再次重啟后成功
具體什么原因最好的辦法是先查看下錯誤日志:
1、可能是/usr/local/mysql/data/mysql.pid文件沒有寫的權限
解決方法 :給予權限,執行 “chown -R mysql:mysql /var/data” “chmod -R 755 /usr/local/mysql/data” 然后重新啟動mysqld!
2、可能進程里已經存在mysql進程
解決方法:用命令“ps -ef|grep mysqld”查看是否有mysqld進程,如果有使用“kill -9 進程號”殺死,然后重新啟動mysqld!
3、可能是第二次在機器上安裝mysql,有殘余數據影響了服務的啟動。
解決方法:去mysql的數據目錄/data看看,如果存在mysql-bin.index,就趕快把它刪除掉吧,它就是罪魁禍首了。本人就是使用第三條方法解決的 !
4、mysql在啟動時沒有指定配置文件時會使用/etc/my.cnf配置文件,請打開這個文件查看在[mysqld]節下有沒有指定數據目錄(datadir)。
解決方法:請在[mysqld]下設置這一行:datadir = /usr/local/mysql/data
5、skip-federated字段問題
解決方法:檢查一下/etc/my.cnf文件中有沒有沒被注釋掉的skip-federated字段,如果有就立即注釋掉吧。
6、錯誤日志目錄不存在
解決方法:使用“chown” “chmod”命令賦予mysql所有者及權限
7、selinux惹的禍,如果是centos系統,默認會開啟selinux
解決方法:關閉它,打開/etc/selinux/config,把SELINUX=enforcing改為SELINUX=disabled后存盤退出重啟機器試試。
以上摘抄https://blog.csdn.net/miss1181248983/article/details/82426283
情形二.非正常關機導致pid文件丟失
截取錯誤日志/var/log/mysqld.log
##################################################
2021-04-10T05:58:06.290754Z 0 [ERROR] InnoDB: Table mysql
.innodb_table_stats
not found.
2021-04-10T05:58:06.290791Z 0 [ERROR] InnoDB: Fetch of persistent statistics requested for table mysql
.gtid_executed
but the required system tables mysql.innodb_table_stats and mysql.innodb_index_stats are not present or have un
expected structure. Using transient stats instead.
2021-04-10T05:58:06.291796Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
2021-04-10T05:58:06.292262Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2021-04-10T05:58:06.292598Z 0 [Note] IPv6 is available.
2021-04-10T05:58:06.292609Z 0 [Note] - '::' resolves to '::';
2021-04-10T05:58:06.292634Z 0 [Note] Server socket created on IP: '::'.
2021-04-10T05:58:06.294492Z 0 [ERROR] Can't start server: can't check PID filepath: No such file or directory
##################################################
1.確認pid文件路徑
more /etc/my.cnf |grep -i pid
2.依照my.cnf文件配置,創建對應的文件並修改權限(root特權執行)
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
touch /var/run/mysqld/mysqld.pid
chown mysql:mysql /var/run/mysqld/mysqld.pid
3.再次嘗試重啟
情形三.如有用戶權限啟動問題,可嘗試如下修改
cp /etc/init.d/mysqld mysqld.bak
修改mysql啟動文件/etc/init.d/mysqld中start模塊
添加--user=root到mysqld_safe
\(bindir/mysqld_safe --user=root --datadir="\)datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
嘗試重啟