windows下安装mysql的bat 脚本
程序员文章站
2022-06-04 15:17:08
...
@echo off
:start
SETLOCAL ENABLEEXTENSIONS&SETLOCAL ENABLEDELAYEDEXPANSION
rem 获取管理员权限
rem %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
set CURRENT_PATH=/d %~dp0
cd %CURRENT_PATH%
set MYSQL_NAME=mysql5.x
set MYSQL_PORT=3306
call :getMysqldPath
sc query %MYSQL_NAME% > nul
if errorlevel 1060 (goto notexist) else goto exist
REM 服务存在时启用mysql服务
:exist
echo 服务存在
goto :eof
REM 服务不存在时安装mysql服务
:notexist
echo 服务不存
call :getnetbyport "%MYSQL_PORT%"
goto :eof
::#####get netstat by port#####
:getnetbyport
if not "%~1"=="" (
set PORT=%~1
for /f "tokens=1,* delims=," %%a in ("!PORT!") do (
set myport=%%a
set PORT=%%b
::PORT==8888
for /f "delims=" %%z in ('netstat -a -n -o^|find /i ":!myport! "') do (
set tLine=%%z
set tLine=!tLine:%%=$!
call :getNetInfo "!tLine!" tProto tLocalAdd tForeignAdd tState tPID
set tLine=!tLine:$=%%!
::echo !tLine!
for /f "tokens=1 delims= " %%j in ('tasklist /nh /fi "PID eq !tPID!"') do (
echo %MYSQL_PORT%端口已占用[%%j]
rem pause
rem goto :eof
)
)
)
)
echo 安装mysql
call :installMysql
goto :eof
::#####FUNCTION#####
:getNetInfo
::将netstat -ano的某一行分隔成不同的变量
::call :getNetInfo "<netstat output line>" tProto tLocalAdd tForeignAdd tState tPID
if not "%~1"=="" (
for /f "tokens=1,2,3,4,5 delims= " %%i in ("%~1") do (
set %2=%%i
set %3=%%j
set %4=%%k
if "%%i"=="TCP" (
set %5=%%l
set %6=%%m
) else (
set %5=
set %6=%%l
)
)
)
goto :eof
:installMysql
rem 设置解压缩工具的path路径
set path=%path%;..\7zTool;
rem 设置压缩包名
set MYSQL=mysql-5.6.30-winx64
rem 设置路径
set INSTALL_PATH=D:\software\YXX
rem 判断安装位置是否为空
if exist %INSTALL_PATH%\%MYSQL% (
echo %INSTALL_PATH%\%MYSQL%已存在
pause
goto :eof
)
rem 删除原安装文件
rd/s/q %INSTALL_PATH%\%MYSQL%
echo. 安装Mysql开始…………
7z x -o%INSTALL_PATH% -aoa %MYSQL%.7z
rem 执行服务安装sql安装脚本
call %INSTALL_PATH%\%MYSQL%\install.bat
goto :eof
rem 获取mysqld 路径
:getMysqldPath
set TEMP_FILE=mysqlpath.txt
sc qc %MYSQL_NAME% | find /i "BINARY_PATH_NAME" > %TEMP_FILE%
for /f "tokens=3 delims= " %%i in (%TEMP_FILE%) do set MYSQLD_PATH=%%i
del /s /q %TEMP_FILE%
echo %MYSQLD_PATH%
goto :eof