MySQL: 利用Mysql的-e参数实现.bat脚本下快速重建数据库和用户.

在做软件自动化测试时,会有清空数据库重新初始化系统的情况.如果业务系统本身不支持清库操作,我们可以自己实现.

利用Mysql的-e参数实现.bat脚本下快速重建数据库和用户.

-e, --execute=name  Execute command and quit. (Disables --force and historyfile.)

 

提前把mysql可执行文件复制到.bat所在目录.

recreate_db_and_user.bat:

recreate_db_and_user.bat:

cls

@echo off
echo.
echo Uses: create_db_mysql.bat dbname dbuser
echo.
echo starting connect mysql...
 

set PATH=.;%PATH%

set HOSTNAME=127.0.0.1
set PORT=3306
set USERNAME=root
set PASSWORD=rootpassword

set DBNAME=%1
set DBUSER=%2
set DBPASSWORD=dbuserpassword

rem set TABLENAME="test_table_name"
 
set MYSQL_CMD=mysql -h%HOSTNAME%  -P%PORT%  -u%USERNAME% -p%PASSWORD%
rem echo %MYSQL_CMD%

echo.
set sql_cmd="drop database %DBNAME%"
echo %sql_cmd%
%MYSQL_CMD% -e %sql_cmd%
if errorlevel 1 (
    echo "drop database %DBNAME% failed ..."
)
 
echo.
set sql_cmd="create database IF NOT EXISTS %DBNAME% DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"
echo %sql_cmd%
%MYSQL_CMD% -e %sql_cmd%
if errorlevel 1 (
    echo "create databases %DBNAME% failed ..."
)
 
echo.
set sql_cmd="drop user '%DBUSER%'"
echo %sql_cmd%
%MYSQL_CMD% -e %sql_cmd%
if errorlevel 1 (
    echo "drop user %DBNAME% failed ..."
)

echo.
set sql_cmd="grant all privileges on %DBNAME%.* to %DBUSER%@'%%' identified by '%DBPASSWORD%'"
echo %sql_cmd%
%MYSQL_CMD% -e %sql_cmd%
if errorlevel 1 (
    echo "create user %DBNAME% failed ..."
)

echo.
pause
--------------------- 
作者:七侠镇莫小贝 
来源:CSDN 
原文:https://blog.csdn.net/kimqcn4/article/details/81869914 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

 

你可能感兴趣的:(自动化测试,运维)