千家信息网

Mysql恢复管理密码

发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,恢复MySQL数据库密码步骤原理停止Mysql服务程序跳过授权表启动MySQL服务程序重设root密码(更新user表记录)以正常方式重启MySQL服务程序密码恢复实例例1:重置MySQL管理密码首先
千家信息网最后更新 2025年11月08日Mysql恢复管理密码

恢复MySQL数据库密码步骤原理

  • 停止Mysql服务程序

  • 跳过授权表启动MySQL服务程序

  • 重设root密码(更新user表记录)

  • 以正常方式重启MySQL服务程序


密码恢复实例

例1:重置MySQL管理密码

  • 首先停止已运行的MySQL服务程序

[root@host50 ~]# systemctl stop mysqld[root@host50 ~]# systemctl status mysqld● mysqld.service - MySQL Server   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)   Active: inactive (dead) since Tue 2019-07-02 03:54:56 CST; 6s ago     Docs: man:mysqld(8)           http://dev.mysql.com/doc/refman/en/using-systemd.html  Process: 1426 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)  Process: 1083 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 1430 (code=exited, status=0/SUCCESS)Jul 02 03:31:22 host50 systemd[1]: Starting MySQL Server...Jul 02 03:31:36 host50 systemd[1]: Started MySQL Server.Jul 02 03:54:55 host50 systemd[1]: Stopping MySQL Server...Jul 02 03:54:56 host50 systemd[1]: Stopped MySQL Server.
  • 跳过授权表启动MySQL服务程序(配置--skip-grant-tables选项)见文档最后一行

[root@host50 ~]# vim /etc/my.cnf[mysqld]## Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M## Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin## Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Mdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socksecure_file_priv="/myload"# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0default-storage-engine=innodblog-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidskip_grant_tables=1
  • 重连mysql后通过修改mysql库中user表中记录,做到重设root用户本机登录密码

[root@host50 ~]# systemctl restart mysqld[root@host50 ~]# mysql -urootmysql> UPDATE mysql.user SET authentication_string=PASSWORD('123456')-> WHERE user='root' AND host='localhost';Query OK, 1 row affected, 1 warning (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 1mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)mysql> exit

注:通过执行"FLUSH PRIVILEGES;"可使授权表立即生效,对于正常运行的MySQL服务,也可以用上述方法来修改密码,不用重启服务。本例中因为是恢复密码,最好重启MySQL服务程序,所以上述"FLUSH PRIVILEGES;"操作可跳过。

  • 重新以正常方式启动Mysql服务程序,验证新密码(可注释skip_grant_tables选项)

[root@host50 ~]# vim /etc/my.cnf[mysqld]#skip_grant_tables=1...[root@host50 ~]# systemctl restart mysqld[root@host50 ~]# mysql -urootERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)[root@host50 ~]# mysql -uroot -p123456mysql: [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 4Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>


例2:重设Mysql管理用户密码(已知密码)

  • 法一:使用mysqladmin管理工具,需要验证旧的密码

[root@host50 ~]# mysqladmin -u root -p password 'qaz123edc'                    Enter password:                                   mysqladmin: [Warning] Using a password on the command line interface can be insecure.Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
  • 法二:以root登录mysql后,使用set password指令设置(须先配置validate_password_policy=0)

mysql> set password for root@localhost=password('123456');Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 法三:以root登录mysql后,使用grant授权工具设置

mysql> grant all on *.* to root@localhost identified by '123456';Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 法四:以root登录MySQL后,使用update更新相应的表记录

mysql> update mysql.user set authentication_string=password('123456')    -> where user='root' and host='localhost';Query OK, 0 rows affected, 1 warning (0.00 sec)Rows matched: 1  Changed: 0  Warnings: 1












密码 服务 程序 登录 管理 工具 方式 用户 更新 运行 配置 验证 一行 不用 原理 实例 指令 数据 数据库 文档 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 物联网与网络安全哪个好 网络安全含金量最高的认证 vf数据库插入一行新数据 计算机网络安全基础舞蹈动作 租用外国服务器 控制软件开发过程 计算机软件开发与应用课程表 市人大网络安全实施方案 山东通用软件开发网上价格 服务器设备参数 软件开发类公司的营业成本 唐山市天气预报软件开发 怎么复制密码登录服务器 软件开发公司业务前置 链接无密码网络安全吗 珠海网络安全培训 给db2数据库授权报错 415国家网络安全教育日简报 东莞奥普特软件开发待遇 河北联想服务器厂商 速达附加数据库分区有问题 高级计算机网络技术课程 深圳贵友网络技术有限公司 奉贤区电商软件开发定制价格 sql数据库语句变多 深圳市骏明网络技术有限公司 一维码可以脱离数据库被识别吗 台湾省ipfs服务器配置 如何在数据库中找到网页密码 网络安全正能量语录
0