In this article, we will show you how to configure MongoDB with replication on 3 servers, so that our data is stored on 3 servers. We will create 3 servers named mongodb1, mongodb2, and mongodb3. We will start with the article How to install MongoDB and complete only the first installation step.
Update hosts
We will edit our hosts file on each server to add each server's host with its corresponding private IP:
10.20.10.22 mongodb1
10.20.10.23 mongodb2
10.20.10.24 mongodb3Create a key
This step only needs to be performed on one of the 3 servers. In our example, we will do it on mongodb1 and then copy it to mongodb2 and mongodb3:
# openssl rand -base64 756 > /var/lib/mongodb/mongo.key
# scp /var/lib/mongodb/mongo.key mongodb2:/var/lib/mongodb/mongo.key
# scp /var/lib/mongodb/mongo.key mongodb3:/var/lib/mongodb/mongo.keyKey permissions
This step must be performed on all 3 servers. In our example, we will do it on mongodb1 and then copy it to mongodb2 and mongodb3:
# chown mongodb:mongodb /var/lib/mongodb/mongo.key
# chmod 400 /var/lib/mongodb/mongo.keyCreate an Admin user
This step only needs to be performed on mongodb1:
# mongo
> use admin
> db.createUser({user: "mongo-replica", pwd: "CloudingEjemploKB", roles:[{role: "root", db: "admin"}]})
> quit()This will return:
Successfully added user: {
"user" : "mongo-replica",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}Configure MongoDB
Now that we have created the administrator user, we will configure MongoDB on all 3 servers to authenticate with the key we created and assign a name to the replica. To do this, we will add the following to the configuration file:
# vi /etc/mongod.conf
security:
keyFile: /var/lib/mongodb/mongo.key
replication:
replSetName: rs0
Once the changes have been made, we will restart MongoDB on all 3 servers:
# systemctl restart mongodInitialize MongoDB replicas
We will perform this step only on mongodb1. First, we will connect using the username and password we created in the previous step and initialize the replica:
# mongo -u mongo-replica -p --authenticationDatabase adminrs.initiate(
{
_id : "rs0",
members: [
{ _id : 0, host : "mongodb1:27017" },
{ _id : 1, host : "mongodb2:27017" },
{ _id : 2, host : "mongodb3:27017" }
]
}
)
This will return:
{
"ok" : 1,
"operationTime" : Timestamp(1520358155, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1520358155, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}We can view the configuration:
rs.conf()rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb2:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5a9ed30a3a8cb5b9647406b5")
}
}We can also view the status of the replicas by running:
rs.status(){
"set" : "rs0",
"date" : ISODate("2018-03-06T17:53:45.571Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 842,
"optime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-06T17:53:37Z"),
"electionTime" : Timestamp(1520358165, 1),
"electionDate" : ISODate("2018-03-06T17:42:45Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "mongodb2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 670,
"optime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-06T17:53:37Z"),
"optimeDurableDate" : ISODate("2018-03-06T17:53:37Z"),
"lastHeartbeat" : ISODate("2018-03-06T17:53:43.925Z"),
"lastHeartbeatRecv" : ISODate("2018-03-06T17:53:44.971Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongodb1:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "mongodb3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 670,
"optime" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1520358817, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-06T17:53:37Z"),
"optimeDurableDate" : ISODate("2018-03-06T17:53:37Z"),
"lastHeartbeat" : ISODate("2018-03-06T17:53:43.925Z"),
"lastHeartbeatRecv" : ISODate("2018-03-06T17:53:44.681Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongodb1:27017",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1520358817, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1520358817, 1),
"signature" : {
"hash" : BinData(0,"4rm4LohRlAYnX8qwMh2XkFZCZ4A="),
"keyId" : NumberLong("6529888605471506433")
}
}
}This article is based on: https://docs.mongodb.com