systemctl status mysqld.service或者systemctl start mysqld 启动失败的解决办法

报错过程:

mysql需要启动,执行一下命令启动mysql:

systemctl start  mysqld.service

结果出现如下提示

Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

在这里给大家一个提示,因为每个人的报错原因不同,我们需要去查看mysql的日志

vi /var/log/mysqld.log

如果mysql日志太多,可以通过dG将mysql的日志全部删除,之后再执行启动musql的命令

systemctl start  mysqld.service
vi /var/log/mysqld.log  // 查看报错日志

找到日志中出现error的地方,根据具体问题搜索具体博客的解决办法

我的日志文件中报错大致如下。

mysqld: Table 'mysql.plugin' doesn't exist
2021-04-27T08:55:46.468329Z 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
2021-04-27T08:55:46.468825Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-04-27T08:55:46.470106Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2021-04-27T08:55:46.470119Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2021-04-27T08:55:46.471231Z 0 [Warning] CA certificate ca.pem is self signed.
2021-04-27T08:55:46.471281Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2021-04-27T08:55:46.472264Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2021-04-27T08:55:46.472311Z 0 [Note] IPv6 is available.
2021-04-27T08:55:46.472322Z 0 [Note]   - '::' resolves to '::';
2021-04-27T08:55:46.472345Z 0 [Note] Server socket created on IP: '::'.
2021-04-27T08:55:46.472543Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2021-04-27T08:55:46.472624Z 0 [Note] InnoDB: Buffer pool(s) load completed at 210427 16:55:46
2021-04-27T08:55:46.476676Z 0 [Warning] Failed to open optimizer cost constant tables
 
2021-04-27T08:55:46.476791Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2021-04-27T08:55:46.476806Z 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
2021-04-27T08:55:46.476836Z 0 [ERROR] Aborting

从上面的报错可以看出,Mysql初始化出了问题。执行以下命令:

cd /   
rm -rf /var/lib/mysql/*
mysqld --initialize --user=mysql --datadir=/var/lib/mysql

之后重启mysql 并查看mysql的状态

systemctl restart mysqld
systemctl status mysqld

在这里插入图片描述
MySQL第一次启动后会创建超级管理员账号root@localhost,初始密码存储在日志文件中,查看密码

sudo grep 'temporary password' /var/log/mysqld.log

在这里插入图片描述
可以看到,我的初始密码为 iFqxsrSB&10o,准备登陆

mysql -uroot -p 

在这里插入图片描述
登陆成功后,修改密码,并开启访问权限

set global validate_password_length=4;      # 设置密码长度最低位数,适用于老版本
set global validate_password_policy=LOW;	# 设置密码安全等级低,便于密码可以修改成root,适用于老版本
set password=password('root');				# 设置密码为root

# 开启访问权限
grant all on *.* to 'root'@'%' identified by 'root';
flush privileges;

在这里插入图片描述
之后退出mysql,再次通过root密码登录mysql

mysql -uroot -proot

在这里插入图片描述
登陆成功!

猜你喜欢

转载自blog.csdn.net/qq_43316970/article/details/127440564