The basic idea is to modify the MySQL configuration file, set it to MySQL without a password, restart MySQL, and then reset the root password. Finally, restore the original configuration and restart MySQL.
Http://blog.chinaunix.net/uid-26569333-id-3067038.html
1. First confirm that the server is in a secure state, that is, no one can connect to the MySQL database at will.
Because, during the resetting of the MySQL Root Password, the MySQL database is completely out of password-free
Other users can log on to and modify MySQL information as needed. You can use
The external port is closed, and Apache and all user processes are stopped to implement server quasi-security.
Status. The safest status is to operate on the server console and unplug the network cable.
2. Modify MySQL Logon Settings:
# Vi/etc/My. CNF
In the [mysqld] section, add skip-grant-tables.
For example:
[Mysqld]
######################################## ######################################## ###
# If forgot passwd, you can un-comment the following line, and restart MySQL.
# Then everyone can login to MySQL server without password with any user, you can
# Operate anything, receiveding Reset Password.
# It's very dangerous!
# Don't forget to comment the following line and restart mysql when everything done!
######################################## ######################################## ####
Skip-grant-tables
3. Restart mysqld
#/Etc/init. d/mysqld restart
Stopping MySQL: [OK]
Starting MySQL: [OK]
4. log on to and modify the MySQL root Password
#/Usr/bin/mysql
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 3 to server version: 3.23.56
Type 'help; 'or' \ H' for help. Type '\ C' to clear the buffer.
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-
Database changed
Mysql> UPDATE user SET Password = password ('new-password') WHERE User = 'root ';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
Mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
Mysql> quit
Bye
5. Modify the MySQL Logon Settings.
# Vi/etc/my. cnf
Delete the skip-grant-tables added to the [mysqld] section.
Save and exit vi.
6. Restart mysqld
#/Etc/init. d/mysqld restart
Stopping MySQL: [OK]
Starting MySQL: [OK]
Or simply:
1. Stop mysqld;
/Etc/init. d/mysql stop
(You may have other methods. Simply stop running mysqld)
2. Run the following command to start MySQL without checking its permissions;
Mysqld -- skip-grant-tables &
3. Use the empty password to log on to MySQL as the root user;
Mysql-u root
4. Modify the password of the root user;
Mysql> update mysql. user set password = PASSWORD ('newpassword') where User = 'root ';
Mysql> flush privileges;
Mysql> quit
Restart MySQL