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

删除mysql的root用户恢复方法相关推荐

  1. mysql删除root链接_删除mysql中root用户恢复方法

    1.# service mysqld stop  #停止mysql数据库服务 2.# service mysqld start --skip-grant-tables #跳过授权表启动mysql数据库 ...

  2. linux mysql删除root_Linux下误删MySQL的root用户解决方法

    开始对Linux界面不熟悉,可能由于不小心,把root误删了,怎么办? 1. # killall MySQLd    干掉所有mysql进程 2. # mysqld_safe --skip-grant ...

  3. mysql恢复root用户_恢复MYSQL的root用户

    1.恢复MYSQL的root用户 不小心把root的localhost权限给删除了,结果无法在服务器上以root用户登录,参考网上资料,解决方法如下: 1.KILL掉mysql的守护进程. 2.跳过权 ...

  4. mac mysql root不能登陆_Mac下新安装的MySQL无法登陆root用户解决方法

    一 设置MySQL命令行搜索路径 0.苹果->系统偏好设置->最下边点mysql 在弹出页面中 启动mysql服务 1.打开终端,输入: sudo vi ~/.bash_profile 如 ...

  5. mysql删去root用户无法登录_MySQL误删root用户导致无法登陆解决方法

    测试环境 删除前 mysql> select user,host,password from mysql.user; +------+-----------+------------------ ...

  6. xampp修改mysql密码_XAMPP环境下mysql的root用户密码修改方法_MySQL

    XAMPP 最近有客户忘记了mysql的root用户密码,使用mysqld_safe无法正常启动mysql,也没有办法按照mysql manual中的方法修改root密码. 网页教学网站长注:今天有学 ...

  7. MySQL重置root用户密码的方法

    MySQL重置root用户密码的方法 转自:http://www.xunmeinet.com/help/info.asp?id=45 本教程适用于采用Win2003.WinXP操作系统的迅美VPS和云 ...

  8. mysql 恢复root用户_mysql误删root用户恢复方案

    linux下误删mysql的root用户,解决方法 开始对liunx界面不熟悉,可能由于不小心,把root误删了,怎么办? 1. # killall mysqld    干掉所有mysql进程 2. ...

  9. Mac下新安装的MySQL无法登陆root用户解决方法

    Mac下新安装的MySQL无法登陆root用户解决方法 参考文章: (1)Mac下新安装的MySQL无法登陆root用户解决方法 (2)https://www.cnblogs.com/maxinlia ...

最新文章

  1. 中科院发布11大领域171个热点和新兴前沿!有你的研究方向吗?
  2. 【多元域除法】多项式除法电路原理及MATLAB详解
  3. chrome修改js数据怎么生效_chrome浏览器中 F12 功能的简单介绍
  4. 1.请求安全-- MD5的必要性以及实际应用场景
  5. 大数据之-Hadoop_1.x和2.x区别---大数据之hadoop工作笔记0012
  6. Python之路:初识
  7. RMAN 系列(四) ---- RMAN 备份
  8. SparkStreaming优化
  9. Oracle 11.2.0.4.0 Dataguard部署和日常维护(6)-Active Dataguard篇
  10. 我的电脑上的软件推荐
  11. 公式编辑器MathType中矩阵模板的使用技巧
  12. 为什么打工人996会猝死,而老板007不会?
  13. Excel之MATCH和INDEX函数(零基础快速上手)
  14. LeetCode1156. 单字符重复子串的最大长度
  15. Godot官网新闻翻译 - 2015年
  16. 医院挂号系统代码_智慧医院中心是怎样做的?分诊叫号系统如何正确使用!
  17. sql字符串和数字转换
  18. Failure to find xxx:jar:0.0.1 in https://repo.maven.apache.org/maven2 was cached in the local re
  19. php sid打印不出来,pinpoint php 使用不当引发棘手的问题 --psid sid tid pname ptype ah
  20. PHP将Word转PDF文件

热门文章

  1. 插入排序的Python实现
  2. 手把手教你发布自己的CocoaPods开源库
  3. openstack学习笔记三 创建第一个实例
  4. 实现在Android本地视频播放器开发
  5. 【IBM Tivoli Identity Manager 学习文档】11 TIM设计思路介绍
  6. php pdf 文字水印图片,php pdf添加水印(中文水印,图片水印)
  7. mysql故障诊断_mysql常见故障诊断
  8. php代码审计工具_【学习笔记】PHP代码审计入门:代码审计实例2
  9. 设计模式 — 行为型模式 — 中介者模式
  10. 互联网协议 — HTTP/2 超文本传输协议第 2 版