MySQL user password for reset server in Linux

Source: Internet
Author: User
Tags flush mysql client mysql login password protection

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.