登录MySQL
$mysql -u root -p
$password
登录失败
- 修改
/etc/my.cnf
,在mysqld
小节下添加一行:skip-grant-tables=1
这一行配置让mysqld
启动时不对密码进行验证 - 重启
mysqld
服务:$systemctl restart mysqld
- 使用 root 用户登录到 mysql:
$mysql -u root
- 切换到 mysql 数据库,更新 user 表:
update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';
在之前的版本中,密码字段的字段名是password
,5.7版本改成了authentication_string
- 退出
mysql
,编辑/etc/my.cnf
文件,删除skip-grant-tables=1
的内容 - 重启
mysqld
服务,再用新密码登录即可