遗忘MySQL 8.0数据库的root密码解决办法
[root@mysql01 ~]# mysql --version #查看MySQL版本
mysql Ver 8.0.18 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
[root@mysql01 ~]# vim /etc/my.cnf #编辑主配置文件
[mysqld] #在mysqld这行下写入下面内容
skip-grant-tables
.................#省略部分内容
[root@mysql01 ~]# systemctl restart mysqld #重启MySQL服务,使配置文件生效
[root@mysql01 ~]# mysql -u root #跳过密码验证,直接登录数据库
将root密码设置
mysql> use 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
mysql> update user set authentication_string='' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
Bye
开启密码验证并重新登录数据库
[root@mysql01 ~]# vim /etc/my.cnf #编辑主配置文件
[mysqld]
skip-grant-tables #删除此行
[root@mysql01 ~]# systemctl restart mysqld #重启使更改生效
[root@mysql01 ~]# mysql -u root -p #直接登录数据库
mysql> alter user root@localhost identified by 'pwd@111';
mysql> flush privileges;
mysql> exit