Configuring MongoDB as Windows Service for MVC applications

Download sample MongoDB configuration file

In previous post Installing MongoDB for MVC applications, I discussed the very first step of installing MongoDB database in your environment. In production environment you are not going to be using your database as a console application with whatever default settings it is shipped with. For production environment you want control on various run time options. MongoDB documents have lot of details on this topic. In this post I will highlight topics that may or may not be very clear in the documentation. In this post I will focus on following topics.

  • How to specify run time MongoDB configuration parameters
  • How to run MongoDB as windows service

MongoDB Configuration

In previous post I mentioned that when you try to execute Mongod.exe from command line without any parameters, it complains that there is no data folder specified. Then I specified that value using dbpath option on command line. In production environment you are going to have a lot of configuration parameters. It is not going to be practical to specify all those parameters on command line. MongoDB allows you to specify all these parameters in a configuration file. This configuration is nothing but a text file and you can put all your configuration values in that file on separate lines. You can get all the details of this configuration file from Run-time Database Configuration page in MongoDB documentation. Following link on MongoDB web site provides all the parameters that you can configure for your database.

For this docussion I will focus on very basic options that you will need to get your database configured for most situations.

  • dbpath: This settings allow you to specify folder location where MongoDB will create all data files. For example in my case I have set this value to dbpath=c:\Development\MongoDatabase\data. This path does not have to be on your local machine. It can be a network folder as long as MongoDB engine has read and write access rights on it.
  • logpath: This settings allow you to specify log files where MongoDB will log actions, errors etc. For example in my case this value looks like logpath=c:\Development\MongoDatabase\log\MongoLog.txt. It is very important to note that this value is a file name and not folder name. If you do not specify file name, MongoDB will not be able to start. This configuration is optional. But I will highly recommend that you do specify a log path for your installation. This will come very handy in debugging any issues.
  • verbose: This setting allows you to control verbosity of log file. By default this value is set to false. This means that only important actions are recorded. If you are using log file for your installation then you can use this setting. If you are not using log file, then this setting will be ignored.
  • v,vv,vvv,vvvv,vvvvv: These five settings allow you to set level of verbosity for your log file. When you are running into problems or want more details about internal operations, you can increase the verbosity level accordingly.
  • logappend: This setting decides if log file is going to append new entries to end of file when database restarts or new file is going to be created. The default value of this setting is false. This means that every time you restart MongoDB, your previous log entries will be lost. The value of this setting is going to depend on your need. I keep this value set to true to allow debugging of old issues that may have happened. Other option you have is to back up your log files. Since this file can grow to large size, it is reasonable to truncate the file when database restarts. You will have to make a judgement call here.
  • port: By default MongoDB listens on port 27017 (may differ for your installation). But if that port is already in use, then this setting can be used to configure MongoDB to listen on different port. I will strongly recommend that you do specify a different port than default. This is good security practice for run time operation of any database. If you leave your database running on standard port, then you are more open to network attacks by standard hacking scripts.
  • diaglog: This setting allows you to specify if you want all read and write operations logged for diagnostic purposes. Default value of this setting is 0. This means no operations are logged. But if you are having some CRUD issues then you can change this value to one of these values, (0,1,2, 3,7).

I have attached sample MongoDB configuration file with thise post that I have used for my prototype development.

Running MongoDB as Windows service

Like any other database, you would want that your database starts running when machine starts. And definitely it will be preferable that database is running as a background process and not a console application. MongoDB can be configured as a windows service.

Install MongoDB on Windows page in documentation at the bottom specifies steps to follow to run MongoDB as Windows service. This is where the configuration file that we created is going to come handy. Following command installs MongodB as service.

mongod.exe --config C:\mongodb\mongod.cfg --install

Config command line parameter allows you to specify location of configuration file.

Important:It is very important that command line shell is running under administrator account when you install the service. Windows needs elevated privileges to install a windows service. If you do not run as administrator then installation will throw an error with error telling that access is denied.

When you execute above command, windows will pop a dialog box telling you that MongoD is making changes to your system configuration. It will ask for permission. Go ahead and click on "Allow" button to proceed with installation. Now you will see an entry for MongoDB in your services list.

mongodb as windows service

As you can see from above screenshot, now you can finish service configuration. Change service Startup type to Automatic if you want MongoDB to start when operating system starts. This interface will allow you to configure account that should be used to run MongoDB service. By default it will use Local System Account. I will recommend that you create a new windows user account with controlled rights and then configure MongoDB to use that account. This is good security practice.

Now your MongoDB is configured as windows service. You can start the service either from command line using net start mongod command or from user interface by clicking on Start button.

Now that we have MongoDB all up and running for prime time operations, in next post I will show some basic operations we can perform from our MVC application.

comments powered by Disqus

Search

Social

Weather

4.9 °C / 40.8 °F

weather conditions Clear

Monthly Posts

Blog Tags