MySQL忘记密码了怎么解决

笔者曾经有一次误删了mysqlroot用户,怎么办?

之前的解决方式是通过忽略授权表的方式重启mysql然后插入相关数据解决该问题的,但是这种方式需要重启mysql,会影响现有业务,那么有没有其他方式可以不重启MySQL就解决呢?

因为mysql的user表示MyISAM引擎的,因此我们可以通过修改对应的文件来解决这个问题。下面是本人在测试环境的一次演练,仅供参考。

一、查看现有用户

  1. 04:18:34 root@localhost [mysql]>select user,host from user;
  2. +---------------+-----------+
  3. | user | host |
  4. +---------------+-----------+
  5. | mysql.session | localhost |
  6. | mysql.sys | localhost |
  7. | root | localhost |
  8. +---------------+-----------+
  9. 3 rows in set (0,00 sec)

二、删除本地root用户

  1. 04:18:59 root@localhost [mysql]>drop user root@'localhost';

三、再次查看用户

  1. 04:20:02 root@localhost [mysql]>select user,host from user;
  2. +---------------+-----------+
  3. | user | host |
  4. +---------------+-----------+
  5. | mysql.session | localhost |
  6. | mysql.sys | localhost |
  7. +---------------+-----------+

现在我们开始恢复用户数据

四、移动user表相关文件

  1. [root@localhost mysql]# cp user.* /vagrant/mysql/3307/data/test/

五、登录另一个实例并查看user表

  1. 04:23:53 root@localhost [(none)]>use test;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. 04:23:56 root@localhost [test]>show tables;
  6. +----------------+
  7. | Tables_in_test |
  8. +----------------+
  9. | user |
  10. +----------------+
  11. 1 row in set (0,00 sec)
  12. 04:23:58 root@localhost [test]>select * from user\G
  13. *************************** 1. row ***************************
  14. Host: localhost
  15. User: mysql.session
  16. Select_priv: N
  17. Insert_priv: N
  18. Update_priv: N
  19. Delete_priv: N
  20. Create_priv: N
  21. Drop_priv: N
  22. Reload_priv: N
  23. Shutdown_priv: N
  24. Process_priv: N
  25. File_priv: N
  26. Grant_priv: N
  27. References_priv: N
  28. Index_priv: N
  29. Alter_priv: N
  30. Show_db_priv: N
  31. Super_priv: Y
  32. Create_tmp_table_priv: N
  33. Lock_tables_priv: N
  34. Execute_priv: N
  35. Repl_slave_priv: N
  36. Repl_client_priv: N
  37. Create_view_priv: N
  38. Show_view_priv: N
  39. Create_routine_priv: N
  40. Alter_routine_priv: N
  41. Create_user_priv: N
  42. Event_priv: N
  43. Trigger_priv: N
  44. Create_tablespace_priv: N
  45. ssl_type:
  46. ssl_cipher:
  47. x509_issuer:
  48. x509_subject:
  49. max_questions: 0
  50. max_updates: 0
  51. max_connections: 0
  52. max_user_connections: 0
  53. plugin: mysql_native_password
  54. authentication_string: *C7A1AAE2D250AFD864050FAF4935EF6F5D185A92
  55. password_expired: N
  56. password_last_changed: 2018-02-23 13:19:12
  57. password_lifetime: NULL
  58. account_locked: Y
  59. *************************** 2. row ***************************
  60. Host: localhost
  61. User: mysql.sys
  62. Select_priv: N
  63. Insert_priv: N
  64. Update_priv: N
  65. Delete_priv: N
  66. Create_priv: N
  67. Drop_priv: N
  68. Reload_priv: N
  69. Shutdown_priv: N
  70. Process_priv: N
  71. File_priv: N
  72. Grant_priv: N
  73. References_priv: N
  74. Index_priv: N
  75. Alter_priv: N
  76. Show_db_priv: N
  77. Super_priv: N
  78. Create_tmp_table_priv: N
  79. Lock_tables_priv: N
  80. Execute_priv: N
  81. Repl_slave_priv: N
  82. Repl_client_priv: N
  83. Create_view_priv: N
  84. Show_view_priv: N
  85. Create_routine_priv: N
  86. Alter_routine_priv: N
  87. Create_user_priv: N
  88. Event_priv: N
  89. Trigger_priv: N
  90. Create_tablespace_priv: N
  91. ssl_type:
  92. ssl_cipher:
  93. x509_issuer:
  94. x509_subject:
  95. max_questions: 0
  96. max_updates: 0
  97. max_connections: 0
  98. max_user_connections: 0
  99. plugin: mysql_native_password
  100. authentication_string: *C7A1AAE2D250AFD864050FAF4935EF6F5D185A92
  101. password_expired: N
  102. password_last_changed: 2018-02-23 13:19:26
  103. password_lifetime: NULL
  104. account_locked: Y
  105. 2 rows in set (0,00 sec)

六、把本实例的root用户插入刚刚移动过来的user表

  1. 04:25:03 root@localhost [test]>insert into user select * from mysql.user where user = 'root' and host ='localhost';
  2. Query OK, 1 row affected (0,02 sec)
  3. Records: 1 Duplicates: 0 Warnings: 0

七、再次移动user文件到原先的位置

  1. [root@localhost test]# cp user.* /vagrant/mysql/3306/data/mysql

八、重新加载配置到内存

  1. [root@localhost test]# kill -HUP `pidof mysqld`

通过该步mysql并不会重启,对应用不会有影响

九、重新登录,OK

  1. [root@localhost test]# mysql -uroot -p -S /tmp/mysql3306.sock
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 13
  5. Server version: 5.7.21-log MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. 04:30:57 root@localhost [(none)]>