刪除mysql的root使用者恢複方法

來源:互聯網
上載者:User

標籤:mysql

1、# service mysqld stop  #停止mysql資料庫服務
Shutting down MySQL.. SUCCESS!
2、# service mysqld start --skip-grant-tables #跳過授權表啟動mysql資料庫服務(註:參數--skip-grant-tables為跳過授權表)
Starting MySQL.... SUCCESS!
3、# mysql -p    #進入mysql資料庫添加root使用者並授權
Enter password:      #此處直接斷行符號,不用輸密碼
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) 2000, 2015, 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 mysql;
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> 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 IS 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 will be blank,
so you should just press enter here.

Enter current password for 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             #此處詢問是否修改root使用者密碼,輸入"y"後斷行符號,給前面添加的root使用者佈建密碼
New password:
Re-enter new password:
Password updated successfully!    #提示root使用者密碼修改成功
Reloading privilege tables..
 ... Success!

#以下為mysql資料庫的一些安全最佳化
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make 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 be allowed to connect from ‘localhost‘.  This
ensures that someone cannot guess at the root 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 is 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 will ensure that all changes made so far
will 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 be secure.

Thanks for using MySQL!


Cleaning up...   

6、驗證前面5步操作是否有效
# mysql -uroot -p  #此時不輸入正確的root使用者密碼,已經提示錯誤,不能登入         
Enter password:
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
# mysql -uroot -p
Enter password:     #再次輸入正確的root使用者密碼後,進入資料庫,代表root使用者添加成功,並成功修改密碼,如下所示:
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) 2000, 2015, 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> 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
You 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

本文出自 “蘭州linux營運” 部落格,請務必保留此出處http://linuxzkq.blog.51cto.com/9379412/1693991

刪除mysql的root使用者恢複方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.