一、Windows 10開機自啓

1.1 核心啓動腳本

創建 C:\infra\start-services.bat

@echo off
REM 啓動MySQL服務
net start MySQL80

REM 啓動Redis
start "" /MIN "C:\Redis\redis-server.exe" "C:\Redis\redis.windows.conf"

REM 啓動Nginx  
start "" /MIN "C:\nginx\nginx.exe"

REM 啓動Nacos
cd /d "C:\nacos\bin"
start "" /MIN cmd /c "startup.cmd -m standalone"

1.2 設置開機自啓(兩種方法)

方法1:啓動文件夾

Win+R 輸入:shell:startup
將 start-services.bat 複製到此文件夾

方法2:任務計劃程序

schtasks /create /tn "AutoStartServices" /tr "C:\infra\start-services.bat" /sc onstart /ru SYSTEM

二、Linux系統開機自啓

2.1 創建Systemd服務文件

MySQL服務 /etc/systemd/system/mysql.service

[Unit]
Description=MySQL Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/mysqld_safe
Restart=always

[Install]
WantedBy=multi-user.target

Redis服務 /etc/systemd/system/redis.service

[Unit]
Description=Redis Server
After=network.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
Restart=always

[Install]
WantedBy=multi-user.target

Nginx服務 /etc/systemd/system/nginx.service

[Unit]
Description=Nginx Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
Restart=always

[Install]
WantedBy=multi-user.target

Nacos服務 /etc/systemd/system/nacos.service

[Unit]
Description=Nacos Server
After=network.target mysql.service

[Service]
Type=forking
Environment="MODE=standalone"
WorkingDirectory=/opt/nacos/bin
ExecStart=/opt/nacos/bin/startup.sh -m standalone
Restart=always

[Install]
WantedBy=multi-user.target

2.2 啓用服務並設置開機啓動

# 重新加載配置
systemctl daemon-reload

# 啓用開機啓動
systemctl enable mysql redis nginx nacos

# 立即啓動服務
systemctl start mysql redis nginx nacos

三、統一管理腳本

3.1 Windows管理腳本

:: manage-services.bat
@echo off
if "%1"=="start" (
    net start MySQL80
    start "" /MIN "C:\Redis\redis-server.exe"
    start "" /MIN "C:\nginx\nginx.exe"
    cd /d C:\nacos\bin && startup.cmd -m standalone
) else if "%1"=="stop" (
    net stop MySQL80
    taskkill /F /IM nginx.exe
    taskkill /F /IM redis-server.exe
    taskkill /F /IM java.exe
) else if "%1"=="status" (
    netstat -ano | findstr ":3306 :6379 :80 :8848"
)

3.2 Linux管理腳本

#!/bin/bash
# /usr/local/bin/manage-infra.sh
case $1 in
    start)
        systemctl start mysql redis nginx nacos
        ;;
    stop)
        systemctl stop nacos nginx redis mysql
        ;;
    restart)
        systemctl restart mysql redis nginx nacos
        ;;
    status)
        systemctl status mysql redis nginx nacos --no-pager
        ;;
    enable)
        systemctl enable mysql redis nginx nacos
        ;;
    disable)
        systemctl disable mysql redis nginx nacos
        ;;
    *)
        echo "用法: $0 {start|stop|restart|status|enable|disable}"
        ;;
esac

四、快速驗證方法

Windows驗證:

# 查看端口監聽
netstat -ano | findstr ":3306 :6379 :80 :8848"

# 測試服務
mysql -uroot -p
redis-cli ping
curl http://localhost
curl http://localhost:8848/nacos

Linux驗證:

# 查看服務狀態
systemctl is-active mysql redis nginx nacos

# 測試端口
nc -z localhost 3306  # MySQL
nc -z localhost 6379  # Redis
curl -I http://localhost  # Nginx
curl http://localhost:8848/nacos/health  # Nacos

五、故障排查要點

Windows常見問題:

  1. 權限不足:以管理員身份運行腳本
  2. 端口衝突netstat -ano | findstr :端口號
  3. 服務未啓動:檢查事件查看器日誌

Linux常見問題:

  1. 啓動失敗journalctl -u 服務名 -n 50
  2. 權限問題chmod +x 腳本文件
  3. 配置錯誤systemctl daemon-reload

六、關鍵注意事項

  1. 啓動順序:MySQL → Redis → Nginx → Nacos
  2. 日誌記錄:Windows看事件查看器,Linux看journalctl
  3. 權限設置:Linux服務文件需要正確用户權限
  4. 環境變量:確保JAVA_HOME等環境變量正確設置

核心命令總結

  • Windows開機啓動:shell:startupschtasks
  • Linux開機啓動:systemctl enable 服務名
  • 查看狀態:Windows用netstat,Linux用systemctl status
  • 重啓服務:Windows重啓對應進程,Linux用systemctl restart