How to recover the mysql password when forgotten-windows/liunx version: mysql-8.0.27

created at 11-24-2021 views: 3

1 mysql password reset under windows

1.1 Stop the mysql service (in the starting state)

net stop MySql

1.2 Create a new folder D/ under the disk mysql-init.txt, and put the following statement (including the new password: MyNewPass):

ALTER USER'root'@'localhost' IDENTIFIED BY'MyNewPass';

1.3 Execute the initialization password statement (executed in the bin/ directory of mysql):

mysqld --init-file=D:\\mysql-init.txt

Note: stuck during execution can be cancelled by Ctrl+C

1.4 Turn on the service and log in again and the password has been modified:

RESTART MYSQL AND LOGIN

2 mysql password reset under liunx:

2.1 Stop the mysql service (in the starting state):

# Stop mysql service
service mysqld stop 

2.2 Write mysql configuration file

# Write mysql configuration file, add skip-grant-tables to skip verification
vim /etc/my.cnf

2.3 Start the mysql service:

# Start mysql service
service mysqld start 

2.4 Login again, skip verification

# Skip verification
mysql -u root 

2.5 The mysql client tells the server to reload the authorization table so that the account management statement works

mysql> FLUSH PRIVILEGES;

2.6 Change the password of the root'@'localhost account. Replace the password with the password you want to use

mysql> ALTER USER'root'@'localhost' IDENTIFIED BY'Mrwhite@2021';

2.7 Exit, restore mysql configuration file, comment skip-grant-tables

mysql> exit
Bye
[root@localhost ~]# vim /etc/my.cnf

skip-grant-tables

2.8 Restart the mysql service. At this time, use the modified password to log in successfully

vim /etc/my.cnf
service mysqld restart

RESTART MYSQL AND LOGIN

created at:11-24-2021
edited at: 11-24-2021: