mysql What if I forget my password

  • 2021-08-12 03:53:29
  • OfStack

Solution to MySQL forgetting password:


[root@localhost ~]# mysql -uroot -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

If this 1 appears, it is because MySQL did not rise


[root@localhost ~]# mysql -uroot -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

When this happens, it is because the password is incorrectly entered

Solution:

1. Stop MySQL


[root@localhost ~]# service mysqld stop
Stopping mysqld:                      [ OK ]

2. Modify the configuration file to add the following in the last line 1


skip-grant-tables  // Skip authorization and go directly to the database 

3. Restart MySQL


[root@localhost ~]# service mysqld start
[root@localhost ~]# mysql -uroot -p  
mysql> 

4. Re-password MySQL


mysql> use mysql // Passwords are stored in MySQL Need to enter 
mysql> desc user; // View user Fields in a table 
 Password 1 General existence password In the table 
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field     | Type        | Null | Key | Default    | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host     | char(60)       | NO | PRI |      |  |
| User     | char(16)       | NO | PRI |      |  |
| Password    | char(41)       | NO |  |      |  |

mysql> update user set password=password('passw0rd') where user="root"; // Set the password to passw0rd
mysql> flush privileges; // Refresh 


5. Then exit and re-log in


[root@localhost ~]# mysql -uroot -ppassw0rd
mysql>

Related articles: