1.如果记得原密码,以原密码登录MySQL。否则需要先修改my.cnf文件,在mysqld模块中增加一行:skip-grant-tables,然后重启mysqld,并以空密码登录(直接回车)。
$ mysql -uroot -p
Enter password:
2.修改密码。
mysql> alter user root@'%' identified by [with mysql_native_password] 'Admin123!@#';
Query OK, 0 rows affected (0.02 sec)】
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
注意,这里可能需要一个高强度的密码,MySQL8默认的验证密码强度(validate_password_policy)为"MEDIUM",可通过下面语句查询当前密码强度:
mysql> show variables like "%validate_password%";
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | ON |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.11 sec)
// 如果要修改密码强度,可以执行下面语句
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.03 sec)
若以上不起作用,则可以先删除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> delete from user where user='root';
Query OK, 1 row affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> create user 'root'@'%' identified by 'Admin123!@#';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to root@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后重启mysqld,用root用户的新密码登录即可。