#############################
# MySQL silent installation
#############################
@echo off
set TEMP_DIR=C:\TMP
set MYSQL_MSI=mysql-essential-5.1.58-winx64.msi
REM Timestamp Generator
set cur_yyyy=%date:~10,4%
set cur_mm=%date:~4,2%
set cur_dd=%date:~7,2%
set cur_hh=%time:~0,2%
if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)
set cur_nn=%time:~3,2%
set cur_ss=%time:~6,2%
set TIMESTAMP=%cur_yyyy%-%cur_mm%-%cur_dd%_%cur_hh%_%cur_nn%_%cur_ss%
set INSTALLDIR=C:\Program Files\MySQL\MySQL Server 5.1
set MYSQL_DATA=C:\Program Files\MySQL\
set MYSQL_INSTALL_LOG=MySQL_5_1_InstallLog.txt
set MYSQL_CONFIG_EXE=MySQLInstanceConfig.exe
set MYSQL_SERVICE_NAME=MySQL
set MYSQL_PORT=3306
set TEMPLATE_INI=my-template.ini
set MYSQL_INI=mysql.ini
set MYSQL_CONFIG_LOG=MySQL_5_1_ConfigLog.txt
REM make sure a clean install
IF EXIST "%INSTALLDIR%\data" (
MOVE /Y "%INSTALLDIR%\data" "%INSTALLDIR%\data_%TIMESTAMP%"
)
REM install MySQL
msiexec -i %TEMP_DIR%\%MYSQL_MSI% /quiet /l*v "%TEMP_DIR%\%MYSQL_INSTALL_LOG%" INSTALLDIR="%INSTALLDIR%" MYSQL_DATA="%MYSQL_DATA%"
set RETURN_CODE=%ERRORLEVEL%
IF %RETURN_CODE% EQU 0 GOTO CONFIG_MYSQL
echo %RETURN_CODE%: Install MySQL failed
GOTO END
REM config MySQL instance
:CONFIG_MYSQL
"%INSTALLDIR%\bin\%MYSQL_CONFIG_EXE%" -i -q "-l%TEMP_DIR%\%MYSQL_CONFIG_LOG%" "-t%INSTALLDIR%\%TEMPLATE_INI%" "-c%INSTALLDIR%\%MYSQL_INI%" "AddBinToPath=yes" "-n%PRODUCTNAME%" "Port=%MYSQL_PORT%" "ServiceName=%MYSQL_SERVICE_NAME%"
set RETURN_CODE=%ERRORLEVEL%
IF %RETURN_CODE% EQU 0 GOTO CREATE_USER
echo %RETURN_CODE%: Configure MySQL failed
GOTO END
REM create MySQL user
:CREATE_USER
"%INSTALLDIR%\bin\mysql" -u root -e "CREATE USER 'mysql_user'@'localhost' IDENTIFIED BY 'mysql_pw';"
set RETURN_CODE=%ERRORLEVEL%
IF %RETURN_CODE% EQU 0 GOTO GRANT_USER
echo %RETURN_CODE%: Create MySQL user failed
GOTO END
REM grant MySQL user privileges
:GRANT_USER
"%INSTALLDIR%\bin\mysql" -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'mysql_user'@'localhost' IDENTIFIED BY 'mysql_pw';"
set RETURN_CODE=%ERRORLEVEL%
IF %RETURN_CODE% EQU 0 GOTO FLUSH_PRIVILEGES
echo %RETURN_CODE%: Grant MySQL user failed
GOTO END
REM flush privileges
:FLUSH_PRIVILEGES
"%INSTALLDIR%\bin\mysql" -u root -e "FLUSH PRIVILEGES;"
set RETURN_CODE=%ERRORLEVEL%
IF %RETURN_CODE% EQU 0 GOTO VERIFY_USER_ACCESS
echo %RETURN_CODE%: Flush privileges failed
GOTO END
REM verify MS access rights
:VERIFY_USER_ACCESS
"%INSTALLDIR%\bin\mysql" -D mysql -u mysql_user -h localhost -pmysql_pw -e "SELECT create_priv FROM user WHERE user='mysql_user' AND host='localhost'"
set RETURN_CODE=%ERRORLEVEL%
IF %RETURN_CODE% EQU 0 echo %RETURN_CODE%
IF %RETURN_CODE% NEQ 0 echo %RETURN_CODE%: Verfiy MySQL user failed
GOTO END
:END
#############################
# MySQL silent uninstallation
#############################
@echo off
msiexec.exe /I{7346AA6F-CC49-406D-AA97-93FBE3C25E79} /qn
echo %ERRORLEVEL%