On Ubuntu 22.04, to set up a MongoDB cluster, you must first install MongoDB on each node in the cluster. While you can use the apt-get command to install MongoDB, the default version available in Ubuntu’s official package repositories may be outdated. To ensure you’re using the latest version, install MongoDB directly from the official website.
Install MongoDB via DEB(Mongos will be not installed with this method)
Select the desired MongoDB version here: https://www.mongodb.com/try/download/community-kubernetes-operator
In this tutorial, we’ll choose MongoDB Server Version 6.0.5 for Ubuntu 22.04.
First, install the required dependencies: libcurl4, openssl, and liblzma5 using the following command:
sudo apt-get install libcurl4 openssl liblzma5
Code language: JavaScript (javascript)
Next, download the chosen MongoDB .deb file:
wget https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/6.0/multiverse/binary-amd64/mongodb-org-server_6.0.5_amd64.deb
Code language: JavaScript (javascript)
Install MongoDB using the sudo apt install command:
sudo apt install ./mongodb-org-server_6.0.5_amd64.deb
Start the MongoDB service:
sudo systemctl start mongod.service
Code language: CSS (css)
Check the status of the service:
sudo systemctl status mongod
Enable the service to start on boot:
sudo systemctl enable mongod
Follow those steps on every machines that you want to install mongoDB on.
Install MongoDB via APT(Mongos will be installed with this method)
Please follow this official document: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
Issues we might have with this method:
For this method, we might have the below error when doing the sudo apt update
sudo apt update
Hit:1 https://deb.nodesource.com/node_18.x jammy InRelease
Hit:2 https://download.docker.com/linux/ubuntu jammy InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:5 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Ign:6 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/5.0 InRelease
Ign:8 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 InRelease
Hit:4 https://packages.cloud.google.com/apt kubernetes-xenial InRelease
Hit:9 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/5.0 Release
Hit:10 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 Release
Get:12 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [108 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:7 http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist InRelease [731 kB]
Err:7 http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist InRelease
Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)
Reading package lists... Done
W: Target Packages (stable/binary-amd64/Packages) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target Packages (stable/binary-all/Packages) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target Translations (stable/i18n/Translation-en_US) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target Translations (stable/i18n/Translation-en) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target CNF (stable/cnf/Commands-amd64) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target CNF (stable/cnf/Commands-all) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: https://apt.kubernetes.io/dists/kubernetes-xenial/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
W: https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/5.0/Release.gpg: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
W: https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/6.0/Release.gpg: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
E: Failed to fetch http://downloads-distro.mongodb.org/repo/ubuntu-upstart/dists/dist/InRelease Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)
E: The repository 'http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: Target Packages (stable/binary-amd64/Packages) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target Packages (stable/binary-all/Packages) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target Translations (stable/i18n/Translation-en_US) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target Translations (stable/i18n/Translation-en) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target CNF (stable/cnf/Commands-amd64) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
W: Target CNF (stable/cnf/Commands-all) is configured multiple times in /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-jammy.list:1 and /etc/apt/sources.list.d/docker.list:1
Code language: PHP (php)
To solve this, we will need to first delete those duplicated .list file, first:
cd /etc/apt/sources.list.d/
Code language: PHP (php)
And then do:
ls
it will show you those .list files:
archive_uri-https_download_docker_com_linux_ubuntu-jammy.list docker.list kubernetes.list mongodb-org-6.0.list mongodb-org.list
Code language: CSS (css)
Remove all .list files what might caused a conflict:
sudo rm mongodb-org.list
Code language: CSS (css)
Notice Here, we might also have this error:
<strong>W: </strong>GPG error: https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 6A26B1AE64C3C388
Code language: PHP (php)
To solve this, we need to import the missing key:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6A26B1AE64C3C388
Code language: CSS (css)
Then, we can successfully do:
sudo apt update
sudo apt-get install -y mongodb-org
Code language: JavaScript (javascript)
Sharded Cluster Introduction
Once MongoDB is installed, you can configure security, enable remote access, create users and databases, assign admin roles to users, and more. You should also optimize MongoDB settings for peak performance.
With MongoDB installed, you’re now ready to configure your MongoDB cluster.
A sharded MongoDB cluster is a type of distributed database architecture that horizontally scales data across multiple servers, known as shards. Each shard is an independent database that holds a portion of the entire dataset. Sharding is designed to address challenges with data growth, high query rates, and read/write throughput by distributing the workload across multiple servers.
In a sharded MongoDB cluster, there are three main components:
- Shard: A shard is a MongoDB server or replica set that stores a subset of the data. Shards are responsible for holding and managing the actual data. In production environments, each shard is typically a replica set to ensure data redundancy and high availability.
- Config Server: Config servers store metadata and configuration settings for the cluster. They keep track of the cluster’s state and the distribution of data across shards. In a sharded cluster, you usually have a replica set of config servers to ensure redundancy and high availability.
- Query Router (mongos): Query routers, or mongos instances, are the entry points for client applications to access the sharded cluster. They act as proxies that route queries and write operations to the appropriate shards based on the cluster’s metadata stored in config servers. Clients connect to the mongos instances instead of connecting directly to the shards.
Sharding in MongoDB is done based on a shard key, which is an indexed field or fields used to determine how data is distributed across shards. The shard key helps MongoDB divide the dataset into smaller chunks called “chunks,” which are then distributed across the shards in a balanced manner.
A sharded MongoDB cluster provides several benefits, including:
- Improved horizontal scalability: As your dataset grows or your application requires more capacity, you can easily add more shards to the cluster, distributing the data and workload across more servers.
- Load balancing: MongoDB automatically balances the distribution of data across the shards to ensure that no single shard becomes a bottleneck.
- High availability: By using replica sets for shards and config servers, you can ensure data redundancy and fault tolerance in the case of server failures.
In summary, a sharded MongoDB cluster is a scalable and highly available distributed database architecture that distributes data and workload across multiple servers to optimize performance, capacity, and resilience.
Deploy the Sharded Cluster
To install a sharded MongoDB cluster across multiple machines, you need to set up and configure each component: shards, config servers, and query routers (mongos). This example assumes you have three machines for shards, one machine for config servers, and one machine for the query router (mongos). You can adjust the number of machines based on your requirements.
Install the MongoDB as introduced Above.
Set up the config server(s): On the machine designated for config servers, create a directory for storing the configuration data. For example:
sudo mkdir -p /data/configdb
Edit the MongoDB configuration file (/etc/mongod.conf) and add the following lines:
sharding:
clusterRole: configsvr
replication:
replSetName: ConfigReplSet
net:
bindIp: <IP_ADDRESS_OF_CONFIG_SERVER> Replace <IP_ADDRESS_OF_CONFIG_SERVER> with the actual IP address of the config server machine.
Code language: HTML, XML (xml)
Start the config server:
sudo mongod --config /etc/mongod.conf
Connect to the config server using the MongoDB shell:
mongo --host <IP_ADDRESS_OF_CONFIG_SERVER> --port 27017
Code language: HTML, XML (xml)
Initiate the config server replica set:
rs.initiate({_id: "ConfigReplSet", configsvr: true, members: [{_id: 0, host: "<IP_ADDRESS_OF_CONFIG_SERVER>:27017"}]})
Code language: CSS (css)
If there are more than one node in the ConfigSet, just add them into the members:
rs.initiate({ _id: "ConfigReplSet", configsvr: true, members: [ { _id: 0, host: "<config_server_1_ip>:27017" }, { _id: 1, host: "<config_server_2_ip>:27017" }, { _id: 2, host: "<config_server_3_ip>:27017" }, ], });
Code language: CSS (css)
Set up the shard servers: For each shard server, create a data directory:
sudo mkdir -p /data/shardX
Replace X with the shard number (e.g., shard1, shard2, etc.).
Edit the MongoDB configuration file (/etc/mongod.conf) on each shard server and add the following lines:
sharding:
clusterRole: shardsvr
replication:
replSetName: ShardXReplSet
net:
bindIp: <IP_ADDRESS_OF_SHARD_SERVER>
Code language: HTML, XML (xml)
Replace X with the shard number and <IP_ADDRESS_OF_SHARD_SERVER> with the actual IP address of the shard server machine.
Start the shard server:
sudo mongod --config /etc/mongod.conf
Connect to each shard server using the MongoDB shell and initiate the replica set:
rs.initiate({_id: "ShardXReplSet", members: [{_id: 0, host: "<IP_ADDRESS_OF_SHARD_SERVER>:27017"}]})
Code language: CSS (css)
If there are multi members in the ShardSet, Just modify the config to
rs.initiate({ _id: "ShardXReplSet", members: [ { _id: 0, host: "<shard_server_1_ip>:27017" }, { _id: 1, host: "<shard_server_2_ip>:27017" }, { _id: 2, host: "<shard_server_3_ip>:27017" }, ], });
Code language: CSS (css)
Set up the query router (mongos):
On the machine designated for the query router, edit the MongoDB configuration file (/etc/mongos.conf) and add the following lines:
sharding:
configDB: ConfigReplSet/<IP_ADDRESS_OF_CONFIG_SERVER>:27017
net:
bindIp: <IP_ADDRESS_OF_MONGOS_SERVER>
Code language: HTML, XML (xml)
Replace <IP_ADDRESS_OF_CONFIG_SERVER> with the actual IP address of the config server machine and <IP_ADDRESS_OF_MONGOS_SERVER> with the actual IP address of the query router machine.
Start the query router:
sudo mongos --config /etc/mongos.conf
Use Mongosh to connect to Mongos:
mongosh --host ip --port port
Add Shard:
sh.addShard("ShardXReplSet/<IP_ADDRESS_OF_SHARD_SERVER>:27017")
Code language: HTML, XML (xml)
Other Potential Issues with Mongo DB
If the we cannot connect to the mongodb: we should check its status first:
sudsystemctl status mongod
If the response are:
sudsystemctl status mongod
× mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2023-04-07 15:26:44 PDT; 33s ago
Docs: https://docs.mongodb.org/manual
Process: 1966476 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
Main PID: 1966476 (code=exited, status=14)
CPU: 590ms
Code language: JavaScript (javascript)
Then, we should do:
sudo systemctl stop mongod
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo systemctl start mongod
Code language: JavaScript (javascript)
On the other case, if the status is running like below:
sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-07 15:27:29 PDT; 3s ago
Docs: https://docs.mongodb.org/manual
Main PID: 1966987 (mongod)
Memory: 164.8M
CPU: 1.119s
CGroup: /system.slice/mongod.service
└─1966987 /usr/bin/mongod --config /etc/mongod.conf
Code language: JavaScript (javascript)
We should do
sudo systemctl stop mongod
sudo rm -rf /tmp/mongodb-<Port>.sock
sudo systemctl start mongod
Code language: HTML, XML (xml)
Replace the <Port> with the actual port that your mongodb is running.