使用bat批处理一键安装/卸载/启动/停止mysql服务

一键安装

@cd /d %~dp0
@%1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit
:以管理员身份运行cmd
@echo off 
setlocal enabledelayedexpansion 
:变量延迟的启动语句是“setlocal enabledelayedexpansion”,并且变量要用一对叹号“!!”括起来(注意要用英文的叹号)
echo 开始时间 %date%%time% >> ./install_log.txt 
echo ****************检测是否存在mysql服务*****************
echo.
SC QUERY mysql > NUL
if !errorlevel! equ 0 (
	echo *******************已经存在mysql服务******************
	echo 已经存在mysql服务 >> ./install_log.txt 
	echo.
	pause
	exit
)
echo ***************不存在mysql服务,开始安装**************
echo.
echo **************开始设置系统变量MYSQL_BIN***************
echo.
SET mysqldir=%cd%
SETX  /M "MYSQL_BIN"  "%mysqldir%\bin" >> ./install_log.txt
if !errorlevel! equ 0 (
	echo **************设置系统变量MYSQL_BIN成功***************
	echo.
) else ( 
	echo **************设置系统变量MYSQL_BIN失败***************
	echo 设置系统变量MYSQL_BIN失败 >> ./install_log.txt 
	echo.
	pause
	exit
)
:mysql路径
echo *****************开始设置系统变量path*****************
echo.
SET KEY_NAME=HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment
SET VALUE_NAME=path
FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B)
SET pathvalue=%pInstallDir%
:系统path值
SET setpath=0
:loop
for /f "tokens=1* delims=;" %%a in ("%pathvalue%") do (
	if "%%a"=="%%MYSQL_BIN%%" (
		SET setpath=1
	) 
::剩下的赋值给原来的副本,以备下次分段
set pathvalue=%%b
)
::如果还有剩余,则继续分割
if defined pathvalue goto :loop
if "%setpath%" == "1" (
	echo *****************设置系统变量path成功*****************
	echo.
) else (
	SETX  /M "Path"  "%pInstallDir%;%%MYSQL_BIN%%" >> ./install_log.txt
	if !errorlevel! equ 0 (
		echo ****************设置系统变量path成功******************
		echo.
	) else ( 
		echo *****************设置系统变量path失败*****************
		echo 设置系统变量path失败 >> ./install_log.txt 
		echo.
		pause
		exit
	)
)
echo ******************开始处理my.ini文件******************
echo.
SET myinifilename=my.ini
if exist %myinifilename% (attrib -r -s -h ./my.ini)
:去除文件具有系统、只读、隐藏之类属性这些属性,使用attrib命令
cd.>./my.ini 
:清空my.ini文件
echo ******************处理my.ini文件完成******************
echo.
echo ******************开始写入mysql配置*******************
echo.
echo [mysql]>> my.ini 
echo default-character-set=utf8>> my.ini 
echo.>>my.ini 
echo [mysqld]>> my.ini 
echo port=3306>> my.ini 
echo basedir=%mysqldir:\=/%/>> my.ini 
echo datadir=%mysqldir:\=/%/data/>> my.ini 
echo character-set-server=utf8>> my.ini 
echo default-storage-engine=MyISAM>> my.ini 
echo max_connections=100>> my.ini 
echo ******************写入mysql配置完成*******************
echo.
echo ******************开始安装mysql服务*******************
echo.
cd ./bin
mysqld --install mysql >> ../install_log.txt 
if !errorlevel! equ 0 (
	echo ******************安装mysql服务成功*******************
	echo.
) else ( 
	echo ******************安装mysql服务失败*******************
	echo 安装mysql服务失败 >> ../install_log.txt 
	echo.
	pause
	exit
)
echo ******************开始启动mysql服务*******************
echo.
net start mysql >> ../install_log.txt 
if !errorlevel! equ 0 (
	echo ******************启动mysql服务成功*******************
	echo 1 >> ./is_install.txt 
	echo.
) else ( 
	echo ******************启动mysql服务失败*******************
	echo 启动mysql服务失败 >> ../install_log.txt 
	echo.
	pause
	exit
)
echo 结束时间 %date%%time% >> ../install_log.txt 
echo. >> ../install_log.txt 
echo. >> ../install_log.txt 


pause

一键启动

@ echo off

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"

net start MySQL
echo 启动成功

pause

一键停止

@ echo off

rem ======获取管理员权限========
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
rem ======获取管理员权限 over========

net stop MySQL

pause

一键卸载

@ echo off

rem ======获取管理员权限========
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
rem ======获取管理员权限 over========

net stop MySQL

echo mysql路径
SET mysqldir=%cd%
echo %mysqldir%

pushd %mysqldir%\bin
mysqld --remove

pause

参考地址:
https://blog.csdn.net/minshiwang/article/details/104669791

你可能感兴趣的:(数据库,mysql,linux,服务器)