Do not restart the Mysql method to change the root password

  • 2020-06-12 10:49:32
  • OfStack

1. To solve the problem of forgetting password, restart Mysql
1, grant skip - - tables
The common approach is to use the ES7en-ES8en-ES9en option, which does not use the permissions system (privilege system) after mysqld server is started. The user does not need any account and has unlimited access to all the data in the database. For security, skip-ES15en is usually added, and mysqld does not listen for any TCP/IP connection requests. The operation process is as follows.
1) Modify the ES20en. cnf configuration file to add ES23en-ES24en-ES25en and ES26en-ES27en to the mysqld option.
2) Restart mysqld server again.
3) Modify the password stored in mysql. user table through sql statement. Execute flush privileges and re-enable the mysql permissions system.

UPDATE mysql.USER SET Password=PASSWORD('newpwd')WHERE User='root';
FLUSH PRIVILEGES;

4) Delete or comment the parameters of ES41en-ES42en-ES43en and ES44en-ES45en in the configuration file. If you are using ES46en-ES47en, you need to restart mysqld again. Because ES49en-ES50en is not a system variable, it is only a parameter option for mysqld, and cannot be set dynamically through the system variable. If skip-ES53en is not applied, you only need to execute flush privileges to reactivate the permissions system.
2. --init-file
mysqld_safe enables the init-file parameter option to execute sql statements that reset the password.
1) Create a new initialization file, such as /tmp/initfile, which contains the sql statement above to change the password.
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
FLUSH PRIVILEGES;

2) Close the mysqld service process.
3) Use mysqld_safe to start mysqld;
mysqld_safe --init-file=/home/me/mysql-init &

Both of the above methods are ways to reset your root password if you forget it, and you can see that you need to restart the mysqld service. Many people use the first option to reset the root password, but the recommended option is the second, which is fast and easy to secure.

2. Do not restart the mysqld method

1. The first step is to have an mysql database account with permission to modify, the current mysql instance account (with lower permissions, such as the ability to modify the test database) or any other account with the same version of the instance. Copy the files related to the user table under the data/mysql directory to the data/test directory.


[root@localhost mysql]# cp mysql/user.* test/
[root@localhost mysql]# chown mysql.mysql test/user.*

2. Use another account with lower authority to link the database and set the password data stored by user in the test database.

[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, 2011, 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 -A
Database changed
mysql> update user set password=password('yayun') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5  Changed: 0  Warnings: 0
mysql>

3. Copy the modified ES103en.MYD and ES105en.MYI to the mysql directory and remember to backup the previous files.

mv mysql/user.MYD mysql/user.MYD.bak
mv mysql/user.MYI mysql/user.MYI.bak
cp test/user.MY* mysql/
chown mysql.mysql mysql/user.*

4. Find the process number of mysql, send SIGHUP signal, and reload the permission table.

[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#
[root@localhost mysql]# kill -SIGHUP 2184

5. Login test

[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, 2011, 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 articles: