windows下MySQL忘记密码重置root密码

打开cmd,进入到MySQL Server路径:

C:\windows\system32>pushd C:\Program Files\MySQL\MySQL Server 8.0\bin

查找MySQL进程:

C:\Program Files\MySQL\MySQL Server 8.0\bin>tasklist |findstr mysql
mysqld.exe                    3268 Services                   0      2,028 K

关闭进程:

C:\Program Files\MySQL\MySQL Server 8.0\bin>taskkill /F /PID 3268
成功: 已终止 PID 为 3268 的进程。

输入mysql,提示ERROR 2003 (HY000): Can’t connect to MySQL server on ‘localhost’ (10061)

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账户:

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --initialize --user=root --console
2019-08-20T02:43:39.866767Z 0 [System] [MY-013169] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.16) initializing of server in progress as process 14220
2019-08-20T02:44:12.803308Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fz12ylkyjo_Q
2019-08-20T02:44:36.398476Z 0 [System] [MY-013170] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.16) initializing of server has completed

这里自动生成了临时密码。

启动mysql服务:

C:\Program Files\MySQL\MySQL Server 8.0\bin>net start mysql
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.16

更改root账户密码为root:

mysql> alter user user() identified by "root";
Query OK, 0 rows affected (0.09 sec)

测试:

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.16    |
+-----------+
1 row in set (0.00 sec)

mysql> quit
Bye

    原文作者:风再继续吹起时
    原文地址: https://zhuanlan.zhihu.com/p/79376452
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