WindowsStarter/base/Resources/mysql/template-register-mysql-with-data.bat

76 lines
1.9 KiB
Batchfile
Raw Normal View History

2025-08-14 18:10:10 +08:00
@echo off
setlocal enabledelayedexpansion
2025-08-17 02:18:33 +08:00
chcp 65001 >nul
2025-08-17 02:23:38 +08:00
title Register MySQL Service
2025-08-14 18:10:10 +08:00
2025-08-17 02:23:38 +08:00
:: Check if running as administrator
2025-08-14 18:10:10 +08:00
net session >nul 2>&1
if %errorLevel% neq 0 (
2025-08-17 02:23:38 +08:00
echo Please run this script as Administrator!
2025-08-14 18:10:10 +08:00
pause
exit /b 1
)
2025-08-17 02:23:38 +08:00
:: Configuration parameters
2025-08-14 19:35:02 +08:00
set "SERVICE_NAME=x_database"
2025-08-17 02:23:38 +08:00
set "MYSQLD_PATH=mysqld.exe"
set "MY_INI_PATH=%INSTALLPATH%\my.ini"
2025-08-14 19:35:02 +08:00
2025-08-17 02:23:38 +08:00
:: Verify mysqld.exe exists
where %MYSQLD_PATH% >nul 2>&1
if %errorLevel% neq 0 (
echo Error: mysqld.exe not found in PATH
pause
exit /b 1
)
:: Verify my.ini exists
if not exist "%MY_INI_PATH%" (
echo Error: Configuration file not found at %MY_INI_PATH%
pause
exit /b 1
)
:: 1. Stop and remove existing MySQL service
echo Stopping MySQL service [%SERVICE_NAME%]...
2025-08-14 19:35:02 +08:00
net stop %SERVICE_NAME% >nul 2>&1
2025-08-14 18:10:10 +08:00
2025-08-17 02:23:38 +08:00
echo Removing existing MySQL service [%SERVICE_NAME%]...
sc query %SERVICE_NAME% >nul 2>&1
2025-08-14 18:10:10 +08:00
if %errorLevel% equ 0 (
2025-08-17 02:23:38 +08:00
sc delete %SERVICE_NAME% >nul 2>&1
if %errorLevel% equ 0 (
echo [Success] MySQL service [%SERVICE_NAME%] removed
) else (
echo [Error] Failed to remove MySQL service [%SERVICE_NAME%]
pause
exit /b 1
)
2025-08-14 18:10:10 +08:00
) else (
2025-08-17 02:23:38 +08:00
echo [Info] MySQL service [%SERVICE_NAME%] does not exist (no action needed)
2025-08-14 18:10:10 +08:00
)
2025-08-17 02:23:38 +08:00
:: 2. Install MySQL service
echo Installing MySQL service [%SERVICE_NAME%]...
%MYSQLD_PATH% --install %SERVICE_NAME% --defaults-file="%MY_INI_PATH%"
2025-08-14 18:10:10 +08:00
if %errorLevel% equ 0 (
2025-08-17 02:23:38 +08:00
echo [Success] MySQL service [%SERVICE_NAME%] installed
2025-08-14 18:10:10 +08:00
) else (
2025-08-17 02:23:38 +08:00
echo [Error] Failed to install MySQL service [%SERVICE_NAME%]
2025-08-14 18:10:10 +08:00
pause
exit /b 1
)
2025-08-17 02:23:38 +08:00
:: 3. Start MySQL service
echo Starting MySQL service [%SERVICE_NAME%]...
2025-08-14 19:35:02 +08:00
net start %SERVICE_NAME%
2025-08-14 18:10:10 +08:00
if %errorLevel% equ 0 (
2025-08-17 02:23:38 +08:00
echo [Success] MySQL service [%SERVICE_NAME%] started
2025-08-14 18:10:10 +08:00
) else (
2025-08-17 02:23:38 +08:00
echo [Error] Failed to start MySQL service [%SERVICE_NAME%]
2025-08-14 18:10:10 +08:00
pause
exit /b 1
)
2025-08-17 02:23:38 +08:00
echo All operations completed successfully!
2025-08-14 18:10:10 +08:00
pause