Simplest Enterprise Continuous Integration Solutions

Tuesday, July 12, 2011

MySQL - Windows silent install/uninstall

This batch script example is for a silent install/uninstall mysql-essential-5.1.58-winx64.msi with proper return code:


#############################
# 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%

11 comments:

  1. Excellent code especially that MySQL instance configuration part...thanks.

    ReplyDelete
  2. Thanks dude. code is working weeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllllllll.

    ReplyDelete
  3. completed my POC very quick by copying your code... Thank you so much

    ReplyDelete
  4. i'am installing silently mysql-5.1.68-win32.msi ,how to add fire wall exception and enable root access from remote machines check boxes...thx

    ReplyDelete
  5. Directory For PlacesMarch 16, 2023 at 5:49 AM

    Directory for Places also provides users with the ability to leave reviews and ratings of places they have visited. https://bit.ly/directory-for-places This allows for a community-driven approach to finding and sharing information on places, and helps other users make informed decisions about where to go.
    A business guide provides information and guidance on various aspects of business management and ownership.

    ReplyDelete