【MySQL】mysql root密码忘记怎么办?
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,MySQL忘记密码了怎么解决笔者曾经有一次误删了mysqlroot用户,怎么办?之前的解决方式是通过忽略授权表的方式重启mysql然后插入相关数据解决该问题的,但是这种方式需要重启mysql,会影响现
千家信息网最后更新 2025年11月07日【MySQL】mysql root密码忘记怎么办?
MySQL忘记密码了怎么解决
笔者曾经有一次误删了mysqlroot用户,怎么办?
之前的解决方式是通过忽略授权表的方式重启mysql然后插入相关数据解决该问题的,但是这种方式需要重启mysql,会影响现有业务,那么有没有其他方式可以不重启MySQL就解决呢?
因为mysql的user表示MyISAM引擎的,因此我们可以通过修改对应的文件来解决这个问题。下面是本人在测试环境的一次演练,仅供参考。
一、查看现有用户
04:18:34 root@localhost [mysql]>select user,host from user;+---------------+-----------+| user | host |+---------------+-----------+| mysql.session | localhost || mysql.sys | localhost || root | localhost |+---------------+-----------+3 rows in set (0,00 sec)二、删除本地root用户
04:18:59 root@localhost [mysql]>drop user root@'localhost';三、再次查看用户
04:20:02 root@localhost [mysql]>select user,host from user;+---------------+-----------+| user | host |+---------------+-----------+| mysql.session | localhost || mysql.sys | localhost |+---------------+-----------+现在我们开始恢复用户数据
四、移动user表相关文件
[root@localhost mysql]# cp user.* /vagrant/mysql/3307/data/test/五、登录另一个实例并查看user表
04:23:53 root@localhost [(none)]>use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed04:23:56 root@localhost [test]>show tables;+----------------+| Tables_in_test |+----------------+| user |+----------------+1 row in set (0,00 sec)04:23:58 root@localhost [test]>select * from user\G*************************** 1. row *************************** Host: localhost User: mysql.session Select_priv: N Insert_priv: N Update_priv: N Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: Y Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N Trigger_priv: NCreate_tablespace_priv: N ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *C7A1AAE2D250AFD864050FAF4935EF6F5D185A92 password_expired: N password_last_changed: 2018-02-23 13:19:12 password_lifetime: NULL account_locked: Y*************************** 2. row *************************** Host: localhost User: mysql.sys Select_priv: N Insert_priv: N Update_priv: N Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N Trigger_priv: NCreate_tablespace_priv: N ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *C7A1AAE2D250AFD864050FAF4935EF6F5D185A92 password_expired: N password_last_changed: 2018-02-23 13:19:26 password_lifetime: NULL account_locked: Y2 rows in set (0,00 sec)六、把本实例的root用户插入刚刚移动过来的user表
04:25:03 root@localhost [test]>insert into user select * from mysql.user where user = 'root' and host ='localhost';Query OK, 1 row affected (0,02 sec)Records: 1 Duplicates: 0 Warnings: 0七、再次移动user文件到原先的位置
[root@localhost test]# cp user.* /vagrant/mysql/3306/data/mysql八、重新加载配置到内存
[root@localhost test]# kill -HUP `pidof mysqld`通过该步mysql并不会重启,对应用不会有影响
九、重新登录,OK
[root@localhost test]# mysql -uroot -p -S /tmp/mysql3306.sockEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 13Server version: 5.7.21-log MySQL Community Server (GPL)Copyright (c) 2000, 2017, 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.04:30:57 root@localhost [(none)]>
用户
方式
文件
移动
再次
实例
数据
问题
影响
登录
密码
怎么办
仅供参考
业务
位置
内存
可以通过
引擎
环境
笔者
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
join数据库使用方法
广电网络软件开发
网络安全意识的意义
网络安全实现网站
云计算的网络安全问题研究
54所软件开发专业
讨论网络安全的重要性英语
贵州科达网络技术有限公司
自己买服务器与云服务器哪个贵
我要自学数据库
软件开发去应聘软件实施
x3 erp 数据库结构
北京时光信通网络技术
网络安全架构优化部署方案
温州软件开发驻场好不好
软件开发拖期
网络技术工作畅想
共享办公服务器
软件开发过程管理系统
计算机网络技术第六版知识点
hmc配置管理服务器
网络安全软件扫描工具
科密a1数据库配置
安徽惠普服务器虚拟化部署服务器
华亭市公安局网络安全监督检查
直销软件开发要求
2021国家网络安全宣传周h5
精灵守家怎么换服务器
高级网络技术书
无锡电商软件开发咨询热线