忘记mysql 8 密码,怎么办

  • 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
[[email protected] 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 密码,怎么办)