MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.
I wont go deep into explaining the different parts of mongodb. But i just have a quick agenda here for the below.
Again most of my post are based on video. So if you want to have a full video on this, you can look at the video from this site or access through YouTube.
- INSTALLING MONGODB
- SETUP AUTHORIZATION
- CREATE USERS,DBS AND COLLECTIONS
- SETUP REPLICATION (REPLICASETS)
- PERFORM FAILOVER
- HOW TO REVERT TO FORMER PRIMARY
- SETUP REPLICASETS WITH AN EXISTING MONGODB INSTANCE
- INSTALLING AND USING MONGODB COMPASS
Configure repository.
Create an /etc/yum.repos.d/mongodb-enterprise-4.4.repo
file so that you can install MongoDB enterprise directly using yum
:
[mongodb-enterprise-4.4]name=MongoDB Enterprise Repositorybaseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.4/$basearch/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
NOTE If you have a mongodb-enterprise.repo
file in this directory from a previous installation of MongoDB, you should remove it. Use the mongodb-enterprise-4.4.repo
file above to install MongoDB 4.4.
You can also download the .rpm
files directly from the MongoDB repository. Downloads are organized by Red Hat / CentOS version (e.g. 7
), then MongoDB release version (e.g. 4.4
), then architecture (e.g. x86_64
). Odd-numbered MongoDB release versions, such as 4.4
, are development versions and are unsuitable for production deployment.2
Install the MongoDB Enterprise packages.
Install MongoDB Enterprise 4.4.
Issue the following command:
sudo yum install -y mongodb-enterprise
Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.
Why Replication?
- To keep your data safe
- High (24*7) availability of data
- Disaster recovery
- No downtime for maintenance (like backups, index rebuilds, compaction)
- Read scaling (extra copies to read from)
- Replica set is transparent to the application
How Replication Works in MongoDB
MongoDB achieves replication by the use of replica set. A replica set is a group of mongod instances that host the same data set. In a replica, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set. Replica set can have only one primary node.
- Replica set is a group of two or more nodes (generally minimum 3 nodes are required).
- In a replica set, one node is primary node and remaining nodes are secondary.
- All data replicates from primary to secondary node.
- At the time of automatic failover or maintenance, election establishes for primary and a new primary node is elected.
- After the recovery of failed node, it again join the replica set and works as a secondary node.
A typical diagram of MongoDB replication is shown in which client application always interact with the primary node and the primary node then replicates the data to the secondary nodes.
Replica Set Features
- A cluster of N nodes
- Any one node can be primary
- All write operations go to primary
- Automatic failover
- Automatic recovery
- Consensus election of primary
Set Up a Replica Set
In this tutorial, we will convert standalone MongoDB instance to a replica set. To convert to replica set, following are the steps −
- Shutdown already running MongoDB server.
- Start the MongoDB server by specifying — replSet option. Following is the basic syntax of –replSet −
Force a Member to be Primary Using Database Commands
Consider a replica set with the following members:
- mdb0.example.net – the current primary.
- mdb1.example.net – a secondary.
- mdb2.example.net – a secondary .
To force a member to become primary use the following procedure:
- In a mongo shell, run rs.status() to ensure your replica set is running as expected.
- In a mongo shell connected to the mongod instance running on mdb2.example.net, freeze mdb2.example.net so that it does not attempt to become primary for 120 seconds.
rs.freeze(120) |
- In a mongo shell connected the mongod running on mdb0.example.net, step down this instance that the mongod is not eligible to become primary for 120 seconds:
rs.stepDown(120) |
mdb1.example.net becomes primary.
Download and Install Compass
To download Compass on Linux systems, use wget
.NOTE
Alternatively, you can download Compass from the MongoDB downloads page.
- Download MongoDB Compass
wget https://downloads.mongodb.com/compass/mongodb-compass-1.26.1.x86_64.rpm
- Install MongoDB Compass
sudo yum install mongodb-compass-1.26.1.x86_64.rpm
- Start MongoDB Compass
mongodb-compass