MySQL数据库如果忘了账号的密码,除了重装和跑路,针对不同的版本,还是有很多方案可以解决的,社区的这篇文章,就给出了说明,以备不时之需,

https://www.modb.pro/db/429812?utm_source=index_ori

1. MySQL修改密码和远程登录

(1) 8.0以下版本修改密码和允许远程登陆

5.5. 5.6. 5.7版本修改密码和允许远程登陆

-- 修改密码
-- update mysql.user set grant_priv='Y',super_priv='Y' where user='root';
update mysql.user set password=password('test') where user='root';                #适用于5.5~5.6
update mysql.user set authentication_string=password('test') where user='root';   #适用于 >= 5.7 <8.0
set password for root@'%'=password('test');     # 适用于 <= 5.7
set password=password('test');                  #修改当前用户的密码
flush privileges;-- 允许远程登录,创建新用户
grant all on *.* to root@'localhost' identified by 'test'  with grant option;
grant all on *.* to root@'%' identified by 'test'  with grant option;
flush privileges;
select user,host,grant_priv,super_priv,password from mysql.user;  #5.5~5.6
select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; #>= 5.7wojin

(2) 8.0 以上修改密码和允许远程登陆

-- 修改密码
alter user root@'localhost' identified with mysql_native_password by 'test';     #5.7也支持该命令
flush privileges;-- 允许远程登录
grant all on *.* to root@'localhost' with grant option;
create user root@'%' identified with mysql_native_password by 'test';
grant all on *.* to root@'%' with grant option;
flush privileges;
select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;#注:mysql 8.0远程连接,在参数文件的[mysqld]下添加:
default_authentication_plugin=mysql_native_password-- 删除用户:
mysql> drop user yww@'localhost';
Query OK, 0 rows affected (0.01 sec)

测试5.7版本,以上修改密码操作,主从同步中,slave端也会同步修改。

2. 忘记MySQL的root密码后如何登陆数据库

在MySQL中,若密码输入错误,则会返回以下信息:

[root@mysql57 ~]#  mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

在MySQL中,若密码丢失则无法直接找回,只能通过特殊方式来修改密码。

首先,需要确认是“root@localhost”还是“root@%”密码丢失,因为这是2个不同的用户,若其中一个丢失,那么可以使用另一个用户登录,然后修改密码。

方法一:使用"--init-file"选项

具体修改密码的步骤:

(1) 登录MySQL数据库所在的服务器,停止MySQL服务。

对于Linux服务器,使用"ps -ef|grep mysql"来查找MySQL服务的进程号,然后手工kill掉MySQL进程。

对于Windows服务器,在cmd里输入services.msc打开"服务",使用"服务"来停止MySQL服务,或在cmd里使用"net stop mysql"停止。

(2) 创建一个文本文件mysql-init.sql,文件内容写入密码修改语句,

#MySQL 5.5和5.6版本使用以下语句:
set password FOR 'root'@'%' =password('test');
set password FOR 'root'@'localhost' =password('test');#从MySQL 5.7版本开始使用:
alter user 'root'@'%' identified by 'test';
alter user 'root'@'localhost' identified by 'test';

(3) 使用--init-file参数,启动MySQL实例,

#Linux
mysqld_safe --defaults-file=/etc/my.cnf --init-file=/tmp/mysql-init.sql &
#若是Windows服务,则可以通过如下命令启动:
D:\MySQL\mysql-8.0.15-win64\bin\mysqld --defaults-file=D:\MySQL\mysql-8.0.15-win64\data803314\mysql803314.ini --init-file=d:\mysql-init.sql --console

实例启动成功后,密码即修改完毕,最后再删除mysql-init.sql文件。

(4) 重新以正常方式启动MySQL服务并验证新密码。

方法二:使用"--skip-grant-tables"选项

在启动MySQL数据库时使用“--skip-grant-tables”选项,表示启动MySQL服务时跳过权限表认证。通过这种方式启动后,在使用root用户连接到MySQL时将不需要密码。

