Windows 同時安裝多個 MySQL
記一次在 Windows 環(huán)境下手動安裝多個不同版本的 MySQL 的過程,并且設(shè)置手動啟動服務(wù),避免長時間占用后臺資源。
1. 下載 MySQL 軟件壓縮包
下載網(wǎng)址:https://dev.mysql.com/downloads/mysql/
找到需要的 MySQL 版本,選擇 Windows (x86, 64-bit), ZIP Archive 下載即可。
![]() |
![]() |
|---|---|
| 最新版本下載頁面 | 歷史版本下載頁面 |
2. 解壓縮
將上一步下載好的壓縮包分別解壓至合適的位置。

3. 創(chuàng)建配置文件
在 MySQL 軟件根目錄下創(chuàng)建名為 my.ini 的配置文件。

配置文件內(nèi)容如下:
[client]
# 客戶端默認(rèn)字符集
default-character-set=utf8mb4
# 客戶端連接的默認(rèn)端口號
port=5744
[mysqld]
# MySQL 服務(wù)的端口號(根據(jù)需要設(shè)置即可,我這里設(shè)置為與版本號一致)
port=5744
# MySQL 的安裝目錄
basedir="C:\dev\MySQL\mysql-5.7.44-winx64"
# 設(shè)置 MySQL 數(shù)據(jù)庫的數(shù)據(jù)的存放目錄
datadir="C:\dev\MySQL\mysql-5.7.44-winx64\data"
# 允許最大連接數(shù)
max_connections=20
# 字符集
character-set-server=utf8mb4
# 排序規(guī)則
collation-server=utf8mb4_general_ci
# 創(chuàng)建新表時將使用的默認(rèn)存儲引擎
default-storage-engine=INNODB
4. 初始化 MySQL
在 bin 目錄下打開終端執(zhí)行 .\mysqld.exe --initialize --console 命令以初始化 MySQL,初始化成功后將輸出 root 用戶的臨時密碼。例如下面的臨時密碼就是 p0q%adaOseZe。
C:\dev\MySQL\mysql-5.7.44-winx64\bin>.\mysqld.exe --initialize --console
2025-08-11T03:11:13.767360Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-08-11T03:11:14.125029Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-08-11T03:11:14.221824Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-08-11T03:11:14.318691Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d6eb798b-7660-11f0-9b33-00ff84a6f5fe.
2025-08-11T03:11:14.324439Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-08-11T03:11:14.661271Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2025-08-11T03:11:14.661576Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2025-08-11T03:11:14.665021Z 0 [Warning] CA certificate ca.pem is self signed.
2025-08-11T03:11:14.802173Z 1 [Note] A temporary password is generated for root@localhost: p0q%adaOseZe
如果之前安裝過 MySQL,可以直接把原來的 data 目錄復(fù)制過來,或者在配置文件中將 datadir 配置為之前的目錄,從而直接使用原來的數(shù)據(jù)。(不同的版本可能無法這樣操作)
5. 安裝 MySQL 服務(wù)并啟動
以管理員身份運(yùn)行終端,cd 到 bin 目錄下執(zhí)行 .\mysqld.exe --install <服務(wù)名> --defaults-file=<配置文件路徑> 命令以安裝 MySQL 服務(wù)。
執(zhí)行 sc config <服務(wù)名> start=demand 命令以配置 MySQL 服務(wù)為手動啟動。
執(zhí)行 net start <服務(wù)名> 命令以啟動 MySQL 服務(wù)。
服務(wù)名似乎不區(qū)分字母大小寫,安裝為
MySQL5744的服務(wù),使用mysql5744也能啟動。使用
net stop <服務(wù)名>命令可停止 MySQL 服務(wù)。使用
.\mysqld.exe --remove <服務(wù)名>命令可卸載 MySQL 服務(wù)。
C:\Windows\system32>cd \dev\MySQL\mysql-5.7.44-winx64\bin
C:\dev\MySQL\mysql-5.7.44-winx64\bin>.\mysqld.exe --install MySQL5744 --defaults-file="C:\dev\MySQL\mysql-5.7.44-winx64\my.ini"
Service successfully installed.
C:\dev\MySQL\mysql-5.7.44-winx64\bin>sc config MySQL5744 start=demand
[SC] ChangeServiceConfig 成功
C:\dev\MySQL\mysql-5.7.44-winx64\bin>net start mysql5744
MySQL5744 服務(wù)正在啟動 .
MySQL5744 服務(wù)已經(jīng)啟動成功。
如果啟動失敗,可以到配置文件 my.ini 配置的數(shù)據(jù)存放目錄查找 設(shè)備名稱.err 的日志文件查看原因。
6. 修改 root 密碼
在 bin 目錄下執(zhí)行 .\mysqladmin.exe -u root -p password <新密碼> 命令以修改 root 用戶的密碼,執(zhí)行后需輸入初始化時顯示的臨時 root 用戶密碼。
C:\dev\MySQL\mysql-5.7.44-winx64\bin>.\mysqladmin.exe -u root -p password 1234
Enter password: ************
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
檢查一下新密碼是否可用。
C:\dev\MySQL\mysql-5.7.44-winx64\bin>.\mysql.exe -u root -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
7. 創(chuàng)建腳本文件實(shí)現(xiàn)一鍵啟停
在 MySQL 軟件根目錄下創(chuàng)建名為 start.cmd 的 Windows 命令腳本文件,用于啟動 MySQL 服務(wù),內(nèi)容如下:
net start mysql5744
timeout /t 3
在 MySQL 軟件根目錄下創(chuàng)建名為 stop.cmd 的 Windows 命令腳本文件,用于停止 MySQL 服務(wù),內(nèi)容如下:
net stop mysql5744
timeout /t 3
使用 net 命令啟停 MySQL 服務(wù)需要管理員權(quán)限
選中這兩個腳本文件,使用鼠標(biāo)右鍵拖動到當(dāng)前文件夾內(nèi),在彈出的菜單中選擇在當(dāng)前位置創(chuàng)建快捷方式,右鍵剛剛新建的快捷方式,點(diǎn)擊快捷方式選項(xiàng)卡下的高級按鈕,勾選用管理員身份運(yùn)行復(fù)選框,確定保存。
這樣,雙擊快捷方式就可以直接以管理員身份運(yùn)行啟停腳本了,十分的優(yōu)雅。



浙公網(wǎng)安備 33010602011771號