mysql 8.0修改root密码(忘记密码、Ubuntu 22.04)
修改MySQL的配置文件,在[mysqld]下添加一行
/etc/mysql/mysql.conf.d/mysqld.cnf
保存退出、重启服务
systemctl restart mysql.service
登录MySQL,此时不需要密码,直接回车
mysql -uroot -p
使用update语句清空authentication_string字段
use mysql;
update user set authentication_string='' where user='root';
exit;
删除在[mysqld]下添加skip-grant-tables
/etc/mysql/mysql.conf.d/mysqld.cnf
保存退出、重启服务
systemctl restart mysql.service
登录MySQL,此时不需要密码,直接回车
mysql -uroot -p
修改root密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root密码';
刷新权限
flush privileges;
exit;
用新的密码登录MySQL,查看是否修改成功
mysql -uroot -p