在使用skip-grant-tables时需要注意以下内容:

(1) 如果在命令行只添加了--skip-grant-tables,那么在修改完密码后,删除该参数,其实无需重启MySQL服务,只需要执行flush privileges即可。

(2) 从MySQL 8.0开始,必须去掉--skip-grant-tables才能远程登陆数据库。否则抛出如下报错:

[root@mysql8 data]# mysql -uroot -p -h x.x.x.x
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on 'x.x.x.x' (111)

(3) 从安全角度出发,建议加上--skip-networking,但是因为其是静态参数,所以将其剔除掉需要重启实例。

(4) 加上--skip-networking,虽然可以屏蔽掉TCP连接,但对于本地其它用户,只要有Socket文件的可读权限,都能无密码登录,存在安全隐患。

具体修改密码的步骤:

(1) 登录MySQL数据库所在的服务器,停止MySQL服务。

(2) 在参数文件的[mysqld]项下添加skip-grant-tables语句,或使用--skip-grant-tables选项重启MySQL服务,

#Linux启动MySQL服务:
/var/lib/mysql57/mysql5719/bin/mysqld_safe  --defaults-file=/etc/my.cnf --skip-grant-tables  &#Windows启动MySQL服务可以用命令行也可以用“服务”来启停,cmd命令行如下:
D:\MySQL\mysql-5.7.19-win32\bin\mysqld --defaults-file=D:\MySQL\mysql-5.7.19-win32\mysql553308.ini --skip-grant-tables --console#注意,若MySQL是8.0且安装在Windows上,则需要加上--shared-memory参数:
D:\MySQL\mysql-8.0.15-win64\bin\mysqld --defaults-file=D:\MySQL\mysql-8.0.15-win64\mysql803313.ini  --skip-grant-tables --shared-memory --console

(3) 使用空密码的root用户连接到MySQL,并且修改root密码。注意,此时可以以任意一个密码登陆也可以以一个空密码登陆MySQL,

[root@mysql57 ~]# mysql -uroot
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> update mysql.user set authentication_string=password('test') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 0  Warnings: 1
mysql> alter user 'root'@'localhost' IDENTIFIED BY 'test';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)mysql> alter user root@'%' IDENTIFIED BY 'test';
Query OK, 0 rows affected (0.00 sec)mysql> alter user root@'localhost' IDENTIFIED BY 'test';
Query OK, 0 rows affected (0.00 sec)MySQL [(none)]> select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;
+-----------+-----------+------------+------------+-------------------------------------------+-----------------------+
| user      | host      | grant_priv | super_priv | authentication_string                     | password_last_changed |
+-----------+-----------+------------+------------+-------------------------------------------+-----------------------+
| root      | %         | Y          | Y          | *827F67F8037D3F8F076544B6FFC6D56058166D8B | 2020-02-04 11:28:56   |
| mysql.sys | %         | N          | N          | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | 2019-07-18 10:52:13   |
| root      | localhost  | Y          | Y         | *827F67F8037D3F8F076544B6FFC6D56058166D8B | 2020-02-04 11:28:58   |
+-----------+-----------+------------+------------+-------------------------------------------+-----------------------+
3 rows in set (0.00 sec)

(4) 刷新权限表,使得权限认证重新生效。刷新权限表的语句必须执行,

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

(5) 重新使用新密码来登录MySQL。使用新密码成功登录后,再将MySQL服务器去掉“--skip-grant-tables”选项重启即可。

(6) 重启后验证新密码是否可用。

需要注意的是:

(1) 在MySQL 5.7以下版本中修改密码时,需要更新mysql.user表的Password列,尽管有authentication_string列,但是密码保存在Password列;而从MySQL 5.7开始,去掉了Password列,需要修改mysql.user表的authentication_string列,

#所以,在MySQL 5.7以下版本修改密码应该使用如下SQL:
update mysql.user set password=password('test') where user='root';#从MySQL 5.7开始可以使用:
update mysql.user set authentication_string=password('test') where user='root';

需要注意的是,从MySQL 8.0开始,password函数已经弃用。

