wiki:windows:windows-cmd-script
这是本文档旧的修订版!
批处理常用脚本
样例:安装并启动服务
说明:设置my.ini、安装MySQL服务然后启动服务
- 01_install_then_run_server.bat
@echo off rem echo 注意:不支持带空格的目录 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% ) for /f "tokens=1,* delims= " %%i in (%mydefaultini%) do ( rem echo %%i %%j 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. setx /M MYSQL_HOME %curDir% echo 已设置环境变量 MYSQL_HOME=%curDir% echo. echo ********************************************************** cd ../ call ReadMeAfterInstalled.txt pause :end echo 正在退出
样例:关闭并卸载服务
- 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
样例:向MySQL中导入数据
- 01_build_data.bat
@echo off echo. echo %date% %time% echo. SET curDir=%~dp0 echo 当前路径为 %curDir% echo. if /i "%MYSQL_HOME%" == "" goto mysqlnotexist goto mysqlexist :mysqlexist echo. echo MYSQL_HOME 环境变量有效 echo %MYSQL_HOME% echo. goto dothing :mysqlnotexist echo. echo MYSQL_HOME 环境变量无效,请检查 echo. goto end :dothing set sqlfile=%curDir%import.sql set importsql=%curDir%CLDDGL20190820.sql rem 创建导入sql脚本 if exist %sqlfile% ( del %sqlfile% ) echo drop database if exists CLDDGL;>>%sqlfile% echo create database CLDDGL;>>%sqlfile% echo use CLDDGL;>>%sqlfile% echo source %importsql%>>%sqlfile% echo 生成脚本成功,开始执行导入 echo. pause %MYSQL_HOME%bin\mysql -h localhost -u root -pmysqld<%sqlfile% echo. echo 数据导入成功 echo. rem 删除导入sql脚本 if exist %sqlfile% ( del %sqlfile% ) :end pause
其中 CLDDGL20190820.sql 是要导入的数据
wiki/windows/windows-cmd-script.1566313571.txt.gz · 最后更改: 2023/01/03 15:24 (外部编辑)
评论