服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL root密码的重置方法

MySQL root密码的重置方法

2021-06-21 18:08Tse先生 Mysql

这篇文章主要介绍了MySQL root密码的重置方法,帮助大家更好的理解和学习使用MySQL,感兴趣的朋友可以了解下

对于dba来说,丢失超管用户root的密码是致命的,

可以通过添加--ship-grant-tables参数来跳过权限表。

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

MySQL root密码的重置方法

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

MySQL root密码的重置方法

kill掉mysql进程,命令如下:

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

?
1
2
[root@tse2 tmp]# kill -9 9840 1
[root@tse2 tmp]# ps -ef |grep mysql

MySQL root密码的重置方法

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

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

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

MySQL root密码的重置方法

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

MySQL root密码的重置方法

给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

MySQL root密码的重置方法

?
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                |
+--------------------+
4 rows in set (0.01 sec)

以上就是mysql root密码的重置方法的详细内容,更多关于mysql root密码重置的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/Sungeek/p/12199303.html

延伸 · 阅读

精彩推荐