错误信息:[root@VM_0_5_centos ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@VM_0_5_centos ~]# mysql -u root -p
解决方法:[root@VM_0_5_centos ~]# systemctl stop mysqld.service #关闭MySQL
[root@VM_0_5_centos ~]# echo "skip-grant-tables" >> /etc/my.cnf #启动mysql时不启动授权表
[root@VM_0_5_centos ~]# systemctl start mysqld.service #启动MySQL
[root@VM_0_5_centos ~]# mysql -uroot -p
Enter password: #直接回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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> use mysql; #进入MySQL库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
#修改密码(msyql5.7.9之后使用authentication_string加密,放弃了passwd字段和passwd()函数),本示例新密码为qingqing!
mysql> update user set authentication_string='qingqing!' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> exit #修改成功之后退出
Bye
[root@VM_0_5_centos ~]# sed -i '$d' /etc/my.cnf #删除追加的 Byeskip-grant-tables
[root@VM_0_5_centos ~]# mysql -uroot -p
Enter password: #输入新密码本示例新密码为qingqing!
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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.