适用:如果你已经安装好MySQL,但因为忘记root密码且不想重新下载而无法建立连接,那么本文可能对你有用
前提:已经在环境变量中配置好mysql路径
步骤:
Step1.准备配置文件
(1)查看根目录MySQL\MySQL Server 8.0下是否有my.ini文件
若无则手动添加,内容如下:
[mysqld] basedir=C:\Program Files\MySQL\MySQL Server 8.0 datadir=C:\Program Files\MySQL\MySQL Server 8.0\data port = 3306 character-set-server=utf8mb4 default_authentication_plugin=mysql_native_password [mysql]default-character-set=utf8mb4
注: 这里使用utfmb4而非一般的utf8是因为后来出现系统警告:utf8默认为utf8mb3,建议修改成utf8mb4,否则会引起歧义
注:若因权限无法创建文件,可以在桌面先创建一个txt文档,将my.ini内容复制进去,再修改txt文档为my.ini,最后拖进根目录下。
(2)查看根目录下是否有data文档,若无则创建一个。
Step2.获得初始化密码
以管理员身份打开cmd,执行如下指令
mysqld -- initialize --console
如果成功,没有任何[ERROR]出现,则记住以[Note]开头的新生成密码(若无则说明密码为空),并跳转到step 5
如果出现无法初始化data文件等情况,继续进行step3
2020-05-27T13:15:21.009962Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.18) starting as process 9524 2020-05-27T13:15:21.036492Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory. 2020-05-27T13:15:21.041391Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2020-05-27T13:15:21.043381Z 0 [ERROR] [MY-010119] [Server] Aborting 2020-05-27T13:15:21.045730Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.18) MySQL Community Server - GPL.
Step3.删除mysqld
先删除根目录下data文件夹的内容,再执行mysqld -remove MySQL命令,删除mysqld
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld -remove MySQL
结果如下:
Step4.重新初始化mysqld
执行如下命令:
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --initialize
可以看到data文件夹下生成新文件
Step5.安装mysqld
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --install Service successfully installed.
Step6.启动mysql
C:\WINDOWS\system32>net start mysql MySQL 服务正在启动 . MySQL 服务已经启动成功。
Step7.登录mysql
C:\WINDOWS\system32>mysql -u root -p
此处提示输入密码,如果前面有新密码生成,则使用之,若无,则直接按回车。进入以下界面
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.18 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>
Step8.更换密码
使用ALTER修改密码(下面的password处换成你自己的新密码)
mysql> ALTER USER "root"@"localhost" IDENTIFIED BY "password"; Query OK, 0 rows affected (0.01 sec)
最后去mysql workbench建立连接,输入密码,成功连接。
PS:
(1)如果出现以下端口占用提示
2020-05-27T13:19:25.830301Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.18) starting as process 13376 2020-05-27T13:19:27.055277Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2020-05-27T13:19:27.064723Z 0 [ERROR] [MY-010262] [Server] Can‘t start server: Bind on TCP/IP port: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 2020-05-27T13:19:27.070412Z 0 [ERROR] [MY-010257] [Server] Do you already have another mysqld server running on port: 3306 ? 2020-05-27T13:19:27.082431Z 0 [ERROR] [MY-010119] [Server] Aborting 2020-05-27T13:19:28.077042Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.18) MySQL Community Server - GPL.
说明3306端口被占用,新打开cmd,查看端口占用情况
C:\WINDOWS\system32>netstat -ano|findstr "3306"
杀死对应的进程
C:\WINDOWS\system32>taskkill/f /pid 15060
(2)如果mysql服务无法启动,且没有报错,则说明你的mysql已经登录或使用,关闭对应进程就行。