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

系统环境:centos7

数据库版本:mysql  Ver 14.14 Distrib 5.7.29, for linux-glibc2.12 (x86_64) using  EditLine wrapper

一:首先要关闭数据库,以什么方式启动的数据库就要以什么方式结束数据库

1 [root@db01 ~]#systemctl stop mysql
2 [root@db01 ~]#ss -ntl
3 State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
4 LISTEN      0      128                              *:22                                           *:*                  
5 LISTEN      0      128                           [::]:22                                        [::]:* 

:在命令行界面执行,如下命令,执行完成后,数据库root账户的密码就清空了

[root@db01 ~]#mysqld_safe --skip-grant-tables --skip-networking &
[1] 15143
[root@db01 ~]#2020-12-14T12:13:17.814024Z mysqld_safe Logging to '/data/mysql/data/db01.err'.
2020-12-14T12:13:17.843723Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data

三: 登录数据库,这个时候数据库没有密码的,如下

[root@db01 ~]#mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> 

四:修改数据库的密码,如下

mysql> alter user root@localhost identified by '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

在执行上述语句后,mysql会报错,继续执行如下语句,如下

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

再一次执行修改密码的命令,如下,

mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

  出现如上信息,说明数据库密码修改成功。

五:停止数据库的进程并登录验证数据库

 停止数据库的相关进程

pkill mysqld

数据库进程结束后,马上要启动数据库
systemctl start mysqld
[root@db01 ~]#mysql -uroot -p123456
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 3
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> 

  由以上信息可知,数据库登录成功

  

 

posted @ 2020-12-14 12:26  Molson  阅读(406)  评论(0编辑  收藏  举报