【MySQL】MySQL免密登录(忘记root密码时)

很多时候,在数据库中忘记用户密码还能找上root账号修改,但一旦忘记了root密码时,那又该怎么办呢?
以下是需要修改root密码的过程步骤:
1、修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:

skip-grant-tables=1

这一行配置让 mysqld 启动时不对密码进行验证

2、重启 mysqld 服务:systemctl restart mysqld

3、使用 root 用户登录到 mysql:mysql -u root

4、切换到mysql数据库,更新 user 表:

update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';

在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

5、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容

6、重启 mysqld 服务,再用新密码登录即可

注意:skip-grant-tables参数是会让mysql在登录的时候跳过验证表直接登录到数据库,没特殊情况建议不要将该参数留在数据库配置文件中




原文地址:https://www.cnblogs.com/weifeng1463/p/15266333.html