[BATCH] Start and stop your DB by parsing the INI

These two batch files will start / stop your DB by parsing the MySQL config file to obtain the port number and other

relevant information. This is particularly handy if you have multiple realms running. You can drop these in with the

worldserver.conf files for each realm.

While running, you would see something like “MySQL Server 5.1 (./tdb/test… on port 45787)” in the titlebar,

depending on the settings in your “tdb_config.ini”

Note: You can delete the menu code in tdb_start.bat if you just want it to start without waiting for you to choose.

Just change “goto menu” to “goto startup” and delete everything between “:menu” and “:menuerror”

Usage:

Drop your “whatever.ini”, “tdb_start.bat” and “tdb_stop.bat” in a folder, edit the variables in the batch files to

suit your needs and then run “tdb_start.bat”. Run “tdb_stop.bat” to shutdown the DB whenever you need.

Tested on XP SP3

tdb_start.bat


@echo off


REM Starts a MySQL instance by reading the relevant information

REM  from the config file. Sets the console window title to reflect

REM  the port number and datasource in use


setlocal enabledelayedexpansion


REM **** IMPORTANT change this to match your info

REM

REM **** Be sure to put quotes around CONFIGFILE and SQLPATH if there are spaces


set CONFIGFILE=".\tdb_config.ini"

set SQLPATH="C:\Program Files\MySQL\MySQL Server 5.1\bin"

set SQLBIN=mysqld.exe


REM ****


set CurrSect=

set PORTSECT=[client]

set PORTTKEY=port

set PORTVAL=unknown

set DBSECT=[mysqld]

set DBKEY=datadir

set DBNAME=unknown


REM Try to read the port number from the ini file


for /F "tokens=1,* delims==" %%A in ('Type "%CONFIGFILE%'

) do (set Line=%%A

If "!Line:~0,1!" EQU "[" set CurrSect=!Line!

If /I !CurrSect! EQU %PORTSECT% if /I %%A EQU %PORTTKEY% set PORTVAL=%%B & goto portfound

)


:portfound


REM Try to read the database name from the ini file


set CurrSect=


for /F "tokens=1,* delims==" %%A in ('Type "%CONFIGFILE%'

) do (set Line=%%A

If "!Line:~0,1!" EQU "[" set CurrSect=!Line!

If /I !CurrSect! EQU %DBSECT% if /I %%A EQU %DBKEY% set DBNAME=%%B & goto dbfound

)


:dbfound


REM Determine if we can set the title of the DOS window


ver | find "XP" > nul

if %ERRORLEVEL% == 0 goto settitle


ver | find "2000" > nul

if %ERRORLEVEL% == 0 goto settitle


ver | find "2003" > nul

if %ERRORLEVEL% == 0 goto settitle


ver | find "NT" > nul

if %ERRORLEVEL% == 0 goto settitle


REM Unable to determine the version, unable to set title. Jump to the menu

goto menu


:settitle

title MySQL Server 5.1 (%DBNAME:~1,-7%... on port %PORTVAL%)


:menu

cls

echo MySQL Server 5.1 Launcher

echo.

echo 	1 = Start MySQL

echo.

echo 	x = Exit

echo.


set /p m=	Type an option and press enter: 

if %m%==* goto menuerror

if %m%==1 goto startup

if %m%==x goto quit


:menuerror

CLS

ECHO.

ECHO.

ECHO [ERROR] An error has occured, you will be directed back to the

ECHO [ERROR] main menu.

PAUSE	

GOTO menu


:startup

cls

echo   MySQL Server 5.1 is running...

echo.

echo   Binary: %SQLBIN%

echo   Configuration: %CONFIGFILE% (console)

echo.

echo   ** Please dont close this window while MySQL is running **

echo   ** To shutdown the server, use: tdb_stop.bat 		**

echo.


%SQLPATH%\%SQLBIN% --defaults-file=%CONFIGFILE% --standalone --console


if errorlevel 1 goto startuperror

goto quit


:startuperror

echo.

echo [ERROR] There was an error starting MySQL Server 5.1

echo.

echo Press any key to exit

pause > nul


:quit

tdb_stop.bat


@echo off


REM Shuts down a MySQL instance reading the relevant information

REM  from the config file


setlocal enabledelayedexpansion


REM **** IMPORTANT change these to match your info

REM

REM **** The user in USERNAME only needs shutdown privileges on the DB

REM **** Make sure to put an entry in PWDVAL if your user with shutdown

REM  	privileges requires a password

REM

REM **** Be sure to put quotes around CONFIGFILE and SQLPATH if there are spaces


set CONFIGFILE=".\tdb_config.ini"

set USERNAME=tdbuser

set PWDVAL=

set SQLPATH="C:\Program Files\MySQL\MySQL Server 5.1\bin"

set SQLBIN=mysqladmin.exe


REM ****


set CurrSect=

set PORTSECT=[client]

set PORTTKEY=port

set PORTVAL=0


REM Try to read the port number from the ini file


for /F "tokens=1,* delims==" %%A in ('Type "%CONFIGFILE%'

) do (set Line=%%A

If "!Line:~0,1!" EQU "[" set CurrSect=!Line!

If /I !CurrSect! EQU %PORTSECT% if /I %%A EQU %PORTTKEY% set PORTVAL=%%B & goto portfound

)


REM Unable to find the port

echo.

echo Unable to shutdown MySQL, port not found

echo.

echo Press any key to exit

pause > nul

goto quit


:portfound


echo Shutting down MySQL Server on port %PORTVAL%

%SQLPATH%\%SQLBIN% --port=%PORTVAL% --user=%USERNAME% --password=%PWDVAL% shutdown


:quit