按照知乎这篇操作:windows下MySQL忘记密码重置root密码
(开始的时候在关闭进程的一步报错了,重新用管理员身份打开cmd就可以了)
进入目录
C:\WINDOWS\system32>cd C:\Program Files\MySQL\MySQL Server 8.0\bin
查找进程
C:\Program Files\MySQL\MySQL Server 8.0\bin>tasklist |findstr mysql
mysqld.exe 7964 Services 0 7,684 K
mysqld.exe 9208 Services 0 47,864 K
关闭进程
C:\Program Files\MySQL\MySQL Server 8.0\bin>taskkill /F /PID 7964
成功: 已终止 PID 为 7964 的进程。
C:\Program Files\MySQL\MySQL Server 8.0\bin>taskkill /F /PID 9208
成功: 已终止 PID 为 9208 的进程。
输入mysql
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
安装mysqld服务
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --install
Service successfully installed.
初始化root账户,最后生成了临时密码(dgd*kQtuG7!U)
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --initialize --user=root --console
2021-08-08T09:12:46.445499Z 0 [System] [MY-013169] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.21) initializing of server in progress as process 2536
2021-08-08T09:12:46.476230Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-08-08T09:12:47.916136Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-08-08T09:12:51.491881Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: dgd*kQtuG7!U
启动mysql服务
C:\Program Files\MySQL\MySQL Server 8.0\bin>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。
用刚才的临时密码登录
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21
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.
测试&退出,密码修改成了“root”
mysql> alter user user() identified by "root";
Query OK, 0 rows affected (0.02 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.21 |
+-----------+
1 row in set (0.00 sec)
mysql> quit
Bye
结束~!