MySql8.0修改root密码

发布时间 2023-04-11 09:54:18作者: ryan刘玮

MySQL 5.7 的版本,因为在user表中没有password字段,一直使用下边的方式来修改root密码

use mysql;

update user set authentication_string=password('root')  where user='root'

现在要用MySQL8.0.11版本,装好MySQL后用上边方法修改密码,一直报错。后来去掉password()函数后,没有报错,但是输入密码时不对。

查阅后才知道在mysql 5.7.9以后废弃了password字段和password()函数;authentication_string:字段表示用户密码,而authentication_string字段下只能是mysql加密后的41位字符串密码。所以需要用以下方式来修改root密码:先检查authentication_string是否为空
1、如果不为空 

update user set authentication_string=''  where user='root'    --将字段设置为空

alter user 'root'@'localhost' identified by 'root';                       --修改密码为root  如果为空,直接执行alter user 'root'@'localhost' identified by 'root'; 

如果出现如下错误

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

需要执行   flush privileges; 再执行alter user 'root'@'localhost' identified by 'root';