适⽤:如果你已经安装好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\ (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\: 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\ (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\: 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 /f
(2)如果mysql服务⽆法启动,且没有报错,则说明你的mysql已经登录或使⽤,或者3306端⼝被占⽤,关闭对应进程就⾏。
(3)如果下次打开MySQL Workbench连接时⼜提⽰3306端⼝被占⽤或者密码错误,则按照上⾯⽅法查看3306端⼝是否被占⽤,杀死相应进程。如果还是不⾏,就再次进⼊MySQL Server的bin⽬录下执⾏net start mysql命令,将服务开启,再去MySQL Workbench连接
发布评论