Introduction
MongoDB is a database that is a source-available cross-platform document-oriented database program. It was classified as a NoSQL database program. MongoDB uses JSON-like documents with optional schemas. MongoDB was developed by MongoDB Inc.and licensed under the Server Side Public License (SSPL).MongoDB cluster is another type of configuration.
PREREQUISITES
So Basic knowledge of programming concepts CLI and shell, functions, variables, and boolean operators.
Main features
- Ad-hoc queries
- Indexing
- Replication
- Load balancing
- File storage
- Aggregation
- Server-side JavaScript execution
- Capped collections
- Transcations
Setup network configuration for all nodes mongodb cluster
vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
8
192.168.56.101 master8.unixcop.com master8
192.168.56.202 worker18.unixcop.com worker1
192.168.56.203 worker28.unixcop.com worker2
Install Mongdb to all nodes
- Install repository to all nodes
cat <<REPO > /etc/yum.repos.d/mongodb.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
REPO
- Installing mongodb-org packages and dependencies
dnf install -y mongodb-org
Configure MongoDB cluster
- Configure mongodb cluster server
cat <<CONFIG > /etc/mongod.conf
....
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 192.168.56.194 # Listen to local interface only, comment to listen on all interfaces.
security:
keyFile: /var/lib/mongo/mongo-keyfile
#operationProfiling:
replication:
replSetName: configReplSet
sharding:
clusterRole: "configsvr"
CONFIG
2. Configure Mongodb cluster server Nodes
cat <<CONFIG > /etc/mongod.conf
....
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 192.168.56.202 # Listen to local interface only, comment to listen on all interfaces.
security:
keyFile: /var/lib/mongo/mongo-keyfile
#operationProfiling:
replication:
replSetName: configReplSet
sharding:
clusterRole: "shardsvr"
CONFIG
Create an Administrator User
- Use the master server to create replica set of configuration to all servers
mongo
Use admin database
> use admin
Additionally, Create an administrator user with privileges. Also Replace “password” with a strong password of your choice:
> db.createUser({user: "mongo-admin", pwd: "password", roles:[{role: "root", db: "admin"}]})
Generate a key to secure connection
So, the Issue this command to generate your key file.
Note: please copy key to master server
openssl rand -base64 756 > /var/lib/mongo/mongo-keyfile
chmod 400 /var/lib/mongo/mongo-keyfile
chown mongod:mongod /var/lib/mongo/mongo-keyfile
Also Restart MongoDB service
systemctl restart mongod
Configure Query Router
This query router where to find the config server and their replica nodes
cat /etc/mongoRouter.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongoRouter.log
net:
port: 27017
bindIp: 192.168.56.194
sharding:
configDB: ConfigReplSet/192.168.56.194:27019
Configure shared database mongodb cluster
So Replicate the data from the configured server we told it to replicate the configuration.
cat /etc/mongodShard.conf
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodShard.log
net:
port: 27018
bindIp: 192.168.56.202
sharding:
clusterRole: shardsvr
replication:
replSetName: configReplSet
Execute rs.initiate() to turn on replications with and then check the status with rs.status(). Notice the name configReplSet.Use any name as long as using the same name across all shards in the cluster.
configReplSet:SECONDARY> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "192.168.56.194:27017",
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized",
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
}
}
configReplSet:SECONDARY> rs.status()
{
"set" : "configReplSet",
"date" : ISODate("2021-08-14T15:25:51.392Z"),
"myState" : 2,
"term" : NumberLong(4),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1628939350, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1628939350, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.56.194:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3301,
"optime" : {
"ts" : Timestamp(1628939350, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2021-08-14T11:09:10Z"),
"syncingTo" : "",
................
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2021-08-14T15:25:51.142Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Our replica set configuration is invalid or does not include us",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
}
}
configReplSet:SECONDARY>
Redundancy and Data Availability Mongodb cluster
So Replication provides redundancy and increases data availability.With multiple copies of data on different database servers, the image offers a level of fault tolerance against the loss of a single database server.
Also In some cases, replication can provide increased read capacity as clients can send read operations to different servers. In addition to, Maintaining copies of data in various data centers can increase data locality and availability for distributed applications. You can also keep additional copies for dedicated purposes, such as disaster recovery, reporting, or backup. Also, check PostgreSQL