MySQL服务管理-[批处理]

@echo off
setlocal enabledelayedexpansion

:: 检查是否以管理员身份运行
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo 请右键选择"以管理员身份运行"此脚本
    timeout /t 5 >nul
    exit
)

:: 配置数据库类型(有效值:MariaDB / MySQL)
:: 示例:若使用MySQL,请修改为 set DBType=MySQL
set DBType=MySQL

:: 设置数据库服务名称(需与系统中注册的服务名一致)
:: - MariaDB默认服务名通常为 "MariaDB"
:: - MySQL默认服务名通常为 "MySQL" 或 "MySQL80"
set ServiceName=MySQL80

:menu
cls
echo.
echo.
echo [信息] 正在检查 %DBType% 数据库服务(服务名称:%ServiceName%)...
sc query "%ServiceName%" | findstr /C:"STATE" >nul||goto notExistsService
sc query "%ServiceName%" | findstr /C:"STATE" | findstr /C:"RUNNING" >nul

if %errorlevel% equ 0 (
    set status=正在运行
    set start_option=
    set stop_option=2.停止服务
    set restart_option=3.重启服务
) else (
    set status=已停止
    set start_option=1.启动服务
    set stop_option=
    set restart_option=
)

echo.
echo ====================================================
echo      %DBType% 服务管理(服务名称:%ServiceName%)
echo               当前状态: %status%
echo ====================================================
echo 【菜单】
if defined start_option echo %start_option%
if defined stop_option echo %stop_option%
if defined restart_option echo %restart_option%
echo 4.退出
echo ====================================================

choice /c 1234 /n /m "请输入选项:"
set choice=%errorlevel%

if "%choice%"=="1" (
    echo 正在启动服务...
    net start "%ServiceName%"
    timeout /t 5 >nul
    goto menu
)

if "%choice%"=="2" (
    echo 正在停止服务...
    net stop "%ServiceName%" 
    timeout /t 5 >nul
    goto menu
)

if "%choice%"=="3" (
    echo 正在重启服务...
    net stop "%ServiceName%" 
    timeout /t 2 >nul
    net start "%ServiceName%" 
    timeout /t 5 >nul
    goto menu
)

if "%choice%"=="4" exit

:notExistsService
echo 错误:系统中未找到名为 %ServiceName% 的 %DBType% 服务。
timeout /t 5 >nul
exit

 

posted @ 2025-12-10 09:45  逍客1  阅读(1)  评论(0)    收藏  举报