(2) 从MySQL 5.7开始,不建议通过UPDATE的方式修改密码,更通用的其实是ALTER USER。但是,需要先通过flush privileges操作触发权限表的加载,然后才能使用alter user的方式来修改密码,

alter user root@'localhost' identified by 'test';
alter user root@'%' identified by 'test';

(3) 可以使用如下命令查询密码,

#查询密码,5.7以下
select user,host,grant_priv,super_priv,password,authentication_string from mysql.user;#查询密码,5.7以上
select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;

注意,

从MySQL 5.7开始,在mysql.user表中新增了password_last_changed列,但是该列只有使用ALTER USER语句后才会更新password_last_changed列。

3. 去掉密码安全策略

一般yum方式安装的mysql,可能需要去掉密码验证策略,

-- validate_password 是 mysql5.6以后可以引入的一个新密码校验插件, 管理用户密码长度. 强度等。show variables like 'validate_password%';
show status like 'validate_password%';
set global validate_password_policy=0;   #这个参数用于控制validate_password的验证策略 0–>low 1–>MEDIUM 2–>strong
set global validate_password_policy=LOW;
set global validate_password_length=1;show plugins;
uninstall plugin validate_password;
show variables like 'plugin_dir';cd /usr/lib64/mysql/plugin
mv validate_password.so validate_password.so_bk
mv /component_validate_password.so component_validate_password.so_bk

4. 密码含特殊符合

修改密码含特殊符号,

update mysql.user set authentication_string=password('test!@#') where user='root';
flush privileges;

登录报错,

[root@mysql57 ~]# mysql -uroot -ptest!@#
-bash: !@#: event not found

解决方案,

(1) 将密码用单引号引起来,

[root@mysql57 ~]# mysql -uroot -p'test!@#'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.27-log MySQL Community Server (GPL)Copyright (c) 2000, 2019, 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>

(2) 在特殊字符&前面加上'\'来进行登录,

[root@mysql57 ~]# mysql -uroot -ptest\!\@\#
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.7.27-log MySQL Community Server (GPL)Copyright (c) 2000, 2019, 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>

(3) 手动输入密码也可以,无需转义,

[root@mysql57 ~]# mysql -uroot -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Enter password:
test!@#

(4) navicat工具登录时候,直接输入密码也可以登录,无需转义。

当然了,最靠谱的,还是别忘了密码。

P. S. 如果你认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",

近期更新的文章:

《非标准数据块的表空间使用》

《数据库安全的重要性》

《CentOS 7.9安装Oracle 21c历险记》

《Linux的10大危险命令》

《你知道Oracle的数据文件大小有上限么?》

近期的热文:

《"红警"游戏开源代码带给我们的震撼》

文章分类和索引:

《公众号1000篇文章分类和索引》

