How to Start MongoDB from a Configuration File

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re new to working with MongoDB, it’s likely that you simply run it with all its default settings. By the time you’re ready to use MongoDB in a production environment, you’ll probably need to customize quite a few options. There’s no need to remember every single option you want to customize when you start MongoDB– all you need to do is set up the configuration from a file. With a configuration file, your settings can be reused over and over and saved in a repository so that you can easily remind yourself which configuration settings are needed. The only other thing you’ll need to know is the command needed to start MongoDB using that configuration file. In this article, we’ll show you how to construct a very simple MongoDB configuration file and how to start MongoDB with that configuration file.

A Simple Configuration File

The configuration file we’ll be looking at is very basic, as it’s just used for demonstration purposes. Let’s take a look at our sample configuration file, and then we’ll talk about what’s going on in it:

File: .mongodb.conf

1
2
3
# Start MongoDB on custom port 5555
port = 5555
logpath = mongodb.log

In this configuration file, two major changes have been made:

  • First, we’ve changed the port that MongoDB will run on from the default value of 27017 to port 5555.
  • We’ve also changed the logpath to a file called mongodb.log. This file doesn’t yet exist in our current directory, but it will be created automatically by MongoDB.

Naming Conventions

In our example, we named the configuration file .mongodb.conf, but you can use another name if desired. Note that the filename begins with a dot, which renders it invisible in certain operating systems such as OS X; in these cases, you’ll have to use special commands in the terminal ( eg. ls -a) or Finder to see the files.

Syntax

When you set options in the configuration file, the options and values are separated by an equals sign: for example, port = 5555. It’s important to spell each option exactly as stated in the documentation because the options are case sensitive.

Comments in Configuration File

Looking at our sample configuration file, you can see that we’re able to put comments in the file by using the # sign. When you begin a line with that sign, it renders the rest of the text on that line as a comment. It’s best to use comments extensively in your configuration file so that other developers can understand why you’re running MongoDB a certain way.

Starting MondoDB from the Configuration File

Now that we’ve customized our configuration file and specified how we want to start MongoDB, we need to be able to tell MongoDB to use it when starting up. Let’s take a look at the command we need to use, and then we’ll talk about how it works afterwards:

1
mongod --config .mongodb.conf

We’re using the standard mongod command, which gets the MongoDB daemon service running in the background. The --config flag is essential because it allows you to specify the configuration options from a file. The argument that follows the --config flag is the path to our MongoDB configuration file: .mongodb.conf. You can specify a full path here, but in our example, the configuration file exists in our current directory so the full path wasn’t necessary.

Verify the MongoDB Configuration

After we run this command, we need to make sure that MongoDB is running. This can be accomplished just by examining the output of the command and making sure there are no errors in the console stating that the process has exited. We can also check if MongoDB created the log files in our current directory.

1
2
3
4
$ ls -a
. mongodb.log mongodb.log.2019-05-29T15-47-47
.. mongodb.log.2019-05-29T15-13-00
.mongodb.conf mongodb.log.2019-05-29T15-13-26

As you can see in the command output above, when we list all the files in the current directory we see some new MongoDB log files. This confirms that MongoDB did start up with our configuration file and created a log with the name specified in the file.

Conclusion

Starting up MongoDB with its default settings can work well for simple deployments; however, when you start moving to more complex configurations and you have different environments for development, staging, and production, the ability to customize options becomes valuable. There are many options available when starting MongoDB; it’s easy and efficient to add them to a configuration file and put them in your repository to avoid issuing a lengthy mongod command. If you need any advice on how to handle your MongoDB database or customize your configuration please don’t hesitate to reach out to Object Rocket so that we can help.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.