- 先退出数据库,用以下指令重启,跳过密码验证登录
service mysqld restart --skip-grant-tables;
- 直接登录,不需要密码
mysql
- 查询用户密码
# authentication_string:用户密码,5.7版本之前是 password 字段,5.7版本之后是authentication_string字段
SELECT host, user, authentication_string FROM mysql.user;
- 将root用户密码设为空值
UPDATE user SET authentication_string='' WHERE user='root';
- 重新加载授权列表
FLUSH PRIVILEGES;
- 替换root用户的密码
ALTER USER 'root'@'%' IDENTIFIED BY 'yourPassword';
- 重启mysql
mysql> FLUSH PRIVILEGES;
mysql> exit;
service mysqld restart;
- 然后mysql -uroot -p输入你修改的密码登录就行啦