延續 5.7 系列安全機制,免安裝版必須手動執行 mysqld --initialize --console 獲取隨機臨時 Root 密碼,再註冊服務、修正註冊表啟動路徑。本文保留實際 CMD 輸出日誌,供對照排查。
C:\mysql-5.7.13-winx64bin 目錄:C:\mysql-5.7.13-winx64\binC:\>cd mysql-5.7.13-winx64\bin
C:\mysql-5.7.13-winx64\bin>mysqld install
Service successfully installed.
C:\mysql-5.7.13-winx64\bin>net start mysql
MySQL 服務正在啟動 .
MySQL 服務無法啟動。
服務沒有報告任何錯誤。
請鍵入 NET HELPMSG 3534 以獲得更多的幫助。
C:\mysql-5.7.13-winx64\bin>mysqld --initialize --user=mysql --console
2016-06-21T14:07:20.922112Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated...
2016-06-21T14:07:22.669315Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-06-21T14:07:23.106116Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-06-21T14:07:23.308916Z 0 [Warning] No existing UUID has been found...
2016-06-21T14:07:23.340116Z 0 [Warning] Gtid table is not ready to be used...
2016-06-21T14:07:23.355716Z 1 [Note] A temporary password is generated for root@localhost: rr9.8h74twfR
C:\mysql-5.7.13-winx64\bin>mysql -uroot -prr9.8h74twfR
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
# 服務尚未啟動
C:\mysql-5.7.13-winx64\bin>net start mysql
MySQL 服務正在啟動 .
MySQL 服務已經啟動成功。
C:\mysql-5.7.13-winx64\bin>mysql -uroot -prr9.8h74twfR
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Server version: 5.7.13
...
mysql> set password=password('');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
C:\mysql-5.7.13-winx64\bin>mysql -uroot
Welcome to the MySQL monitor...
mysql> exit
SET PASSWORD = PASSWORD('newpass'); 或 ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass'; (5.7.6+)。範例改為空密碼僅供測試,**生產嚴禁空密碼**。net start mysql 報「找不到檔案」,需修正註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL\ImagePath 為完整路徑 "C:\mysql-5.7.13-winx64\bin\mysqld" MySQL (參考 ID 171745 註冊表修復圖解)。解壓 → 配置 my.ini (basedir/datadir) → 管理員 CMD
→ mysqld --initialize --console (記錄臨時密碼)
→ mysqld install (註冊服務)
→ [若啟動失敗] 修正註冊表 ImagePath
→ net start mysql
→ mysql -uroot -p'臨時密碼'
→ ALTER USER 'root'@'localhost' IDENTIFIED BY '強密碼';
→ FLUSH PRIVILEGES;
→ 驗證新密碼登入
掌握 --initialize --console 獲取臨時密碼與註冊表 ImagePath 修復,是 Windows 免安裝版 MySQL 5.7+ 部署成功的兩大關鍵。