MySQL忘了账号密码,除了跑路,还能补救么?相关推荐

  1. 误操作rm -rf /*之后该如何挽救,除了跑路还能怎么办

    一.前言 误执行了rm -rf /*  之后,除了跑路还能怎么办?其实像rm这样的敏感指定在生产环境中都是禁用的,如果某天,不小心执行了有什么方法可以挽救吗?答案是有的 二.初探案发现场 如果你的代码 ...

  2. MySQL数据库无完整备份删库,除了跑路还能怎么办?

    来源 | 阿丸笔记 责编| Carol 封图| CSDN│下载于视觉中国 "删库跑路"这个词儿,经常被挂在嘴边当玩笑,是因为大家都知道,一旦真的发生这样的事情,企业损失是无比惨重的 ...

  3. mysql试题百度云_MySQL数据库无完整备份删库,除了跑路还能怎么办?

    1.背景 前段时间,由于运维同事的一次误操作,清空了内网核心数据库,导致了公司内部管理系统长时间不可用,大量知识库内容由于没有备份险些丢失. 结合这两天微盟的删库跑路事件,我们可以看到,数据库的备份与 ...

  4. mysql获取数据库账号密码报错errorCode 1045, state 28000——常见5种解决办法

    本地写了个一个项目后想上传到阿里云服务器(Windows)中,mysql+tomcat+jdk都已经配置好了,但就在项目运行时报错,显示如下 create connection error, url: ...

  5. 为什么MySQL输入正确账号密码后仍然拒绝访问

    MySQL在输入正确的账号密码后仍然拒绝访问可能是由于以下几种原因造成的: 账号权限问题,检查该账号是否具有连接数据库的权限. IP地址限制,检查该账号是否只能在特定的IP地址连接. MySQL服务器 ...

  6. android密码忘记了vivo,vivo手机忘了账号密码怎么办?vivo手机账号密码找回教程...

    vivo手机和oppo手机作为今年手机行业的黑马可谓出尽了风头,在大多数手机走下坡路的时候,vivo.oppo两个品牌的手机确可以逆势而上,可见这两个品牌的手机多么牛,但是再牛的手机也有忘记账号密码的 ...

  7. 删库了,除了跑路还能怎么办?在线等!

    作者 | 林晓斌 编辑 | 小智 程序员小智刚刚犯了每个程序员都可能犯的错:误删库.他很热爱这份工作,但在网上搜索一圈解决办法,最后出来的结果都是:祭天.跑路等关键词.他想知道有没有什么补救方法,在线 ...

  8. 删库后,除了跑路还能怎么办?

    阅读本文大概需要 1.4 分钟. 当年悟空学艺于菩提祖师门下,老师遣他下山,悟空觉得自己蒙受师傅传授大恩,还没有报答.菩提祖师就说:不要提什么报答之恩,只要你日后闯出祸来不把为师说出来就行了. 我听说 ...

  9. 误执行了rm -fr /*之后,除了跑路还能怎么办?!

    作者 | 小林coding 前言 临近假期,我开始飘了. 写个简单的 Bash 脚本都不上心了,写完连检查都不检查,直接拖到到实体服务器跑. 结果一跑起来,发现不对劲,怎么一个简单脚本跑了 10  秒 ...

最新文章

  1. 基础006 宏基因组入门理论以及分析环境的部署
  2. GIT如何查看本地分支与远程分支的关联配置(git branch --set-upstream)
  3. 虚拟机系统的磁盘扩容妙招及案例
  4. 神策数据林美天于大数据与人工智能分享沙龙分享
  5. 《NIOSII那些事儿》rev7.0 PDF版本发布
  6. 项目构建之springboot集成lomback.xml,和log4j基于properties方式的日志配置记录
  7. 二叉树C++ | 实现删除节点_4
  8. php和mysql实现图书管理系统_0074 实现图书管理系统的登录、员工和菜单功能
  9. python groupby_用python自动生成全校学生成绩报告
  10. C#程序员66个编码好习惯
  11. C语言判断一个数是不是质数(C笔记)
  12. 以汉维语音翻译为例-uniapp原生顶部栏维语翻译-使用字体图标
  13. RoR vs. Java
  14. grads插值_grads各类参数设置.pptx
  15. Android环境搭建
  16. 汉字转拼音之Jpinyin 简单使用
  17. ispreedSheet ios编辑类似表格界面在ipad上
  18. confirm-order提交订单
  19. 无线蓝牙耳机手机端app开发_汪峰耗时1500天造了一款耳机,秒杀苹果AirPods!
  20. 网页视频播放插件ckplayer的使用-详细介绍

热门文章

  1. 分离出句子中的单词(双指针)(1)
  2. 老男孩全栈Python开发
  3. RabbitMQ实现定时任务发送短信通知
  4. C#关于ComboBox的使用
  5. JAVA消息(第二篇)AMQP-RabbitMQ实战!!!不打哈哈!!!包教包会!!不闹!!
  6. 33 款主宰 2017 iOS 开发的开源库
  7. 适合90后的,100句最凄美的伤感签名!
  8. 工控一体机在智能工厂的应用
  9. kali常用的软件包工具汇总
  10. 蓝桥杯算法之核桃的数量