Mysql5.7 忘记密码怎么办?

在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码:
1、修改配置文件my.cnf 
      vim /etc/my.cnf    
      在[mysqld]中添加 skip-grant-tables

      保存退出。

2、重启mysql服务
     service mysqld restart

 3、用户登录
       mysql -uroot -p (直接点击回车,密码为空)
       use mysql;
       update user set password=password('root') where user='root';
       但是在5.7版本中不存在password字段,所有我们要用以下修改进行重置密码
       update user set authentication_string=password('123456') where user='root';
        执行
       flush privileges;
       quit;
4、将最开始修改的配置文件my.cnf中的skip-grant-tables删除
5、重启mysql
6、当你登陆mysql之后你会发现,当你执行命令时会出现
     ERROR 1820 (HY000): You must reset your password using ALTER USER statement;
     这是提示你需要修改密码
     SET PASSWORD = PASSWORD('123456');
      如果出现:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
      你需要执行两个参数来把mysql默认的密码强度的取消了才行
      set global validate_password_policy=0; set global validate_password_mixed_case_count=2;
      这时你再执行
      SET PASSWORD = PASSWORD('123456');
完美解决。

猜你喜欢

转载自blog.csdn.net/y534560449/article/details/79744959
今日推荐