MySQL - root密码忘记了怎么办

发布时间 2023-06-09 10:50:57作者: HOUHUILIN

 

 

 

 

1、先使用管理员身份打开两个命令提示符窗口(winA和winB)

2、在命令提示符窗口winA中将MySQL服务停掉net stop mysql

Microsoft Windows [版本 10.0.22621.1413]
(c) Microsoft Corporation。保留所有权利。

C:\Windows\System32>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。

3、输入mysqld --console --skip-grant-tables --shared-memory

Microsoft Windows [版本 10.0.22621.1413]
(c) Microsoft Corporation。保留所有权利。

C:\Windows\System32>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。

C:\Windows\System32>mysqld --console --skip-grant-tables --shared-memory
2023-03-30T06:09:38.525723Z 0 [System] [MY-010116] [Server] D:\Plugins\mysql\bin\mysqld.exe (mysqld 8.0.28) starting as process 21596
2023-03-30T06:09:38.545438Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-03-30T06:09:38.776569Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-03-30T06:09:38.911341Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'
2023-03-30T06:09:38.964124Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-03-30T06:09:38.964391Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-03-30T06:09:39.003842Z 0 [System] [MY-010931] [Server] D:\Plugins\mysql\bin\mysqld.exe: ready for connections. Version: '8.0.28'  socket: ''  port: 0  MySQL Community Server - GPL.

4、在命令提示符窗口winB中登录MySQLmysql -uroot -p (在Enter password:处直接按回车键即可)

C:\Windows\System32>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

5、切换到数据库mysql:use mysql;

6、修改root用户密码:alter user root@localhost identified by '新密码';

7、刷新权限表:flush privileges;

8、退出之后将两个窗口关掉,然后启动MySQL服务,用新密码登录即可。