Read Catalogue
- Workaround One
- Workaround Two
When you forget the MySQL password, we try to log in and report the following Error
[Email protected] ~]# mysql-u root-penter password:error 1045 (28000): Access denied for user ' root ' @ ' localhost ' (using Password:no)
Workaround One
[[Email protected] ~]# service mysqld stopstopping mysqld: [ OK ]
- Edit/ETC/MY.CNF Add a parameter under the [Mysqld] field skip-grant
[Email protected] ~]# vim/etc/my.cnf[mysqld]skip-grant datadir=/var/lib/mysqlsocket=/var/lib/mysql/ mysql.sockuser=mysql# disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld _safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
[[Email protected] ~]# service mysqld restartstopping mysqld: [ OK ]starting mysqld: [ OK ]
[Email protected] ~]# mysql-u rootwelcome to the MySQL monitor. Commands End With; or \g.your MySQL connection ID is 2Server version:5.1.73 Source distributioncopyright (c), +, Oracle and/or its a Ffiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names trademarks of their respectiveowners. Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.mysql>
mysql> use MySQL; Enter MySQL database reading table information for completion of table and column namesyou can turn off this feature to get a quick ER startup with-adatabase changedmysql> update user set Password=password (' 123456 ') where user= ' root '; Update root password is 123456Query OK, 3 Rows Affected (0.00 sec) rows Matched:3 changed:3 warnings:0 mysql> flush Privi Leges; Re-swipe the permissions table to memory query OK, 0 rows Affected (0.00 sec)
- Edit/etc/my.cnf Remove the parameter skip-grant in the [mysqld] field and restart the MySQL service
[Email protected] ~]# vim/etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/ Log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
[[Email protected] ~]# service mysqld restartstopping mysqld: [ OK ]starting mysqld: [ OK ]
Workaround Two
[[Email protected] ~]# service mysqld stopstopping mysqld: [ OK ]
- Input command: Mysqld_safe--skip-grant-tables &
[Email protected] ~]# Mysqld_safe--skip-grant-tables &
[Email protected] ~]# mysql-u rootwelcome to the MySQL monitor.
mysql> use MySQL; Enter MySQL database reading table information for completion of table and column namesyou can turn off this feature to get a quick ER startup with-adatabase changedmysql> update user set Password=password (' 123456 ') where user= ' root '; Update root password is 123456Query OK, 3 Rows Affected (0.00 sec) rows Matched:3 changed:3 warnings:0 mysql> flush Privi Leges; Re-swipe the permissions table to memory query OK, 0 rows Affected (0.00 sec)
[[Email protected] ~]# service mysqld restart150906 10:21:51 mysqld_safe mysqld from PID file/var/run/mysqld/mysqld.pid E Ndedstopping mysqld: [ OK ]starting mysqld: [ OK ]
All right, it's done! The two solutions above are more convenient, but the principle is the same.
Again, there are some things you should be aware of when doing this to restore your password:
- First verify that the server is in a secure state, that is, no one can connect to the MySQL database arbitrarily, because during the reset of the MySQL root password, the MySQL database is completely out of password-protected state, other users can also arbitrarily login and modify the MySQL information
- You can implement the server's quasi-security status by enclosing MySQL ports externally and stopping Apache and all user processes! The safest state is to operate on the console of the server, and unplug the network cable for operation
- After updating the password, be sure to delete the parameter under the [Mysqld] field skip-grant
Forget the root password of Mysql