当前位置:   article > 正文

数据库(mysql)忘记密码解决办法

数据库(mysql)忘记密码解决办法

步骤一:先停止数据库服务

  1. [root@GGB ~]# systemctl stop mysql.service
  2. [root@GGB ~]# systemctl status mysql.service
  3. ● mysql.service - MySQL Server
  4. Loaded: loaded (/etc/systemd/system/mysql.service; enabled; vendor preset: disabled)
  5. Active: failed (Result: exit-code) since 五 2024-07-12 01:03:45 CST; 1s ago
  6. Process: 3738 ExecStop=/usr/local/mysql/bin/mysqladmin shutdown (code=exited, status=1/FAILURE)
  7. Process: 2951 ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/3306/data (code=exited, status=0/SUCCESS)
  8. Main PID: 2951 (code=exited, status=0/SUCCESS)

步骤二:采用安全模式启动数据库(空密码方式进入数据库) 
 

  1. #mysqld_safe  一个命令
  2. #--skip-grant-tables   Start without grant tables. This gives all users FULL  两个参数
  3. #                      数据库服务启动时,不加载授权表(没有保安)
  4. #--skip-networking     Don't allow connection with TCP/IP
  5.                       关闭远程连接端口
  6. mysqld_safe --skip-grant-tables  --skip-networking   &
  7. [root@GGB ~]
  8. [root@GGB ~] mysqld_safe --skip-grant-tables --skip-networking &
  9. [1] 4642
  10. [root@GGB ~] 2024-07-11T17:15:00.581337Z mysqld_safe Logging to '/data/3306/data/GGB.err'.
  11. 2024-07-11T17:15:00.600168Z mysqld_safe Starting mysqld daemon with databases from /data/3306/data
  12. [root@GGB ~]
  13. [root@GGB ~] ps -ef | grep mysql
  14. root 4642 2019 0 01:14 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking
  15. mysql 4798 4642 3 01:14 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=GGB.err --pid-file=GGB.pid --socket=/tmp/mysql.sock
  16. root 4840 2019 0 01:15 pts/0 00:00:00 grep --color=auto mysql
  17. [root@GGB ~]#

步骤三:登录数据库设置数据库密码信息

  1. [root@GGB ~] mysql -uroot
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 7
  4. Server version: 8.0.26 MySQL Community Server - GPL
  5. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> flush privileges;
  11. Query OK, 0 rows affected (0.01 sec)
  12. mysql> alter user root@'localhost' identified by '1';
  13. Query OK, 0 rows affected (0.00 sec)
  14. mysql>

步骤四:重启数据库服务

  1. #先pkiil 掉之前后台启动的数据库
  2. [root@GGB ~] pkill mysql
  3. [root@GGB ~]
  4. [root@GGB ~] 2024-07-11T17:19:26.708758Z mysqld_safe mysqld from pid file /data/3306/data/GGB.pid ended
  5. [1]+ 完成 mysqld_safe --skip-grant-tables --skip-networking
  6. [root@GGB ~]
  7. [root@GGB ~]
  8. [root@GGB ~] systemctl start mysql.service
  9. [root@GGB ~] systemctl status mysql.service
  10. ● mysql.service - MySQL Server
  11. Loaded: loaded (/etc/systemd/system/mysql.service; enabled; vendor preset: disabled)
  12. Active: active (running) since 五 2024-07-12 01:21:31 CST; 2s ago
  13. Process: 3738 ExecStop=/usr/local/mysql/bin/mysqladmin shutdown (code=exited, status=1/FAILURE)
  14. Main PID: 5097 (mysqld)
  15. CGroup: /system.slice/mysql.service
  16. └─5097 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/3306/data
  17. [root@GGB ~]

步骤五:验证数据库密码

  1. [root@GGB ~] mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.26 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  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. mysql>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号