- 打开mysql配置文件
vim /etc/my.cnf
,在mysqld模块下添加配置:
skip_grant_tables=1
- 重启mysql:
systemctl restart mysqld.service
- 这时登录mysql时不需要密码:
mysql -h127.0.0.1 -P3306 -uroot
- 修改密码:
use mysql;
update user set authentication_string = password('yourpassword') where user = 'root';
flush privileges;
- 退出mysql,将配置文件中的skip_grant_tables一行注释或删除,然后重启mysql,即可用新密码登录mysql了。