How to enter the service and modify the MySQL forgot your login password

Frequently asked questions, what should I do if I

Insert picture description here


forgot my password when logging in to mysql
? Don't panic~
Solution:
Method 1: You can reset the MySQL root password by following the steps below;
1. First, ensure that the server is in a safe state, that is, no one can connect to the MySQL database arbitrarily.
Because when the MySQL root password is reset, the MySQL database is completely in a state of no password protection, and it can be used to log in and modify MySQL information at will, which is extremely insecure.
The quasi-safe state of the server can be achieved by closing the external port of MySQL and stopping Apache and all user processes.
The safest operation is to operate on the console of the server and unplug the network cable.

2. Modify MySQL login settings:
modify the Mysql configuration file

vim /etc/my.cnf
skip-name-resolve 
skip-grant-tables

Add in [mysqld]: skip-grant-tables

Insert picture description here


3. Then restart mysqld

/etc/init.d/mysqld restart

4. Enter mysql again

Insert picture description here


without a password
5. Then modify the root password

mysql> use mysql;
mysql> update user set password=PASSWORD('123456') where user='root';
Query OK, 0 rows affected (0.04 sec)
Rows matched: 4  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Insert picture description here


Insert picture description here


5. Then modify the configuration in mysql

vim /etc/my.cnf

Delete the two lines added

6. Then restart mysqld

/etc/init.d/mysqld restart
Insert picture description here


Successful login~

Insert picture description here


Method 2:
1. End the currently running MySQL process in the terminal.

sudo /etc/init.d/mysql stop

2. Run in mysql safe mode and skip permission verification.

sudo /usr/bin/mysqld_safe --skip-grant-tables

3. ctrl+T reopen a terminal and log in to mysql as root.

./mysql -u root

4. Modify the root user password.

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
mysql> update user set Password = PASSWORD('123456') where User ='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> exit
注意:括号里的'root'就是新密码。

5. End mysql safe mode and run mysql in normal mode.

sudo /etc/init.d/mysql restart

6. Try your newly modified password to log in to MySQL

./mysql -u root -p

Enter the password root

mysql> show grants for 'root'@'127.0.0.1'; 
mysql> flush privileges;
刷新账户后,退出。
mysql> quit;