美文网首页
忘记mysql 8 密码,怎么办

忘记mysql 8 密码,怎么办

作者: 青玉_f18c | 来源:发表于2022-03-17 11:08 被阅读0次
    • vim /etc/my.cnf
      注意不同的系统路径不太一样,也有可能是 /etc/my.cnf.d/mysql-server.cnf,根据自己的实际情况来
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    log-error=/var/log/mysql/mysqld.log
    pid-file=/run/mysqld/mysqld.pid
    # 但都是要在[myqld]这个节点下添加
    skip-grant-tables
    
    • 重启mysql服务:service mysqld restart
    • 免密进入mysql
    [root@centos8.2 dir]# mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 7
    Server version: 8.0.26 Source distribution
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> use mysql
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    
    
    • 查询root用户
    mysql> select user,authentication_string,host,plugin from user;
    
    image.png
    • 将root的密码置为空
    mysql> update user set authentication_string='' where user='root';
    Query OK, 1 row affected (0.02 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    • 然后将第一步文件中的skip-grant-tables去掉,重启mysql服务: service mysqld restart
    • 现在就可以了无密码进入msql了
    mysql  
    //或者  
    mysql -u root -p   
    password直接回车  
    
    • 设置mysql的新密码
    # 执行这个
    ALTER user 'root'@'localhost' IDENTIFIED with mysql_native_password BY '123456'
    # 如果上面的报错,改为
    ALTER user 'root'@'%' IDENTIFIED with mysql_native_password BY '123456'
    

    相关文章

      网友评论

          本文标题:忘记mysql 8 密码,怎么办

          本文链接:https://www.haomeiwen.com/subject/wtrvdrtx.html