mysql怎么重置root密码

发布时间:2021-09-14 18:23:59 作者:chen
来源:亿速云 阅读:115

本篇内容主要讲解“mysql怎么重置root密码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql怎么重置root密码”吧!

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

具体方法如下:

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

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

Kill掉MySQL进程,命令如下:

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

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

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

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

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

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

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

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

(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没有密码是无法进入的了。

[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

[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怎么重置root密码”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何简单重置mysql root密码
  2. MySQL密码忘了怎么办?MySQL重置root密码方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:PHP新手必知的七种数据类型

下一篇:yii1.1的安装步骤详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》