mysql重置root密码


本文摘自PHP中文网,作者V,侵删。

对于数据库来说,如果忘记root用户的密码,那将是非常致命的,但是我们可以通过添加--ship-grant-tables参数来跳过权限表。

具体方法如下:

忘记root密码,进不去数据库:

1.png

这个时候需要强制停库,先查看MySQL进程号

2.png

Kill掉MySQL进程,命令如下:

Kill完可以再查看是否还有进程

1

2

[root@tse2 tmp]# kill -9 9840 1

[root@tse2 tmp]# ps -ef |grep mysql

0717b539d505182591591cc08d69d49.png

建议停库这种操作不要影响业务的情况下,停服操作的,还有最好这种密码记录好的,可以用keepass保存密码软件!

免费学习视频教程分享:mysql视频教程

然后加跳过权限表参数,重启数据库。这样即使不输入密码,也可以进入数据库。

1

2

[root@tse2 bin]# mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &

[1] 4854

3.png

直接输入mysql不需要密码即可进入

989229735cc7f02f9d3ae025903de10.png

给root用户设置新的密码并刷新权限,MySQL5.7之后,库下的password字段用authentication_string字段代替。

1

2

3

4

5

6

7

8

(product)root@localhost:mysql.sock [(none)]> use mysql;

Database changed

(product)root@localhost:mysql.sock [mysql]> update user set authentication_string=password('123456') where user='root';

Query OK, 0 rows affected, 1 warning (0.00 sec)

Rows matched: 1  Changed: 0  Warnings: 1

 

(product)root@localhost:mysql.sock [mysql]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

设置完成之后,重启数据库。注意重启的时候不需要再加--skip-grant-tables参数了,正常启动服务,输入新的密码可以正常进入数据库了。这边重启以后测试直接mysql没有密码是无法进入的了。

1

2

3

4

5

6

7

8

[root@tse2 bin]# mysqld_safe --deaults-file=/etc/my.cnf &

[2] 6720

[root@tse2 bin]# 2020-01-16T02:55:45.223195Z mysqld_safe Logging to '/mysql/mysql3306/logs/error.log'.

2020-01-16T02:55:45.262302Z mysqld_safe A mysqld process already exists

 

[2]+  Exit 1                  mysqld_safe --deaults-file=/etc/my.cnf

[root@tse2 bin]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

这边输入密码连接,即可正常进入,是修改后的密码123456

dc08e22cf550e1612302569d2fafce4.png

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

[root@tse2 bin]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.23-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2018, 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.

 

(product)root@localhost:mysql.sock [(none)]> use mysql

Database changed

(product)root@localhost:mysql.sock [mysql]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

rows in set (0.01 sec)

相关文章教程推荐:mysql教程

以上就是mysql重置root密码的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

mysql数据库压缩版本安装与配置详细教程

一篇文章带你入门sql编程

mysql 出现1267错误怎么办

mysql数据库如何允许远程连接?

mysql中的中文显示为问号怎么办

mysql essential版本和普通版本有什么区别?

mysql中关于超长自动截断的案例详解

mysql中关于表,列以及库的增删改查问题总结(收藏)

mysql什么是事务处理 ?(代码示例)

mysql的加密方法

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...