● mysqld.service - MySQL Server
Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2024-11-03 18:51:25 CST; 1min 33s ago
Process: 3071ExecStart=/usr/bin/mysqld --defaults-file=/etc/mysql/my.cnf --datadir=/var/lib/mysql --socket=/var/run/mysqld/mysqld.sock (code=exited, status=1/FAILURE)
Main PID: 3071(code=exited, status=1/FAILURE)
3. 尝试初始化MySQL
3.1 停止MySQL服务
systemctl stop mysqld
3.2 删除数据目录中的内容(谨慎操作!确保已备份数据)
sudorm -rf /var/lib/mysql/*
3.3 重新初始化 MySQL
sudo mysqld --initialize --user=mysql
3.4 启动 MySQL 服务
systemctl start mysqld
4. 尝试登录MySQL
[root@hcss-ecs-8875 shuaijie]# mysql -u root -p
Enter password:
4.1显示使用提供的密码尝试以root用户从localhost连接 MySQL 时被拒绝
ERROR 1045(28000): Access denied for user 'root'@'localhost'(using password: YES)
5. 重置密码
5.1 停止 MySQL 服务
systemctl stop mysqld
5.2 以特殊模式启动 MySQL,跳过权限表检查
mysqld --skip-grant-tables --user=mysql &
5.3 另开一个终端窗口,连接到 MySQL 而不需要密码
mysql -u root
5.4 更新密码
USE mysql;
UPDATE user SET authentication_string=PASSWORD('新密码') WHERE User='root';
FLUSH PRIVILEGES;
5.5 退出 MySQL 并停止特殊模式启动的 MySQL 进程
systemctl start mysqld
6. 正常启动 MySQL 服务
[root@hcss-ecs-8875 shuaijie]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44
Copyright (c)2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
mysql>