MongoDB
database management
server status
troubleshooting
NoSQL

How to check if MongoDB is running (status commands)

Codemia Knowledge Hub ·

On Linux with systemd, run sudo systemctl status mongod. On Windows, run sc query MongoDB. A running service is half the answer, so then confirm the server accepts connections.

Service status on Linux

On systemd systems such as Ubuntu or CentOS, check the status:

bash
    sudo systemctl status mongod

The output shows whether MongoDB is active (running) or inactive (stopped), plus the process ID and recent log lines. Start it if needed:

bash
    sudo systemctl start mongod

Service status on Windows

MongoDB runs as a Windows service. Check its status:

 
    sc query MongoDB

The output includes a state such as RUNNING or STOPPED. Start it with:

 
    net start MongoDB

Confirming it accepts connections

Open the shell:

bash
    mongo

If the server is up, you get a prompt connected to the default test database. On MongoDB 6.0 and later the legacy mongo shell is no longer shipped; use mongosh instead.

From Python with pymongo:

python
1from pymongo import MongoClient
2
3try:
4    client = MongoClient('localhost', 27017)
5    # The ismaster command is cheap and does not require auth.
6    client.admin.command('ismaster')
7    print("MongoDB is running and connection is successful.")
8except Exception as e:
9    print("Unable to connect to MongoDB:", e)

If it is not running or you can't connect

  1. Configuration: check that mongod.conf has the right IP bindings and port.
  2. Logs: read /var/log/mongodb/mongod.log on Linux, or the log in the installation directory on Windows.
  3. Ports and firewall: make sure the MongoDB port (default 27017) is open through any firewall.
  4. Resources: make sure there is enough CPU and memory for the MongoDB processes.
StepDescriptionCommands/Tools
1Check MongoDB service statussystemctl status mongod sc query MongoDB
2Start MongoDB service if not runningsystemctl start mongod net start MongoDB
3Test MongoDB connectionmongo shell pymongo script
4Review logs for issuestail /var/log/mongodb/mongod.log
5Verify network settings and portsCheck firewalls and port bindings

Keep the configuration secure at the network and data level so the server is not open to unauthorized access.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design