Recently used Aliyun server, due to the installation of LNMP installation package inadvertently, after installation forgot the MySQL root user password. Originally wanted to reinstall the server system, but because of the previous change system and installation environment spent a lot of time, also feel that there is no need to have any problems to reload the system. Because the server is installed Linux system, so only with the shell command to modify.
When you reset the MySQL user password, you first confirm that the server is in a safe state, that is, no one can connect to the MySQL database arbitrarily. Because the MySQL database is completely out of password protection during the reset of the MySQL root password, other users can also arbitrarily log in and modify MySQL information. The server's quasi-security status can be achieved by blocking MySQL's external ports and stopping Apache and all user processes.
1> stop MySQL.
The code is as follows |
Copy Code |
#/etc/init.d/mysqld Stop |
2> to modify MySQL login settings
The code is as follows |
Copy Code |
# VI/ETC/MY.CNF
|
In the paragraph of [mysqld], add a sentence: Skip-grant-tables
For example:
The code is as follows |
Copy Code |
[Mysqld] Port = 3306 Socket =/tmp/mysql.sock Skip-external-locking Skip-grant-tables |
Save and Exit VI.
3> Restart MySQL
The code is as follows |
Copy Code |
#/etc/init.d/mysqld Restart |
4> Login and modify the MySQL root password
The code is as follows |
Copy Code |
#/usr/bin/mysql mysql> use MySQL; mysql> UPDATE user SET Password = Password (' newpassword ') WHERE user = ' root '; mysql> flush Privileges; Mysql> quit;//www.111cn.net |
5> to change MySQL login settings back
The code is as follows |
Copy Code |
# VI/ETC/MY.CNF |
Delete the skip-grant-tables that you just added in the [mysqld] section
Save and Exit VI.
6> Restart MySQL
The code is as follows |
Copy Code |
#/etc/init.d/mysqld Restart |
example, a universal method
Optionally, on any platform, you can use the MySQL client to set up a new password (but the method is not secure):
Stop mysqld and restart it with the "–skip-grant-tables–user=root" option (Windows user can omit the –user=root section).
Connect to the MYSQLD server using the following command: Www.111Cn.net
The code is as follows |
Copy Code |
shell> Mysql-u Root
|
The following statement is issued on the MySQL client:
The code is as follows |
Copy Code |
mysql> UPDATE mysql.user SET password=password (' newpwd ') -> WHERE user= ' root '; mysql> FLUSH privileges;
|
Replace "Newpwd" with the actual root user password you intend to use.
You should be able to connect using the new password.