WindowsサービスとしてMongoDBを実行する方法

MongoDBをWindowsサービスとして実行する方法

This Article is outdated!
この最新のinstall MongoDB as Windows Service.ガイドを参照してください

MongoDBをWindowsにインストールする方法を示すガイド。

1. mongod –ヘルプ

mongod --help」と入力して、Windowsサービスに関連するすべてのコマンドを理解してください。

C:\MongoDB\bin>mongod --help

Windows Service Control Manager options:
  --install                install mongodb service
  --remove              remove mongodb service
  --reinstall             reinstall mongodb service (equivilant of mongod
                             --remove followed by mongod --install)
  --serviceName arg           windows service name
  --serviceDisplayName arg windows service display name
  --serviceDescription arg    windows service description
  --serviceUser arg              user name service executes as
  --servicePassword arg       password used to authenticate serviceUser

2つの--installおよび--remove引数が必要です。

2. Windowsサービスとしてインストール

Windowsサービスとしてインストールするには、次のように「mongod --install」を発行します。

#> mongod --dbpath "c:\mymongodb" --logpath "c:\mymongodb\logs.txt" --install --serviceName "MongoDB"
all output going to: c:\mymongodb\logs.txt
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.

つまり、「c:\mymongodb」データディレクトリを指すMongoDB、「c:\mymongodb\logs.txt」へのログ出力、および「MongoDB」という名前のWindowsサービスをインストールします。

図:MongoDBはWindowsサービスとしてインストールされています

mongodb as windows service

3. アンインストール

上記でインストールしたMongoDBサービスをアンインストールするには、インストールしたサービス名とともに「mongod --remove」を発行します。

#> mongod --remove --serviceName "MongoDB"
Deleting service MongoDB.
Service deleted successfully.
Fri Apr 29 18:39:06 dbexit:
Fri Apr 29 18:39:06 shutdown: going to close listening sockets...
Fri Apr 29 18:39:06 shutdown: going to flush diaglog...
Fri Apr 29 18:39:06 shutdown: going to close sockets...
Fri Apr 29 18:39:06 shutdown: waiting for fs preallocator...
Fri Apr 29 18:39:06 shutdown: closing all files...
Fri Apr 29 18:39:06 closeAllFiles() finished
Fri Apr 29 18:39:06 dbexit: really exiting now

完了しました。