Last active
June 14, 2017 17:33
-
-
Save Brad-Christie/132b35874b4d991737421811f2810d0e to your computer and use it in GitHub Desktop.
Run MongoDB as a windows service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
TITLE MongoDB as a Service | |
REM Change to your installation dir. | |
SET MONGODIR=%ProgramFiles%\MongoDB\Server\2.7\bin | |
REM Change to where you'd like the databases and logs. | |
SET LOGDIR=%MONGODIR%\data\log | |
SET DBDIR=%MONGODIR%\data\db | |
SET LOGFILE=%LOGDIR%\mongod.log | |
SET CFGFILE=%MONGODIR%\mongod.cfg | |
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
REM Permissions Check | |
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
NET SESSION >nul 2>&1 | |
IF ERRORLEVEL 0 ( | |
ECHO Sufficient permissions found | |
) ELSE ( | |
COLOR 40 | |
ECHO Unsufficient permissions, must be run as administrator | |
ECHO. | |
PAUSE>nul | |
) | |
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
REM Pre-Installation Check | |
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
SC QUERY MongoDB >nul 2>&1 | |
IF NOT ERRORLEVEL 1060 ( | |
ECHO Service already exists. | |
GOTO Done | |
) | |
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
REM Installation | |
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
PUSHD "%MONGODIR%" | |
IF NOT EXIST "%LOGDIR%" MD "%LOGDIR%" | |
IF NOT EXIST "%DBDIR%" MD "%DBDIR%" | |
IF NOT EXIST mongod.cfg ( | |
ECHO logpath=%LOGFILE% > "%CFGFILE%" | |
ECHO dbpath=%DBDIR% >> "%CFGFILE%" | |
ECHO Config File Generated. | |
) | |
SC CREATE MongoDB binPath= "\"%MONGODIR%\mongod.exe\" --service --config=\"%CFGFILE%\"" DisplayName= "MongoDB" start= "auto" | |
IF ERRORLEVEL 0 ( | |
ECHO Service Installed | |
) ELSE ( | |
ECHO Error Creating Service | |
GOTO Done | |
) | |
NET START MongoDB | |
IF ERRORLEVEL 0 ( | |
ECHO Service Started...You're all set! | |
) ELSE ( | |
ECHO Error starting service. | |
GOTO Done | |
) | |
:Done | |
POPD | |
REM Need to UNDO? Command line help: | |
REM | |
REM C:\Windows> NET STOP MongoDB | |
REM C:\Windows> SC DELETE MongoDB | |
REM | |
REM You can also use Services applet in Computer Management to stop the | |
REM service, but you will still need to delete it through SC.EXE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment