Installing MongoDB Community Server Locally

A local MongoDB server gives you direct control over the database process and is useful for learning without depending on a remote deployment.

Step 1: Download MongoDB Community Server

Visit the official download page:

https://www.mongodb.com/try/download/community

Select the appropriate operating system and installation package. Follow the installation instructions for your platform.

Step 2: Install mongosh

MongoDB Shell is distributed separately from the database server in many installation workflows. Verify that mongosh is installed and available in your terminal.

mongosh --version

If the terminal reports that the command cannot be found, check the installation and PATH configuration.

Step 3: Start the database server

The startup procedure depends on the operating system and installation method.

On systems configured with a service manager, MongoDB may run as a service. On other setups, you may need to start the server process manually with a configured data directory.

Do not assume the server is running simply because the installation completed.

Step 4: Connect to the server

If the server is running locally on the default port and does not require authentication, connect using:

mongosh "mongodb://127.0.0.1:27017"

127.0.0.1 refers to the local machine. The default MongoDB server port is 27017 unless the deployment has been configured differently.

Step 5: Verify the server

db.runCommand({ ping: 1 });

If the connection is successful, continue with the practice database:

use mongodb_course_lab

db.installationCheck.insertOne({
 status: "ready",
 createdAt: new Date()
});

Understanding server and shell processes

The database server and shell are different processes:

  • mongod is the MongoDB database server process.
  • mongosh is the interactive shell client.

Starting mongosh does not necessarily start mongod. If the server is stopped or unreachable, the shell cannot establish a connection.

Common local connection problems

If a connection fails, check whether the server is running, whether the port matches the server configuration, whether a firewall is blocking the connection, and whether authentication is required.

Also verify that the server is using the data directory you expect. Accidentally starting multiple database processes with conflicting ports or data directories can cause confusion and operational problems.

Check your local MongoDB server
db.runCommand({ ping: 1 });

db.serverStatus().version;

db.getMongo();
💡 The ping command verifies that the server responds. serverStatus() returns server status information, including the version, when the user has permission to run it. getMongo() displays the current connection object in mongosh. Some diagnostic commands require additional privileges.
Key Takeaways
  • MongoDB Community Server can run on your local machine.
  • mongod is the database server process; mongosh is the shell client.
  • The default MongoDB server port is 27017.
  • The server must be running before a shell client can connect.
  • Installation and startup procedures differ across operating systems.
Knowledge Check

What is the primary difference between mongod and mongosh?