删除mysql的root用户恢复方法

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:
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

本文转自 linuxzkq 51CTO博客,原文链接:http://blog.51cto.com/linuxzkq/1693991


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
关系型数据库 MySQL 数据库
mysql中如何将默认用户名root改成其他?
mysql中如何将默认用户名root改成其他?
27 0
|
2月前
|
关系型数据库 MySQL 数据库
MySQL忘记root密码、远程无法连接的解决方法
MySQL忘记root密码、远程无法连接的解决方法
|
2月前
|
关系型数据库 MySQL 数据库连接
关于MySQL-ODBC的zip包安装方法
关于MySQL-ODBC的zip包安装方法
|
4月前
|
Ubuntu 关系型数据库 MySQL
百度搜索:蓝易云【ubuntu下Mysql安装与root密码重置教程】
请注意,以上步骤是针对Ubuntu系统的。如果你使用的是其他Linux发行版,请相应地调整命令。
110 1
|
4月前
|
关系型数据库 MySQL
解决MySQL无法插入中文的方法
解决MySQL无法插入中文的方法
23 0
|
2月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
106 1
|
2月前
|
SQL 关系型数据库 MySQL
MYSQL分页limit速度太慢优化方法
MYSQL分页limit速度太慢优化方法
33 0
|
2月前
|
关系型数据库 MySQL 测试技术
mysql中删除数据的几种方法
在MySQL数据库中,删除数据是一个常见的操作,它允许从表中移除不再需要的数据。在执行删除操作时,需要谨慎,以免误删重要数据。
57 3
|
23天前
|
关系型数据库 MySQL Linux
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
22 0
|
2天前
|
缓存 关系型数据库 MySQL
mysql用in查询大量数据的方法
在MySQL中使用 IN 子句来查询大量数据时,性能可能会成为一个问题