Start MongoDB Server from Command Prompt
MongoDB Community Server can run either as an operating-system service or as a foreground mongod process. The commands depend on how MongoDB was installed. The examples below cover Windows, macOS, and Linux, and also show how to verify, connect to, and stop the server.
Windows Users
Assuming that you installed MongoDB Server with default options, especially the installation folder as C:\Program Files\MongoDB\Server\4.0. Inside this folder, you have the bin directory containing mongod.exe.
Also assuming that the database path is: C:\data\db\
To start MongoDB Server in Windows, start Mongo Daemon (mongod.exe) using the following command:
C:\> "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"
Note that the program we are running is mongod.exe and not mongo.exe.
mongo.exe is used to start Mongo Shell, while mongod.exe is used to run Mongo Server.
C:\>"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"
2018-10-10T11:02:43.496+0530 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-10-10T11:02:43.799+0530 I CONTROL [initandlisten] MongoDB starting : pid=11716 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-QRVE3I1
2018-10-10T11:02:43.800+0530 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-10-10T11:02:43.801+0530 I CONTROL [initandlisten] db version v4.0.3
2018-10-10T11:02:43.804+0530 I CONTROL [initandlisten] git version: 7ea530946fa7880364d88c8d8b6026bbc9ffa48c
2018-10-10T11:02:43.804+0530 I CONTROL [initandlisten] allocator: tcmalloc
2018-10-10T11:02:43.805+0530 I CONTROL [initandlisten] modules: none
2018-10-10T11:02:43.805+0530 I CONTROL [initandlisten] build environment:
2018-10-10T11:02:43.806+0530 I CONTROL [initandlisten] distmod: 2008plus-ssl
2018-10-10T11:02:43.807+0530 I CONTROL [initandlisten] distarch: x86_64
2018-10-10T11:02:43.807+0530 I CONTROL [initandlisten] target_arch: x86_64
2018-10-10T11:02:43.807+0530 I CONTROL [initandlisten] options: {}
2018-10-10T11:02:43.809+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7612M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-10-10T11:02:43.840+0530 I STORAGE [initandlisten] WiredTiger message [1539149563:839654][11716:140703174245456], txn-recover: Set global recovery timestamp: 0
2018-10-10T11:02:43.849+0530 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-10-10T11:02:43.874+0530 I CONTROL [initandlisten]
2018-10-10T11:02:43.875+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-10-10T11:02:43.875+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-10-10T11:02:43.878+0530 I CONTROL [initandlisten]
2018-10-10T11:02:43.878+0530 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-10-10T11:02:43.879+0530 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-10-10T11:02:43.879+0530 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-10-10T11:02:43.879+0530 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-10-10T11:02:43.880+0530 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-10-10T11:02:43.880+0530 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-10-10T11:02:43.880+0530 I CONTROL [initandlisten]
2018-10-10T11:02:43.881+0530 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: 0bf57dd0-b7c8-41da-8ddb-039c9fcf7c98
2018-10-10T11:02:43.899+0530 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2018-10-10T11:02:43.903+0530 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 8102662d-18e5-4490-a9d5-696462810c13
2018-10-10T11:02:44.064+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data'
2018-10-10T11:02:44.067+0530 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 34b765ee-96c7-456b-9864-bfc5d53c1a22
2018-10-10T11:02:44.067+0530 I NETWORK [initandlisten] waiting for connections on port 27017
2018-10-10T11:02:44.094+0530 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2018-10-10T11:02:44.094+0530 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-10-10T11:02:44.099+0530 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
The MongoDB Server has started successfully.
From the messages logged to the console, you can observe that:
- Mongo Server is started as a process with process id (pid): 11716.
- Mongo Server is listening on the default port number
27017. The final log line states that it is waiting for connections on port 27017. - Mongo Server is using the database present at the location
C:\data\db\.
Do not close this Command Prompt window.
Now, you can connect to this server as clients from other Command Prompt windows.
Start MongoDB with an Explicit Database Path on Windows
If your data directory is not defined in a configuration file, pass it with --dbpath. Create the directory first because mongod cannot use a path that does not exist.
mkdir C:\data\db
"C:\Program Files\MongoDB\Server\8.0\bin\mongod.exe" --dbpath "C:\data\db"
Replace 8.0 with the version installed on your computer. You can also open the MongoDB bin directory first and then run mongod.exe --dbpath "C:\data\db".
Start MongoDB as a Windows Service
If MongoDB was installed as a Windows service, start it from an elevated Command Prompt. The default service name is commonly MongoDB, although it can differ when a custom name was chosen during installation.
net start MongoDB
To check the service state, use:
sc query MongoDB
To stop the Windows service, run:
net stop MongoDB
Start MongoDB Server on macOS
For a Homebrew installation, start MongoDB as a background service with the formula name installed on your Mac. The exact suffix can vary by MongoDB version.
brew services list
brew services start mongodb-community
If the installed formula includes a version suffix, use that name, for example mongodb-community@8.0. To stop the service, replace start with stop.
Start MongoDB Server on Ubuntu and Other Linux Systems
Package-based MongoDB installations usually run through systemd. Start the service and then inspect its status:
sudo systemctl start mongod
sudo systemctl status mongod
Enable MongoDB to start automatically when the computer boots:
sudo systemctl enable mongod
Stop or restart the service with:
sudo systemctl stop mongod
sudo systemctl restart mongod
If the unit is not found, confirm whether your package created a service named mongod or mongodb. Official MongoDB packages normally use mongod.
Verify the MongoDB Server and Connect with mongosh
A successful startup normally ends with a message indicating that the server is waiting for connections. Open another terminal and connect to the local server with MongoDB Shell:
mongosh "mongodb://127.0.0.1:27017"
After connecting, run a simple command to confirm that the server responds:
db.runCommand({ ping: 1 })
A successful response includes ok: 1. The older mongo shell shown in legacy tutorials has been replaced by mongosh in current MongoDB installations.
Start MongoDB for a VS Code Connection
The MongoDB extension for Visual Studio Code connects to a running MongoDB deployment; it does not replace the database server. Start mongod or the MongoDB service first, then connect from VS Code with a local connection string such as mongodb://127.0.0.1:27017.
Stop a Foreground MongoDB Server Safely
When mongod is running in the current terminal, press Ctrl+C in that same terminal. MongoDB then performs a controlled shutdown. For service-based installations, use the matching service command shown above instead of terminating the process from Task Manager or with a force-kill command.
Fix Common MongoDB Startup Errors
MongoDB Reports That the Data Directory Does Not Exist
Create the directory specified by --dbpath, or correct the path in the MongoDB configuration file. Also confirm that the account running MongoDB has read and write permission for that directory.
MongoDB Cannot Bind to Port 27017
Another MongoDB process or application may already be using the port. Check whether MongoDB is already running before starting a second instance. When multiple instances are intentional, assign a different port and a separate data directory.
mongod --dbpath "/path/to/another/db" --port 27018
The MongoDB Service Fails to Start
Read the service status and MongoDB log file for the exact reason. Typical causes include an invalid configuration value, an unavailable data path, insufficient file permissions, or a port conflict.
sudo systemctl status mongod
sudo journalctl -u mongod --no-pager -n 100
mongod Is Not Recognized as a Command
Run mongod with its full executable path or add the MongoDB bin directory to your system PATH. On Windows, also verify the installed version directory under C:\Program Files\MongoDB\Server\.
MongoDB Server Startup FAQs
How do I start MongoDB from Command Prompt?
Run mongod.exe from the MongoDB bin directory, optionally with --dbpath. If MongoDB is installed as a Windows service, run net start MongoDB from an elevated Command Prompt.
How do I stop and start a MongoDB server?
Use Ctrl+C for a foreground mongod process. For a service, use net start/net stop on Windows, brew services start/brew services stop on macOS, or systemctl start/systemctl stop on Linux.
How do I know whether MongoDB is running?
Check the service status or connect with mongosh "mongodb://127.0.0.1:27017". A successful db.runCommand({ ping: 1 }) result confirms that the server is accepting requests.
Does VS Code start the MongoDB server?
No. The MongoDB VS Code extension connects to a deployment that is already running. Start the local MongoDB server or connect the extension to a remote deployment first.
Which port does MongoDB use by default?
A standalone MongoDB server uses TCP port 27017 by default. A custom port can be set in the configuration file or with the --port option.
Editorial QA Checklist for Starting MongoDB Server
- Confirm that each command matches the operating system and installation method.
- Verify the installed MongoDB version before using a version-specific executable path or Homebrew formula.
- Check that the configured database directory exists and is writable by the MongoDB process.
- Confirm that the startup log reports port
27017, or document the custom port being used. - Test the connection with
mongoshand a ping command before continuing with application development.
TutorialKart.com