In this chapter, we will cover everything you need to know about installing and setting up MongoDB on various platforms. Whether you are using Windows, macOS, or Linux, this guide will take you from basic installation to advanced configuration, ensuring you have a comprehensive understanding of the process.
Before installing MongoDB, ensure that your system meets the minimum requirements:
C:\Program Files\MongoDB\Server\4.4\bin
).
mongod
To run MongoDB as a service, use the following command:
net start MongoDB
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew tap mongodb/brew
brew install mongodb-community@4.4
brew services start mongodb/brew/mongodb-community
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
MongoDB’s configuration is managed through a file called mongod.conf
. This file is typically located in the /etc
directory on Linux, or in the MongoDB installation directory on Windows and macOS.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
To start MongoDB:
net start MongoDB
To stop MongoDB:
net stop MongoDB
To start MongoDB:
sudo systemctl start mongod
To stop MongoDB:
sudo systemctl stop mongod
On Linux, you can enable MongoDB to start automatically on boot:
sudo systemctl enable mongod
On macOS, you can use Homebrew to start MongoDB as a service:
brew services start mongodb/brew/mongodb-community
The MongoDB shell (mongo
) is an interactive JavaScript interface to MongoDB. You can use it to query and update data, and perform administrative operations.
To connect to the MongoDB shell:
mongo
MongoDB Compass is a graphical user interface that allows you to visualize and interact with your data. You can download it from the MongoDB website.
You can connect to MongoDB using various programming languages. Here is an example using Node.js and the mongodb
package:
npm install mongodb
const { MongoClient } = require('mongodb');
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
await client.connect();
console.log("Connected to MongoDB");
} finally {
await client.close();
}
}
run().catch(console.dir);
// Output
Connected to MongoDB
To enable authentication, edit the mongod.conf
file:
security:
authorization: "enabled"
You can create users with specific roles and privileges. Here is an example using the MongoDB shell:
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
{
"ok": 1
}
Replica sets provide high availability and data redundancy. To configure a replica set, edit the mongod.conf
file:
replication:
replSetName: "rs0"
Initialize the replica set using the MongoDB shell:
rs.initiate()
Sharding distributes data across multiple servers. To configure sharding, start a mongos
instance and add shards:
mongos
instance:
mongos --configdb configReplSet/host1:port1,host2:port2,host3:port3
sh.addShard("shardReplSet/host1:port1")
sh.addShard("shardReplSet/host2:port2")
In this chapter, we covered the installation and setup process of MongoDB, from system requirements to advanced configurations like replica sets and sharding. We discussed how to install MongoDB on Windows, macOS, and Linux, and how to configure it using the mongod.conf file. We also explored how to connect to MongoDB using the MongoDB shell, MongoDB Compass, and programming languages like Node.js. Finally, we touched on basic security setup and advanced configurations to ensure your MongoDB instance is secure and scalable.Happy coding !❤️