删除mysql的root用户恢复方法

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

时间: 2024-10-14 15:05:02

删除mysql的root用户恢复方法的相关文章

删除mysql中root用户恢复方法

1.# service mysqld stop  #停止mysql数据库服务 2.# service mysqld start --skip-grant-tables #跳过授权表启动mysql数据库服务 3.# mysql -p     #进入mysql数据库添加root用户并授权 mysql> use mysql; mysql> select host,user from user; mysql> update user set password=password("new

mysql误删root用户恢复方案

linux下误删mysql的root用户,解决方法 开始对liunx界面不熟悉,可能由于不小心,把root误删了,怎么办? 1. # killall mysqld    干掉所有mysql进程 2. # mysqld_safe --skip-grant-tables &   进入mysql安全模式 3. 通过上一步进入mysql的console(控制台),输入:mysql -p 以root用户进入系统,因为root用户是默认存在的,前面我们误删的只是mysql表了的. 4. 提示,Enter p

删除mysql的root用户,重建本地用户及远程用户

一.重建本地用户:#vi /etc/my.cnf [mysqld]skip-grant-tables# service mysql restartShutting down MySQL..                                                                                                                                                 doneStartin

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

一 设置MySQL命令行搜索路径 0.苹果->系统偏好设置->最下边点mysql 在弹出页面中 启动mysql服务 1.打开终端,输入: sudo vi ~/.bash_profile 如果已存在删除:  sudo rm -rf .bash_* 2.输入 i 3.然后粘贴以下内容 # mysql alias mysql='/usr/local/mysql/bin/mysql' alias mysqladmin='/usr/local/mysql/bin/mysqladmin' # ls ali

MySQL重置root用户密码的方法

本教程适用于采用Win2003.WinXP操作系统的迅美VPS和云主机产品. 当管理员忘记MySQL密码怎么办?屡次输入密码,仍然提示错误,网站无法正常运行,数据库也无法管理,管理员束手无策. 网站程序或MySQL管理软件连接MySQL服务器时密码错误,会出现"1045 - Access denied for user 'root'@'localhost'(using password:YES)"的错误提示,如下图: 当确认已经忘记MySQL密码,则可以通过以下方案重置root用户密码

Linux下 mysql忘记root用户密码

今天在接收一台MYSQL服务器的时候发现忘记MYSQL的root用户的密码,查找资料发了各种文档里面也没有root用户密码,因此需要修改root用户密码. 首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库.因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的状态下,其他的用户也可以任意地登录和修改MySQL的信息.最安全的状态是到服务器的Console上面操作,并且拔掉网线. 1.修改MySQL的登录设置: 在[mysqld]的段中加上的s

XAMPP笔记之重置MySQL/MariaDB Root用户密码

目前在使用MYSQL,在MAC下安装使用的是XAMPP 4.5.2,需要重置MySQL/MariaDB Root用户密码.遇到一个小问题记录于下: 1,根据官方指导(http://localhost/dashboard/docs/reset-mysql-password.html)处的提示,运行如下命令: (1)先启动MYSQL SERVER,这个没有问题: (2)打开终端,切换到XAMPP的默认安装位置,在我的MAC上是 /Applications/XAMPP/xamppfiles/bin ,

关于忘记MySQL的root用户密码的问题

出错 信息: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)'及 如果忘记了MySQL的root用户密码可以如下操作(Linux下): 如果 MySQL 正在运行,首先杀之: killall -TERM mysqld. 启动 MySQL :bin/safe_mysqld --skip-grant-tables 就可以不需要密码就进入 MySQL 了mysql -uroot

Mysql修改root用户密码 For Mac

环境 Mysql版本:5.7.12 操作系统:OSX 10.11 安装文件:.dmg文件 MySQL:mysql-5.7.12-osx10.11-x86_64.dmg(注意5.7跟之前的字段有些不同,下面会说) Step 1 关闭mysql服务.无论你有没有开启mysql服务,保险起见都要运行一下以下命令. sudo /usr/local/mysql/support-files/mysql.server stop (注:我这里报错了:ERROR! MySQL server PID file co