MySQL数据库管理员密码忘记如何修改?

    技术2022-07-11  139

    1 关闭MySQL数据库

    [root@db1 ~]# systemctl stop mysqld 或者: [root@db1 ~]# pkill mysql [root@db1 ~]# ps -aux |grep mysql

    2 使用安全模式启动MySQL数据库

    1)–skip-grant-tables 跳过授权表 2)–skip-networking 跳过TCP/IP连接

    [root@db1 ~]# /etc/init.d/mysqld start --skip-grant-tables --skip-networking & 或者; [root@db1 ~]# mysqld_safe --skip-grant-tables --skip-networking & #无需密码即可登录MySQL [root@db1 ~]# mysql

    3 修改root管理员密码

    mysql> alter user root@'localhost' identified by '123' ; #报错无法修改 ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

    4 手工加载授权表到内存

    mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #再次修改密码成功 mysql> alter user root@'localhost' identified by '123'; Query OK, 0 rows affected (0.00 sec)

    5 重启数据库到正常模式

    #重启MySQL数据库 [root@db1 ~]# systemctl restart mysqld #不适用密码无法登录 [root@db1 ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) #需使用修改后的密码才可以登录 [root@db1 ~]# mysql -uroot -p123 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 4 Server version: 5.7.28 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> show grants; +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 1 row in set (0.00 sec)
    Processed: 0.010, SQL: 9