Linux server forgot MySQL root login password solution

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

1. First, verify that the server is in a secure state, that is, no one can arbitrarily connect to the MySQL database. 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. The safest state is to operate on the console of the server and unplug the network cable.

2. To modify MySQL login settings:
# 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]
Datadir=/var/lib/mysql
Socket=/var/lib/mysql/mysql.sock
Skip-grant-tables

Save and Exit VI.

3. Reboot mysqld

The code is as follows Copy Code
#/etc/init.d/mysqld Restart
stopping MySQL: [OK]
Starting MySQL: [OK]

4. Log in and modify the MySQL root password

  code is as follows copy code

#/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. 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 Quicke R startup with-a

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;
Query OK, 0 rows affected (0.01 sec)

Mysql> quit
Bye

5. Change the MySQL login settings back
# VI/ETC/MY.CNF
Delete the skip-grant-tables that you just added in the [mysqld] section
Save and Exit VI.

6. Reboot mysqld

The code is as follows Copy Code
#/etc/init.d/mysqld Restart
stopping MySQL: [OK]
Starting MySQL: [OK]

7. Restore the server's normal working status
Reverse the operation in step one. Restores the server's working status.

Two. MySQL password recovery method bis
If you forget the MySQL root password, you can reset it in the following ways:
1. Kill off the MySQL process in the system;

The code is as follows Copy Code
Killall-term mysqld

2. Use the following command to start MySQL, to do not check the permissions of the way to start;

The code is as follows Copy Code
Safe_mysqld--skip-grant-tables &

3. Then use the null password method to log in to MySQL with root user;

The code is as follows Copy Code
Mysql-u Root

4. Change the password of root user;

The code is as follows Copy Code
mysql> Update Mysql.user Set Password=password (' New password ') where user= ' root ';
mysql> flush Privileges;
Mysql> quit

Restart MySQL and you can log on with the new password.


Three. mysql Password recovery method three
It is possible that your system does not have SAFE_MYSQLD programs (such as I now use the Ubuntu operating system, Apt-get installed MySQL), the following methods can be restored
1. Stop mysqld;

The code is as follows Copy Code
/etc/init.d/mysql stop

(You may have other methods, in short stop the mysqld run on it)
2. Use the following command to start MySQL, to do not check the permissions of the way to start;

The code is as follows Copy Code
Mysqld--skip-grant-tables &

3. Then use the null password method to log in to MySQL with root user;

The code is as follows Copy Code
Mysql-u Root

4. Change the password of root user;

The code is as follows Copy Code
mysql> Update Mysql.user Set Password=password (' NewPassword ') where user= ' root ';
mysql> flush Privileges;
Mysql> quit

Restart MySQL

The code is as follows Copy Code
/etc/init.d/mysql restart

You can log on using the new password NewPassword.

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.