How to change the root password without restarting Mysql

Source: Internet
Author: User
Tags localhost mysql

I. Generally, You need to restart Mysql if you forget the password.
1. skip-grant-tables
The common method is to use the skip-grant-tables option. After mysqld server is started, it does not use the permission system (privilege system ). The user does not need any account or any restrictions to access all data in the database. For the sake of security, skip-networking is usually added, and mysqld does not listen for any TCP/IP connection requests. The procedure is as follows,
1) modify the my. cnf configuration file and add skip-grant-tables and skip-networking to the mysqld option.
2) restart the mysqld server.
3) use an SQL statement to modify the password stored in the mysql. user table. Run flush privileges to re-enable the mysql permission system.
Copy codeThe Code is as follows: UPDATE mysql. user set Password = PASSWORD ('newpwd') WHERE User = 'root ';
Flush privileges;
4) delete or comment out the skip-grant-tables and skip-networking Parameter options in the configuration file. If skip-networking is used, restart mysqld again. Because skip-networking is not a system variable but a parameter option of mysqld, it cannot be dynamically set through system variables. If skip-networking is not applicable, you only need to execute flush privileges to make the permission system take effect again.
2. -- init-file
Mysqld_safe enables the-init-file parameter option to execute the SQL statement for resetting the password.
1) Create an initialization file, such as/tmp/initfile. The file contains the SQL statement used to change the password above.
Copy codeThe Code is as follows: UPDATE mysql. user SET Password = PASSWORD ('newpwd') WHERE User = 'root ';
Flush privileges;
2) disable the mysqld service process.
3) Start mysqld with mysqld_safe;
Copy codeThe Code is as follows: mysqld_safe -- init-file =/home/me/mysql-init &
The above two methods are to reset the password if you forget the root password, you can find that you need to restart the mysqld service. Many people use the first method to reset the root password, but the second method is recommended, that is, security is quick and simple.

2. Do not restart mysqld

1,First, you must have a mysql database account with the modification permission, the current mysql instance account (the account with lower permissions, such as the account with the test database can be modified), or other accounts of instances of the same version. Copy the files related to the user table under the data/mysql directory to the data/test directory.
Copy codeThe Code is as follows:
[Root @ localhost mysql] # cp mysql/user. * test/
[Root @ localhost mysql] # chown mysql. mysql test/user .*
2,Use another account with lower permissions to connect to the database and set the password data stored by the user in the test database.
Copy codeThe Code is as follows:
[Root @ localhost mysql] # mysql-utest-p12345
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000,201 1, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
Affiliates. Other names may be trademarks of their respective
Owners.

Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement.

Mysql> use test
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 ('ayun') where user = 'root ';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5 Changed: 0 Warnings: 0

Mysql>
3,Copy the modified user. MYD and user. MYI to the mysql directory. Remember to back up the previous files.
Copy codeThe Code is as follows:
Mv mysql/user. MYD mysql/user. MYD. bak
Mv mysql/user. MYI mysql/user. MYI. bak
Cp test/user. MY * mysql/
Chown mysql. mysql/user .*
4,Find the mysql process number, send the SIGHUP signal, and reload the permission table.
Copy codeThe Code is as follows:
[Root @ localhost mysql] # pgrep-n mysql
2184
[Root @ localhost mysql] #
[Root @ localhost mysql] # kill-SIGHUP 2184
5,Login Test
Copy codeThe Code is as follows:
[Root @ localhost mysql] # mysql-uroot-pyayun
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000,201 1, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
Affiliates. Other names may be trademarks of their respective
Owners.

Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement.

Mysql>


 

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.