{"id":4584,"date":"2019-05-11T12:43:04","date_gmt":"2019-05-11T07:13:04","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4584"},"modified":"2026-10-22T14:14:43","modified_gmt":"2026-10-22T08:44:43","slug":"mongodb-basic-operations","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/mongodb-basic-operations\/","title":{"rendered":"MONGODB BASIC OPERATIONS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong><strong>Post Date:<\/strong> May 11, 2019<br><strong>Last Updated:<\/strong> October 21, 2026<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.mongodb.com\/docs\/manual\/\" target=\"_blank\" rel=\"noopener\">MongoDB<\/a> 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you will understand how to perform basic MongoDB service management operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Start MongoDB on a Specific Port<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB uses port <strong>27017<\/strong> by default for client connections.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To start the MongoDB server on port 27017, execute the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod --port 27017<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mongod<\/code> starts the MongoDB database server.<\/li>\n\n\n\n<li><code>--port 27017<\/code> specifies the port on which MongoDB listens for connections.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To start MongoDB with a custom port, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod --port 27018<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The MongoDB data directory must exist and be accessible to the MongoDB process. If MongoDB cannot find its configured data directory, specify one using <code>--dbpath<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod --dbpath \/data\/db --port 27017<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command starts MongoDB using <code>\/data\/db<\/code> as its data directory and port <code>27017<\/code> for client connections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Stop MongoDB<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If MongoDB is running in the foreground in a terminal, press <code>Ctrl + C<\/code> to stop the server gracefully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If MongoDB is running as a background process on Linux, you can identify its process ID (PID) and stop it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Find the MongoDB Process<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ps -ef | grep '[m]ongod'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command displays running MongoDB server processes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongodb  12345  1  0 10:30 ?  00:00:05 mongod --port 27017<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>12345<\/code> is the process ID.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Stop the MongoDB Process<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kill 12345<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>12345<\/code> with the actual MongoDB process ID.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important:<\/strong> Avoid using <code>kill -9<\/code> unless absolutely necessary. A normal <code>kill<\/code> command sends a termination signal that allows MongoDB to shut down gracefully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production environments, prefer MongoDB&#8217;s supported shutdown mechanisms or the service manager responsible for running MongoDB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Find MongoDB Running with a Specific Path<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, MongoDB runs with a custom data directory or configuration. You can inspect the running process to identify its startup arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ps -ef | grep '[m]ongod'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongodb  12345  1  0 10:30 ?  00:00:05 mongod --dbpath \/new\/db\/path --port 27017<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">From this output, you can identify:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The MongoDB process ID.<\/li>\n\n\n\n<li>The configured data directory.<\/li>\n\n\n\n<li>The port used by the server.<\/li>\n\n\n\n<li>Additional startup arguments.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To stop the process, use its actual PID:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kill 12345<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If MongoDB is managed by a service manager, use that manager to stop it instead of terminating the process manually.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Configure MongoDB Using Homebrew<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Homebrew is a package manager commonly used on macOS to install and manage software.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you installed MongoDB through Homebrew, you can use Homebrew commands to manage the MongoDB service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For current installation instructions and supported package names, refer to the official <a href=\"https:\/\/www.mongodb.com\/docs\/manual\/installation\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">MongoDB installation documentation<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the MongoDB Data Directory<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB requires a data directory to store database files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To create the directory <code>\/data\/db<\/code>, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo mkdir -p \/data\/db<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Assign ownership to your current user:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo chown \"$(whoami)\" \/data\/db<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These commands create the directory if it does not exist and allow your current user to write to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> On macOS, <code>\/data\/db<\/code> is not necessarily the best location for database files. You can use a directory under your home folder, such as <code>~\/mongodb-data<\/code>, to avoid some permission and filesystem issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir -p ~\/mongodb-data<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Start MongoDB<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To start MongoDB manually with its default configuration, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB will use its configured data directory and default port unless you specify different settings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use a custom data directory, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod --dbpath \"$HOME\/mongodb-data\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To specify both the data directory and port, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod --dbpath \"$HOME\/mongodb-data\" --port 27017<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the terminal open while MongoDB is running in the foreground.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important:<\/strong> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Start and Stop MongoDB Using Homebrew<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Homebrew provides service-management commands that can start, stop, and inspect supported services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The exact MongoDB service name depends on the installed formula. For example, it may be <code>mongodb-community<\/code> or a version-specific formula such as <code>mongodb-community@8.0<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the example service name below with the formula installed on your system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: List MongoDB Services<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services list | grep mongodb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command displays Homebrew services whose names contain <code>mongodb<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongodb-community started<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output helps you determine whether the service is running.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Start MongoDB<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To start the service, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services start mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command starts the MongoDB service using Homebrew.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your installed formula has a different name, use that name instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Stop MongoDB<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To stop the service, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services stop mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This stops the MongoDB service managed by Homebrew.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Correction:<\/strong> Use <code>brew services stop<\/code> to stop the service, not <code>brew services start<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Restart MongoDB<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To restart the service, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services restart mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is useful when MongoDB needs to be restarted after a configuration change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Verify the Service Status<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After starting or stopping MongoDB, check its status:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services list | grep mongodb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also check whether a process is listening on port 27017:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lsof -nP -iTCP:27017 -sTCP:LISTEN<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If MongoDB is listening on that port, the command should display the process associated with the listening socket.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. MongoDB Basic Operations Workflow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following workflow illustrates the basic process of configuring, starting, checking, and stopping MongoDB.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Workflow Steps<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Configure the data directory:<\/strong> Create a directory where MongoDB can store database files.<\/li>\n\n\n\n<li><strong>Start MongoDB:<\/strong> Run <code>mongod<\/code> manually or start the Homebrew service.<\/li>\n\n\n\n<li><strong>Verify the server:<\/strong> Check the running process or confirm that MongoDB is listening on the expected port.<\/li>\n\n\n\n<li><strong>Connect to MongoDB:<\/strong> Use MongoDB Shell (<code>mongosh<\/code>) to connect to the server.<\/li>\n\n\n\n<li><strong>Stop MongoDB:<\/strong> Use <code>Ctrl + C<\/code>, the appropriate service-management command, or another supported shutdown method.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example Workflow Commands<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For a manual MongoDB installation:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Create the data directory\nmkdir -p ~\/mongodb-data\n\n# Start MongoDB\nmongod --dbpath \"$HOME\/mongodb-data\" --port 27017<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open another terminal and verify the process:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ps -ef | grep '[m]ongod'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To connect using MongoDB Shell, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongosh \"mongodb:\/\/127.0.0.1:27017\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To stop the manually started server, return to the terminal where <code>mongod<\/code> is running and press <code>Ctrl + C<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a Homebrew-managed installation, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services start mongodb-community\nbrew services list | grep mongodb\nbrew services stop mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Remember to replace <code>mongodb-community<\/code> with the actual installed service name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Troubleshooting Common MongoDB Issues<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Issue 1: MongoDB Fails to Start<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible causes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The data directory does not exist.<\/li>\n\n\n\n<li>The current user does not have permission to access the directory.<\/li>\n\n\n\n<li>Another MongoDB process is already running.<\/li>\n\n\n\n<li>The configured port is already in use.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the data directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ls -ld ~\/mongodb-data<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check whether port 27017 is in use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lsof -nP -iTCP:27017 -sTCP:LISTEN<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Resolve the underlying issue before starting MongoDB again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Issue 2: MongoDB Service Is Not Found<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If Homebrew reports that the MongoDB service is unavailable, check the installed packages:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew list --formula | grep mongodb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the output contains a version-specific formula, use that exact formula name with <code>brew services<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Issue 3: MongoDB Does Not Stop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, check whether MongoDB is managed by Homebrew:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services list | grep mongodb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If it is managed by Homebrew, stop it using:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services stop mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you started MongoDB manually, stop it from its terminal using <code>Ctrl + C<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid killing processes indiscriminately because multiple MongoDB instances may be running.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB service management is an essential skill for developers, system administrators, and DevOps engineers working with MongoDB databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We also explored common troubleshooting techniques and answered frequently asked questions about MongoDB service management.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By understanding these basic operations, you can manage your local MongoDB environment more effectively and troubleshoot common startup and shutdown issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Next Step:<\/strong> Once MongoDB is running, you can use MongoDB Shell (<code>mongosh<\/code>) to create databases, insert documents, query records, update documents, and delete data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more information, refer to the official <a href=\"https:\/\/www.mongodb.com\/docs\/manual\/\" target=\"_blank\" rel=\"noopener\">MongoDB Manual<\/a> and <a href=\"https:\/\/docs.brew.sh\" target=\"_blank\" rel=\"noopener\">Homebrew documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions (FAQ)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the default port for MongoDB?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB uses port <strong>27017<\/strong> by default for client connections. You can specify a different port using the <code>--port<\/code> option when starting the server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongod --port 27018<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. How can I check whether MongoDB is running?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On Linux, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ps -ef | grep '[m]ongod'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On macOS, if MongoDB is managed by Homebrew, execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services list | grep mongodb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also check whether a process is listening on the default port:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lsof -nP -iTCP:27017 -sTCP:LISTEN<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. How can I start and stop MongoDB using Homebrew?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following commands, replacing <code>mongodb-community<\/code> with your installed formula name:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Start MongoDB:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services start mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Stop MongoDB:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services stop mongodb-community<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Check the service status:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew services list | grep mongodb<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-enable-the-mongo-database-authentication-method\/\">How to enable the mongo Database authentication method<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-secure-mongodb-databases\/\">How to secure MongoDB databases<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Need Help with MongoDB Configuration and Management?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Get in touch with our team <\/strong><a href=\"https:\/\/pheonixsolutions.com\/contact\"><strong>here<\/strong><\/a><strong>.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: May 11, 2019Last Updated: October 21, 2026 Introduction MongoDB is a popular NoSQL database that stores data in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12417,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[180],"class_list":["post-4584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-architecture","tag-database","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1bW","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/091d7311-262e-4303-b54f-43a446d34ea7.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4584"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4584\/revisions"}],"predecessor-version":[{"id":12438,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4584\/revisions\/12438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/12417"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}