Mysql8.0忘记密码

问题背景:

在ubuntu18上装完mysql8后,初始化时没有给随机密码,也无法设置密码;

第一步:修改/etc/my.cnf配置文件,在[mysqld]ui后加上如下语句:

skip-grant-tables

第二步免密登录到mysql上,

第三步:给root用户重置秘密;

3.1首先查看当前root用户相关信息,在mysql数据库的user表中

select host, user, authentication_string, plugin from user

host: 允许用户登录的ip‘位置’%表示可以远程;

user:当前数据库的用户名;

authentication_string: 用户密码;在mysql 5.7.9以后废弃了password字段和password()函数;

plugin: 密码加密方式;

3.2 如果当前root用户authentication_string字段下有内容,先将其设置为空;

    1. use mysql;  
    2.   update  user set authentication_string='' where user='root';
    3. update user set password_expired='N' where user='root';
    4.  flush privileges;
    5. exit;
    6. 3.3 退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务;
    7. service mysqld restart 
    8. 3.4 使用root用户进行登录,因为上面设置了authentication_string为空,所以可以免密码登录;
      1.   

  

3.5使用ALTER修改root用户密码

ALTER user 'root'@'localhost' IDENTIFIED BY 'Zhige123#';

至此修改成功; 从新使用用户名密码登录即可;

 

猜你喜欢

转载自www.cnblogs.com/new-journey/p/13163148.html