首页 > 问答 > 关键词  > MySQL最新资讯  > 正文

MySQL忘记密码怎么办 密码重置详细教程

2020-07-08 10:07 · 稿源:数据库干货铺

登录MySQL,密码输入错误

/*密码错误,报如下错误*/[root@TESTDB~]#mysql-uroot-p-P3306
Enterpassword:
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:YES)

如果忘记密码,对于MySQL而言处理起来也相对比较简单。但需要修改配置,重启数据库。可以按照如下步骤处理。

1. 修改数据库配置文件

vim/etc/my.cnf--添加如下参数
skip_grant_tables

2. 重启数据库

如果部署了服务 可以重启数据库服务重启,如果没有部署,需要杀掉数据库进程,在重新启动数据库。

/*重启数据库服务*//etc/init.d/mysqldrestart
或
ps-ef|grepmysql/*查出MySQL的进程号,下一步中使用*/kill3051629246/*不建议使用kill-9*/

3. 登录数据库修改密码

/*此时可以直接登录数据库无需输入密码*/[root@TESTDB~]#mysql-uroot-P3306
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis4Serverversion:5.7.23-24-logPerconaServer(GPL),Release24,Revision57a9574
Copyright(c)2009-2018PerconaLLCand/oritsaffiliates
Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>

再修改密码

/*MySQL5.7中修改密码*/mysql>updatemysql.usersetauthentication_string=password('123456')whereuser='root'andhost='localhost';
QueryOK,0rowsaffected,1warning(0.02sec)
Rowsmatched:1Changed:0Warnings:1mysql>flushprivileges;
QueryOK,0rowsaffected(0.06sec)

注:

a) 不可以使用set password命令修改密码,只能通过更新数据库表的方式

mysql>setpassword=password('123456');
ERROR1290(HY000):TheMySQLserverisrunningwiththe--skip-grant-tablesoptionsoitcannotexecutethisstatement

b) 使用update表mysql.user的方式需要flush privileges生效

c) 不同的版本mysql.user的字段以及密码加密方式不同,例如MySQL5. 6 中密码存储在password中,MySQL8. 0 中加密方式有变更等,处理时需要根据版本来相应修改脚本处理。

4 . 将配置文件还原

去掉第 1 步中my.cnf配置文件中添加的skip_grant_tables参数

vim /etc/my.cnf

#skip_grant_tables/*注释掉该参数*/

5. 重启数据库

Mysql5. 7 中可以直接在MySQL命令行中使用shutdown命令关闭数据库,之后再启动数据库即可。

mysql>shutdown;
QueryOK,0rowsaffected(0.00sec)

启动后,即可使用重置后的密码登录

[root@TESTDB~]#mysql-uroot-P3306-p'123456'mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis3Serverversion:5.7.23-24-logPerconaServer(GPL),Release24,Revision57a9574
Copyright(c)2009-2018PerconaLLCand/oritsaffiliates
Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>

至此,密码重置完毕。

本文转载自微信公众号【数据库干货铺】。

举报

  • 相关推荐
  • 大家在看

今日大家都在搜的词:

热文

  • 3 天
  • 7天