Starting MongoDB with Configuration File

MongoDB

In this tutorial, we will show you how to start the MongoDB server with configuration file on Windows.

What We Used

  • Microsoft Windows 7
  • MongoDB 3.2.9
  • Notepad Text Editor

1. Create Configuration File

Create a text file and name it as mongodb.conf. In this guide, we create the configuration file at C:\mongodb\conf\mongodb.conf. Edit the file and specify the configuration options that you required in the configuration file for MongoDB server startup.

mongodb.conf

net:
    # MongoDB server listening port
    port: 27018
storage:
    # Data store directory
    dbPath: "C:\\mongodb\\data\\db"
    mmapv1:
        # Reduce data files size and journal files size
        smallFiles: true
systemLog:
    # Write logs to log file
    destination: file
    path: "C:\\mongodb\\logs\\mongodb.log"

Note: MongoDB configuration file is written in YAML format, you have to use SPACE characters instead of TAB characters for indentation in configuration file because YAML doen’t support TAB characters.

2. Start MongoDB

Start the MongoDB server using configuration file with the --config option or -f option.

mongod --config C:\mongodb\conf\mongodb.conf

Or

mongod -f C:\mongodb\conf\mongodb.conf

3. Connect MongoDB

Connect to MongoDB via MongoDB shell. Open another Command Prompt window and execute the following command:

mongo --port 27018

4. Verify Configuration Options

This is an optional step for you to check what configuration options have been used to start the MongoDB server. Execute the following command in MongoDB shell:

> use admin
> db.runCommand({getCmdLineOpts: 1})

The output will look something like:

C:\Users\chankok>mongo --port 27018
MongoDB shell version: 3.2.9
connecting to: localhost:27018/test
> use admin
switched to db admin
> db.runCommand({getCmdLineOpts: 1})
{
        "argv" : [
                "mongod",
                "--config",
                "C:\\mongodb\\conf\\mongodb.conf"
        ],
        "parsed" : {
                "config" : "C:\\mongodb\\conf\\mongodb.conf",
                "net" : {
                        "port" : 27018
                },
                "storage" : {
                        "dbPath" : "C:\\mongodb\\data\\db",
                        "mmapv1" : {
                                "smallFiles" : true
                        }
                },
                "systemLog" : {
                        "destination" : "file",
                        "path" : "C:\\mongodb\\logs\\mongodb.log"
                }
        },
        "ok" : 1
}
> _

Summary

Congratulation! You have learned how to start the MongoDB server using configuration file on Windows.

Other Resources