Remove the root user recovery method for MySQL

Source: Internet
Author: User
Tags reserved

1. # service mysqld Stop #停止mysql数据库服务
Shutting down MySQL. success!
2. # service mysqld start--skip-grant-tables #跳过授权表启动mysql数据库服务 (Note: Parameter--skip-grant-tables is skip authorization table)
Starting MySQL .... success!
3. # mysql-p #进入mysql数据库添加root用户并授权
Enter Password:#此处直接回车, no password to lose.
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 1
Server Version:5.6.26-log Source Distribution

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

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

Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.

mysql> use MySQL;
Reading table information for completion of table and column names
Can turn off this feature to get a quicker startup with-a

Database changed
Mysql> select Host,user from user;
+----------+-------+
| Host | user |
+----------+-------+
| 10.0.0.% | Nginx |
| 10.0.0.0 | Nginx |
+----------+-------+
2 rows in Set (0.03 sec)
mysql> Update user Set Password=password ("New_password") where user= "root";
Query OK, 0 rows affected (0.16 sec)
Rows matched:0 changed:0 warnings:0
mysql> INSERT INTO user set user= ' root ', ssl_cipher= ', x509_issuer= ', x509_subject= ';#插入root用户
Query OK, 1 row affected (0.03 sec)
Mysql> select Host,user from user;#查询添加root用户是否成功
+----------+-------+
| Host | user |
+----------+-------+
| | Root |
| 10.0.0.% | Nginx |
| 10.0.0.0 | Nginx |
+----------+-------+
3 Rows in Set (0.00 sec)
mysql> Update user set host= ' localhost ', select_priv= ' y ', insert_priv= ' y ', update_priv= ' y ', alter_priv= ' y ', delete_ priv= ' y ', create_priv= ' y ', drop_priv= ' y ', reload_priv= ' y ', shutdown_priv= ' y ', process_priv= ' y ', file_priv= ' y ', grant_ priv= ' y ', references_priv= ' y ', index_priv= ' y ', create_user_priv= ' y ', show_db_priv= ' y ', super_priv= ' y ', create_tmp_ table_priv= ' y ', lock_tables_priv= ' y ', execute_priv= ' y ', repl_slave_priv= ' y ', repl_client_priv= ' y ', create_view_priv = ' y ', show_view_priv= ' y ', create_routine_priv= ' y ', alter_routine_priv= ' y ', create_user_priv= ' y ' where user= ' root ';#更新root用户权限
Query OK, 1 row affected (0.12 sec)
Rows matched:1 changed:1 warnings:0
Mysql> exit
Bye
4. # Service Mysqld Restart#重新启动mysql数据库
Shutting down MySQL. success!
Starting MySQL ..... success!
5. # mysql_secure_installation#进入mysql数据库设置root用户密码

Note:running all PARTS of this SCRIPT are RECOMMENDED for all MySQL
SERVERS in PRODUCTION use! Please READ each STEP carefully!

In order to log into MySQL to secure it, we'll need the current
Password for the root user. If you ' ve just installed MySQL, and
You haven ' t set the root password yet, the password would be blank,
So, should just press ENTER here.

Enter current password to root (enter for none):
OK, successfully used password, moving on ...

Setting The root password ensures that nobody can log into the MySQL
Root user without the proper authorisation.

Set root Password? [y/n] Y#ask if you want to change the root user password, enter "Y", and then set the password for the root user you added earlier.
New Password:
Re-enter new password:
Password Updated successfully!#提示root用户密码修改成功
Reloading privilege tables.
... success!

#以下为mysql数据库的一些安全优化
By default, a MySQL installation have an anonymous user, allowing anyone
To log into MySQL without has to has a user account created for
them. This was intended only for testing, and the installation
Go a bit smoother. You should remove them before moving into a
Production environment.

Remove anonymous users? [y/n] Y
... success!

Normally, Root should only is allowed to connect from ' localhost '. This
Ensures that someone cannot guess at the root of password from the network.

Disallow Root login remotely? [y/n] Y
... success!

By default, MySQL comes with a database named ' test ' that anyone can
Access. This was also intended only for testing, and should be removed
Before moving into a production environment.

Remove test database and access to it? [y/n] Y
-Dropping test database ...
... success!

Reloading the privilege tables would ensure that all changes made so far
would take effect immediately.

Reload privilege tables now? [y/n] Y
... success!


All done! If you've completed all of the above steps, your MySQL
Installation should now is secure.

Thanks for using mysql!


Cleaning up ...

6. Verify that the previous 5 steps are valid
# mysql-uroot-p#此时不输入正确的root用户密码, the error has been prompted and cannot be logged on  
Enter Password:
ERROR 1045 (28000): Access denied for user ' root ' @ ' localhost ' (using Password:no)
# mysql-uroot-p
Enter Password:#再次输入正确的root用户密码后, enter the database, add success on behalf of the root user, and successfully modify the password as follows:
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 15
Server Version:5.6.26-log Source Distribution

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

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

Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| Information_schema |
| BBS |
| Blog |
| MySQL |
| Performance_schema |
| www |
+--------------------+
6 rows in Set (0.08 sec)

mysql> use MySQL;
Reading table information for completion of table and column names
Can turn off this feature to get a quicker startup with-a

Database changed
Mysql> select Host,user from user;
+-----------+-------+
| Host | user |
+-----------+-------+
| 10.0.0.% | Nginx |
| 10.0.0.0 | Nginx |
| localhost | Root |#代表root用户添加成功
+-----------+-------+
3 Rows in Set (0.00 sec)

Mysql> exit
Bye

This article is from the "Lanzhou Linux operation and Maintenance" blog, please be sure to keep this source http://linuxzkq.blog.51cto.com/9379412/1693991

Remove the root user recovery method for 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.