• notice
  • Congratulations on the launch of the Sought Tech site

Solution for forgotten root password for MySQL 5.7 and 8.0 database

Note: MySQL 5.7 cracks the root password, skips password authentication to log in to the database , and directly changes the password in the table, but MySQL 8.0 cannot change the root password in this way. The password is set to blank, and then you can log in to the database and change the root password.

1. Forgot the root password of the MySQL 5.7 database Solution Method 1 (recommended):

[root@mysql ~ ] # systemctl stop mysqld # Stop the mysql service [root@mysql ~ ] # mysqld --user=root --skip-grant-tables # Use the mysqld command to start the mysql service, skip the authorization table #Execute the above command After that, it will always occupy the current terminal, you need to open another terminal, #Don't think about running it in the background, put it in the background [root@mysql ~ ] # netstat -anput | grep 3306 # On opening A terminal, you can see that 3306 is listeningtcp6        0       0 :::3306 :::* LISTEN       3714/mysqld   
[root@mysql ~ ] # mysql -uroot # Log in to the database directly without passwordmysql > update mysql.user set authentication_string =password ( '1234' )               # #Change the root password to "1234"
   - > where user = 'root' and host = 'localhost' ;mysql > flush privileges ;            # Refresh privileges [root@mysql ~ ] # kill 3714 # Kill the terminal process number occupied when mysqld was started before, do not use the -9 option  [root@mysql ~ ] # systemctl start mysqld #Start MySQL service, just log in with the new password

If you use kill -9 to end the terminal occupied by mysqld in the above process, then restarting may report an error and the sock file is locked. At this time, you need to delete your mysql sock file. My sock file is in /tmp Below, the two files, mysql.sock.lock and mysql.sock, respectively, delete them and start MySQL again. Method 2:

[root@mysql ~ ] # mysql --version # Determine MySQL versionmysql Ver 14.14 Distrib 5.7.22, for linux-glibc2.12 (x86_64 ) using EditLine wrapper [root@mysql ~ ] # vim /etc/my.cnf 
[mysqld ]            # Write the following below this lineskip-grant-tables[root@mysql ~ ] # systemctl restart mysqld # Restart the service to make the configuration take effect [root@mysql ~ ] # mysql -u root # Skip password verification and log in directly to the database # Change the root password to pwd@123 and refresh the permissionsmysql > use mysqlmysql > update user set authentication_string = password ( 'pwd@123' ) where user = 'root' ;mysql > flush privileges ;
#Configure password authentication, log in with a new password [root@mysql1 ~ ] # vim /etc/my.cnf #Edit the main configuration file [mysqld ]skip-grant-tables             #Delete this line [root@mysql1 ~ ] # systemctl restart mysqld #Restart to make the changes take effect
#Use the new password to log in successfully [root@mysql1 ~ ] # mysql -uroot -ppwd@123       

2. The solution for forgetting the root password of the MySQL 8.0 database

[root@mysql1 ~ ] # mysql --version #View MySQL versionmysql Ver 8.0.18 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL ) [root@mysql1 ~ ] # vim /etc/my.cnf #Edit the main configuration file [mysqld ]       #Write under the line of mysqld the followingskip-grant-tables
           .. .. .. .. .. .. .. ... #Omit some content [root@mysql1 ~ ] # systemctl restart mysqld #Restart the MySQL service to make the configuration file take effect [root@mysql1 ~ ] # mysql -uroot #Skip password verification and log in directly to the database #Set the root password to emptymysql > use mysqlmysql > update user set authentication_string = '' where user = 'root' ;mysql > flush privileges ;mysql > exit
#Enable password verification and log in to the database again [root@mysql1 ~ ] # vim /etc/my.cnf #Edit the main configuration file [mysqld ]skip-grant-tables             #Delete this line [root@mysql1 ~ ] # systemctl restart mysqld #Restart to make changes take effect [root@mysql1 ~ ] # mysql -uroot #Log in to the database directlymysql > alter user root@localhost identified by 'pwd@111' ;mysql > flush privileges ;mysql > exit
#Login test with new password [root@mysql1 ~ ] # mysql -uroot -ppwd@111


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+