Solution of root Password Forgetting in MySQL 5.7 and 8.0 Database

  • 2021-12-21 05:23:16
  • OfStack

Note: MySQL5.7 can crack root password, skip password authentication and log in to the database, and directly modify the password in the table, but MySQL 8.0 can't modify root password in this way. After logging in to the database, you need to skip password authentication and set root password to null before logging in to the database and modifying root password.

1. root password solution for forgetting MySQL 5.7 database


[root@mysql01 ~]# mysql --version    # Determine MySQL Version 
mysql Ver 14.14 Distrib 5.7.28, for linux-glibc2.12 (x86_64) using EditLine wrapper
[root@mysql01 ~]# vim /etc/my.cnf     # Edit Master Configuration File 
[mysqld]   # In mysqld Write the following under this line 
skip-grant-tables
      .................# Omit part of the content 
[root@mysql01 ~]# systemctl restart mysqld   # Restart MySQL Service to make the configuration file effective 
[root@mysql01 ~]# mysql -uroot      # Skip password verification and log in to the database directly 
# Modify root Password is pwd@123 And refresh permissions 
mysql> use mysql;
mysql> update user set authentication_string = passwoord('pwd@123') where user = 'root';
mysql> flush privileges;   # Refresh permissions 
mysql> exit
# Configure password authentication and log in with the new password 
[root@mysql01 ~]# vim /etc/my.cnf     # Edit Master Configuration File 
[mysqld] 
skip-grant-tables      # Delete this row 
[root@mysql01 ~]# systemctl restart mysqld     # Restart for changes to take effect 
# You can log in successfully with your new password 
[root@mysql01 ~]# mysql -uroot -ppwd@123   

2. root password solution for forgetting MySQL 8.0 database


[root@mysql01 ~]# mysql --version    # View MySQL Version 
mysql Ver 8.0.18 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
[root@mysql01 ~]# vim /etc/my.cnf     # Edit Master Configuration File 
[mysqld]   # In mysqld Write the following under this line 
skip-grant-tables
      .................# Omit part of the content 
[root@mysql01 ~]# systemctl restart mysqld   # Restart MySQL Service to make the configuration file effective 
[root@mysql01 ~]# mysql -uroot      # Skip password verification and log in to the database directly 
# Will root Password set to null 
mysql> use mysql
mysql> update user set authentication_string='' where user = 'root';
mysql> flush privileges;
mysql> exit
# Turn on password authentication and log in to the database again 
[root@mysql01 ~]# vim /etc/my.cnf     # Edit Master Configuration File 
[mysqld] 
skip-grant-tables      # Delete this row 
[root@mysql01 ~]# systemctl restart mysqld     # Restart for changes to take effect 
[root@mysql01 ~]# mysql -uroot      # Log in to the database directly 
mysql> alter user root@localhost identified by 'pwd@111';
mysql> flush privileges;
mysql> exit
# Use the new password for login test 
[root@mysql01 ~]# mysql -uroot -ppwd@111

Summarize


Related articles: