MONGODB BASIC OPERATIONS

MongoDB basic operations workflow showing how to start, stop, and manage MongoDB services, configure the data directory, check server status, and manage services using Homebrew.

Post Date: May 11, 2019
Last Updated: October 21, 2026

Introduction

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. It is widely used in modern applications because of its scalability, flexible data model, and efficient data management capabilities.

Before working with MongoDB, it is important to understand its basic operations, including starting and stopping the MongoDB server, configuring the database directory, running MongoDB on a specific port, and managing MongoDB services using Homebrew.

In this tutorial, we will explore the essential MongoDB commands required to manage a MongoDB server on Linux and macOS. We will also learn how to configure a custom database path, check running MongoDB processes, and start or stop MongoDB services using Homebrew.

By the end of this tutorial, you will understand how to perform basic MongoDB service management operations.

1. Start MongoDB on a Specific Port

MongoDB uses port 27017 by default for client connections.

To start the MongoDB server on port 27017, execute the following command:

mongod --port 27017

Here:

  • mongod starts the MongoDB database server.
  • --port 27017 specifies the port on which MongoDB listens for connections.

To start MongoDB with a custom port, use:

mongod --port 27018

Note: The MongoDB data directory must exist and be accessible to the MongoDB process. If MongoDB cannot find its configured data directory, specify one using --dbpath.

For example:

mongod --dbpath /data/db --port 27017

This command starts MongoDB using /data/db as its data directory and port 27017 for client connections.

2. Stop MongoDB

If MongoDB is running in the foreground in a terminal, press Ctrl + C to stop the server gracefully.

If MongoDB is running as a background process on Linux, you can identify its process ID (PID) and stop it.

Step 1: Find the MongoDB Process

Execute:

ps -ef | grep '[m]ongod'

This command displays running MongoDB server processes.

Example output:

mongodb  12345  1  0 10:30 ?  00:00:05 mongod --port 27017

In this example, 12345 is the process ID.

Step 2: Stop the MongoDB Process

Use the following command:

kill 12345

Replace 12345 with the actual MongoDB process ID.

Important: Avoid using kill -9 unless absolutely necessary. A normal kill command sends a termination signal that allows MongoDB to shut down gracefully.

For production environments, prefer MongoDB’s supported shutdown mechanisms or the service manager responsible for running MongoDB.

3. Find MongoDB Running with a Specific Path

Sometimes, MongoDB runs with a custom data directory or configuration. You can inspect the running process to identify its startup arguments.

Execute:

ps -ef | grep '[m]ongod'

Example output:

mongodb  12345  1  0 10:30 ?  00:00:05 mongod --dbpath /new/db/path --port 27017

From this output, you can identify:

  • The MongoDB process ID.
  • The configured data directory.
  • The port used by the server.
  • Additional startup arguments.

To stop the process, use its actual PID:

kill 12345

If MongoDB is managed by a service manager, use that manager to stop it instead of terminating the process manually.

4. Configure MongoDB Using Homebrew

Homebrew is a package manager commonly used on macOS to install and manage software.

If you installed MongoDB through Homebrew, you can use Homebrew commands to manage the MongoDB service.

For current installation instructions and supported package names, refer to the official MongoDB installation documentation.

Step 1: Create the MongoDB Data Directory

MongoDB requires a data directory to store database files.

To create the directory /data/db, execute:

sudo mkdir -p /data/db

Assign ownership to your current user:

sudo chown "$(whoami)" /data/db

These commands create the directory if it does not exist and allow your current user to write to it.

Note: On macOS, /data/db is not necessarily the best location for database files. You can use a directory under your home folder, such as ~/mongodb-data, to avoid some permission and filesystem issues.

For example:

mkdir -p ~/mongodb-data

Step 2: Start MongoDB

To start MongoDB manually with its default configuration, execute:

mongod

MongoDB will use its configured data directory and default port unless you specify different settings.

To use a custom data directory, execute:

mongod --dbpath "$HOME/mongodb-data"

To specify both the data directory and port, execute:

mongod --dbpath "$HOME/mongodb-data" --port 27017

Keep the terminal open while MongoDB is running in the foreground.

Important: If MongoDB is already running as a service on port 27017, starting another instance on the same port may fail. Stop the existing service first or configure a different port and data directory.

5. Start and Stop MongoDB Using Homebrew

Homebrew provides service-management commands that can start, stop, and inspect supported services.

The exact MongoDB service name depends on the installed formula. For example, it may be mongodb-community or a version-specific formula such as mongodb-community@8.0.

Replace the example service name below with the formula installed on your system.

Step 1: List MongoDB Services

Execute:

brew services list | grep mongodb

This command displays Homebrew services whose names contain mongodb.

Example output:

mongodb-community started

The output helps you determine whether the service is running.

Step 2: Start MongoDB

To start the service, execute:

brew services start mongodb-community

This command starts the MongoDB service using Homebrew.

If your installed formula has a different name, use that name instead.

Step 3: Stop MongoDB

To stop the service, execute:

brew services stop mongodb-community

This stops the MongoDB service managed by Homebrew.

Correction: Use brew services stop to stop the service, not brew services start.

Step 4: Restart MongoDB

To restart the service, execute:

brew services restart mongodb-community

This is useful when MongoDB needs to be restarted after a configuration change.

Step 5: Verify the Service Status

After starting or stopping MongoDB, check its status:

brew services list | grep mongodb

You can also check whether a process is listening on port 27017:

lsof -nP -iTCP:27017 -sTCP:LISTEN

If MongoDB is listening on that port, the command should display the process associated with the listening socket.

6. MongoDB Basic Operations Workflow

The following workflow illustrates the basic process of configuring, starting, checking, and stopping MongoDB.

Workflow Steps

  1. Configure the data directory: Create a directory where MongoDB can store database files.
  2. Start MongoDB: Run mongod manually or start the Homebrew service.
  3. Verify the server: Check the running process or confirm that MongoDB is listening on the expected port.
  4. Connect to MongoDB: Use MongoDB Shell (mongosh) to connect to the server.
  5. Stop MongoDB: Use Ctrl + C, the appropriate service-management command, or another supported shutdown method.

Example Workflow Commands

For a manual MongoDB installation:

# Create the data directory
mkdir -p ~/mongodb-data

# Start MongoDB
mongod --dbpath "$HOME/mongodb-data" --port 27017

Open another terminal and verify the process:

ps -ef | grep '[m]ongod'

To connect using MongoDB Shell, execute:

mongosh "mongodb://127.0.0.1:27017"

To stop the manually started server, return to the terminal where mongod is running and press Ctrl + C.

For a Homebrew-managed installation, use:

brew services start mongodb-community
brew services list | grep mongodb
brew services stop mongodb-community

Remember to replace mongodb-community with the actual installed service name.

7. Troubleshooting Common MongoDB Issues

Issue 1: MongoDB Fails to Start

Possible causes:

  • The data directory does not exist.
  • The current user does not have permission to access the directory.
  • Another MongoDB process is already running.
  • The configured port is already in use.

Solution:

Check the data directory:

ls -ld ~/mongodb-data

Check whether port 27017 is in use:

lsof -nP -iTCP:27017 -sTCP:LISTEN

Resolve the underlying issue before starting MongoDB again.

Issue 2: MongoDB Service Is Not Found

If Homebrew reports that the MongoDB service is unavailable, check the installed packages:

brew list --formula | grep mongodb

If the output contains a version-specific formula, use that exact formula name with brew services.

Issue 3: MongoDB Does Not Stop

First, check whether MongoDB is managed by Homebrew:

brew services list | grep mongodb

If it is managed by Homebrew, stop it using:

brew services stop mongodb-community

If you started MongoDB manually, stop it from its terminal using Ctrl + C.

Avoid killing processes indiscriminately because multiple MongoDB instances may be running.

Conclusion

MongoDB service management is an essential skill for developers, system administrators, and DevOps engineers working with MongoDB databases.

In this tutorial, we covered how to start MongoDB on a specific port, configure a data directory, identify running MongoDB processes, stop MongoDB safely, and manage MongoDB services using Homebrew.

We also explored common troubleshooting techniques and answered frequently asked questions about MongoDB service management.

By understanding these basic operations, you can manage your local MongoDB environment more effectively and troubleshoot common startup and shutdown issues.

Next Step: Once MongoDB is running, you can use MongoDB Shell (mongosh) to create databases, insert documents, query records, update documents, and delete data.

For more information, refer to the official MongoDB Manual and Homebrew documentation.

Frequently Asked Questions (FAQ)

1. What is the default port for MongoDB?

MongoDB uses port 27017 by default for client connections. You can specify a different port using the --port option when starting the server.

Example:

mongod --port 27018

2. How can I check whether MongoDB is running?

On Linux, execute:

ps -ef | grep '[m]ongod'

On macOS, if MongoDB is managed by Homebrew, execute:

brew services list | grep mongodb

You can also check whether a process is listening on the default port:

lsof -nP -iTCP:27017 -sTCP:LISTEN

3. How can I start and stop MongoDB using Homebrew?

Use the following commands, replacing mongodb-community with your installed formula name:

Start MongoDB:

brew services start mongodb-community

Stop MongoDB:

brew services stop mongodb-community

Check the service status:

brew services list | grep mongodb

How to enable the mongo Database authentication method

How to secure MongoDB databases

Need Help with MongoDB Configuration and Management?

Need help with MongoDB installation, database configuration, service management, or server administration? Our experts can assist with MongoDB setup, database performance optimization, Linux server administration, security hardening, and cloud database hosting solutions.

Get in touch with our team here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top