
In this tutorial, we will show you how to start the MongoDB server with a configuration file on Windows.
Tools and Technologies
- Microsoft Windows 7
- MongoDB Community Edition 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"
2. Start MongoDB
Start the MongoDB server using the 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 the MongoDB Shell:
> use admin
> db.runCommand({getCmdLineOpts: 1})
The output will look something like this:
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 a configuration file on Windows.