In the previous post we have seen that how to install and configure mongo database server and how to connect to it using interactive javascript client.If you have read my previous post you will notice that while starting the mongo database server using mongod we are passing the location of where our data is going to reside using the –dbpath argument like this.
mongod --dbpath C:sauravmongodatadb
Actually this is not the ideal way of doing this and moreover not an recommended way because every time passing the dbpath location is an extra overhead.So, here comes the config file as a resuce, we are going to define this dbpath property and some other property in the config file such that mongod will read this config file while starting mongo database server.
Below is the list of steps that will explain how to read from a configuration file.
- Create a configuration file mongod.cfg in any location of your choice but i will recommend it to keep in the same location where u have extracted the contents of mongo zip file. In my case my config file is present here –
mongod.exe --config C:sauravmongomongod.cfg
- Add the following contents in your cfg file.
#Location where my data will reside dbpath=C:sauravmongodbdata #Location of my log file logpath=C:sauravmongomongod.log
- Now start mongo database server and pass this config file as an argument like this –
mongod.exe --config C:sauravmongomongod.cfg
Now when you execute this command you will see something like this in your command prompt.
Install MongoDB as a Windows Service-1 You will notice that all the logs are redirected to the location that we had defined in cfg file.
Now you might be wondering that , how does it help instead of passing dbpath location we are passing the config file location , it is the same thing an extra overhead is still their. Answer to this problem is in next step. - Now we will install Mongodb as a windows service , so that whenever our system reboots it will start mongod as a windows service. As per the mongodb docs you can install it using –install.
From your command prompt execute this command.mongod --config C:sauravmongomongod.cfg --install
Note : Make sure you open this command prompt instance as an adminstrator otherwise you will get an “Access Denied Exception”.
Trying to install Windows service 'MongoDB' Error connecting to the Service Control Manager: Access is denied. (5)
Once you execute this command you will see this message in your cmd console.
Install MongoDB as a Windows Service-2 You can also see MongoDB service in the task manager.By default it will be in stopped state.After installing mongod as a windows service by executing this command.
net start MongoDB
This command will start MongoDB as a windows service ,as you can see from the below snapshot.
Install MongoDB as a Windows Service-3 For stopping mongod as a windows service you can execute this command –
net stop MongoDB
So , now we are all set and configured mongod as a windows service.
Related Posts
Saurabh Jain
Latest posts by Saurabh Jain (see all)
- java.lang.IncompatibleClassChangeError: Found interface org.apache.hadoop.mapreduce.TaskInputOutputContext, but class was expected - August 8, 2014
- org.datanucleus.store.rdbms.exceptions.MappedDatastoreException: INSERT INTO “TABLE_PARAMS” – Hive with Kite Morphlines - July 17, 2014
- java.io.IOException: can not read class parquet.format.PageHeader: null – Hive - July 12, 2014
One thought on “Install MongoDB as a Windows Service”