九溪

溪水润知林,滴露启慧心

用户工具

站点工具


wiki:windows:windows-cmd-script

这是本文档旧的修订版!


批处理常用脚本:MYSQL相关

以下脚本下载后,请使用ANSI编码保存,否则中文乱码。

样例1:登陆MySQL,如果未安装,则先安装并启动服务

说明:判断是否已安装MySQL,

如果未安装,则调用样例2脚本安装并启动服务,

安装后,会使用默认用户密码登陆MySQL

00_run.bat
@echo off 
 
echo.
echo %date% %time%
echo.
 
SET curDir=%~dp0
 
echo 当前路径为 %curDir%
echo.
 
SC QUERY MySQL > NUL
 
rem 如果服务存在,跳转至 serverexist 标签
if ERRORLEVEL 1060 goto servernotexist
   goto serverexist

:serverexist
 
echo 将自动使用默认账户登录MYSQL,请注意及时修改密码
echo.
pause
"%curDir%bin\mysql" -h localhost -u root -pmysqld
 
goto end
 
:servernotexist
rem echo 开始执行安装
echo MySQL 服务无效,将要自动执行安装
echo.
call 01_install_then_run_server.bat
echo.
goto end
 
:end
pause

样例2:安装并启动MYSQL服务

设置my.ini、安装 MySQL 服务然后启动服务、设置 MySQL_HOME 环境变量

01_install_then_run_server.bat
@echo off
 
echo.
echo %date% %time%
echo.
 
SET curDir=%~dp0
 
echo 当前路径为 %curDir%
echo.
 
SC QUERY MySQL > NUL
 
rem 如果服务存在,跳转至 serverexist 标签
if ERRORLEVEL 1060 goto servernotexist
   goto serverexist

:serverexist
echo 服务已存在,程序将会卸载该服务,然后再继续安装。
echo.
set /p  choice=请按 Y 然后回车继续执行安装,或者按任意键退出:
echo.
if /i "%choice%" == "Y" goto choice_y_because_server_exist
    goto end	
:choice_y_because_server_exist
rem 继续安装
echo **********************************************************
echo 卸载已有服务
echo. 
net stop MySQL
sc delete MySQL
goto beginsetup

:servernotexist
rem echo 开始执行安装
goto beginsetup

 
:beginsetup
echo.
echo **********************************************************
echo.
echo 开始安装 MySQL (3)
echo.
rem pause
echo 1. 设置配置文件
echo.
rem 从 my_default.ini 中拷贝文本至my.ini
set mydefaultini="%curDir%my_default.ini"
set myini="%curDir%my.ini"
      
rem 清空文件,重新写入
if exist %myini% (
    del %myini%
    )	

rem usebackq 支持文件路径中带空格
for /f "usebackq tokens=1,* delims= " %%i in (%mydefaultini%) do (
	rem 追加内容
	echo %%i %%j>>%myini%  
	) 
set line1=basedir="%curDir%"
echo %line1%>>%myini% 
set line2=datadir="%curDir%data"
echo %line2%>>%myini% 
 
echo 配置文件配置完成
echo.
echo 2. 安装 MySQL 服务
echo.
cd bin
mysqld install
echo MySQL 服务安装完成
echo.
echo 3. 正在启动 MySQL 服务
echo.
net start mysql
echo MySQL 服务启动完成
echo.
echo **********************************************************
echo 正在设置环境变量
echo.
rem 左单引号,能够支持包含空格的路径
set environment_path="%curDir%
setx /M MYSQL_HOME %environment_path%
echo 已设置环境变量 MYSQL_HOME = %environment_path%
echo.
echo **********************************************************
echo.
echo 请查阅提示信息
cd ../
call ReadMeAfterInstalled.txt
echo. 
echo 将自动使用默认账户登录MYSQL,请注意及时修改密码
echo.
pause
 
"%curDir%bin\mysql" -h localhost -u root -pmysqld
echo.
:end
pause

样例3:停止并删除服务

02_stop_then_del_server.bat
@echo off
 
SC QUERY MySQL > NUL
if ERRORLEVEL 1060 goto servernotexist
   goto serverexist

 
:serverexist   
 
net stop MySQL
sc delete MySQL
 
goto goon

:servernotexist
echo server not exist
goto goon

:goon
 
if exist my.ini (
    del my.ini
    )
 
echo **********************************************************
echo 正在删除环境变量
echo.
setx /M MYSQL_HOME ""
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V MYSQL_HOME
echo 已删除环境变量 MYSQL_HOME
echo.
echo **********************************************************
 
echo uninstalled.
 
pause

评论

请输入您的评论. 可以使用维基语法:
 
wiki/windows/windows-cmd-script.1567242161.txt.gz · 最后更改: 2023/01/03 15:24 (外部编辑)