- 下载社区版压缩包:https://dev.mysql.com/downloads/
- 下载完后解压到安装目录(如:D:/MySQL),不要带中文或特殊字符;
- 将MySQL的bin目录加到系统环境变量path里(如:D:/MySQL/bin);
- 用管理员权限打开cmd窗口;
- 下面是关键步骤,在cmd中执行命令;
-- 初始化root用户【注意会给一个初始密码】 C:\Windows\system32>mysqld --initialize --user=root --console 2020-11-21T09:38:52.438070Z 0 [System] [MY-013169] [Server] D:\Program Files\mysql-8.0.22-winx64\bin\mysqld.exe (mysqld 8.0.22) initializing of server in progress as process 18560 2020-11-21T09:38:52.448760Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2020-11-21T09:38:53.139640Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2020-11-21T09:38:54.451045Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: X;hMs2cWiSz( -- 安装mysql C:\Windows\system32>mysqld --install mysql Service successfully installed. -- 启动mysql C:\Windows\system32>net start mysql mysql 服务正在启动 . mysql 服务已经启动成功。 -- 用root用户和密码登录mysql C:\Windows\system32>mysql -u root -p Enter password: ************ Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.22 ...... -- 重设root用户密码 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123'; Query OK, 0 rows affected (0.02 sec) -- 大功告捷 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
注意:如果之前安装过mysql导致在上述步骤无法成功,需要先卸载mysql,并删除安装目录下的data文件夹!
-- 停止mysql C:\Windows\system32>net stop mysql mysql 服务正在停止. mysql 服务已成功停止。 -- 卸载mysql C:\Windows\system32>mysqld --remove Service successfully removed. -- 初始化root用户【失败,因为data目录没删除】 C:\Windows\system32>mysqld --initialize --user=root --console 2020-11-21T09:38:10.965743Z 0 [System] [MY-013169] [Server] D:\Program Files\mysql-8.0.22-winx64\bin\mysqld.exe (mysqld 8.0.22) initializing of server in progress as process 17288 2020-11-21T09:38:10.967697Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting. 2020-11-21T09:38:10.967717Z 0 [ERROR] [MY-013236] [Server] The designated data directory D:\Program Files\mysql-8.0.22-winx64\data\ is unusable. You can remove all files that the server added to it. 2020-11-21T09:38:10.982219Z 0 [ERROR] [MY-010119] [Server] Aborting 2020-11-21T09:38:10.983814Z 0 [System] [MY-010910] [Server] D:\Program Files\mysql-8.0.22-winx64\bin\mysqld.exe: Shutdown complete (mysqld 8.0.22) MySQL Community Server - GPL.