In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to implement replica set in MongoDB? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
1. Environment
Node IP operating system MongoDB data node 110.163.91.15RHLE6.5_x64mongodb-linux-x86_64-rhel62-3.4.7.tgz data node 210.163.91.16RHLE6.5_x64mongodb-linux-x86_64-rhel62-3.4.7.tgz data node 310.163.91.17RHLE6.5_x64mongodb-linux-x86_64-rhel62-3.4.7.tgz
2. Install, configure and start MongoDB on three nodes
[root@D2-POMS15 ~] # rpm-qa | grep openssl
Openssl098e-0.9.8e-17.el6_2.2.x86_64
Openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~] # mkdir / usr/local/mongodb
[root@D2-POMS15] # tar-xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15] # mv mongodb-linux-x86_64-rhel62-3.4.7Universe * / usr/local/mongodb/
[root@D2-POMS15 ~] # vim .bash _ profile
# .bash _ profile
# Get the aliases and functions
If [- f ~ / .bashrc]; then
. ~ / .bashrc
Fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
Export PATH=$PATH:/usr/local/mongodb/bin/
Export PATH
[root@D2-POMS15] #. .bash _ profile
[root@D2-POMS15 ~] # mkdir / usr/local/mongodb/db
[root@D2-POMS15 ~] # mkdir / usr/local/mongodb/log
[root@D2-POMS15 ~] # touch / usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~] # vim / usr/local/mongodb/mongodb.conf
Dbpath=/usr/local/mongodb/db
Logpath=/usr/local/mongodb/log/mongodb.log
Logappend=true
Noauth=true
Port=27017
Fork=true
ReplSet=stoners
[root@D2-POMS15] # mongod-f / usr/local/mongodb/mongodb.conf
About to fork child process, waiting until server is ready for connections.
Forked process: 11580
Child process started successfully, parent exiting
[root@D2-POMS15 ~] # mongo
MongoDB shell version v3.4.7
Connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
[root@D2-POMS15] # mongod-f / usr/local/mongodb/mongodb.conf
[root@D2-POMS16] # mongod-f / usr/local/mongodb/mongodb.conf
[root@D2-POMS17] # mongod-f / usr/local/mongodb/mongodb.conf
3. Configure the replica set at any node
Initialize the replica set configuration.
Syntax:
{
_ id:
Members: [
{_ id: 0, host:}
{_ id: 1, host:}
{_ id: 2, host:}
]
}
Configure one of the nodes as the syntax for quorum:
{
_ id: "my_replica_set"
Members: [
{_ id: 0, host: "rs1.example.net:27017"}
{_ id: 1, host: "rs2.example.net:27017"}
{_ id: 2, host: "rs3.example.net", arbiterOnly: true}
]
}
> rs.initiate ({_ id: "stoners", members: [{_ id:0,host: "10.163.97.15 virtual 27017"}]})
{"ok": 1}
Stoners:SECONDARY > rs.conf ()
{
"_ id": "stoners"
"version": 1
"protocolVersion": NumberLong (1)
"members": [
{
"_ id": 0
"host": "10.163.97.15 purl 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": 60000
"getLastErrorModes": {
}
"getLastErrorDefaults": {
"w": 1
"wtimeout": 0
}
"replicaSetId": ObjectId ("5996b49a33625594149428f2")
}
}
Add a node:
Stoners:PRIMARY > rs.add ("10.163.97.16")
{"ok": 1}
Stoners:PRIMARY > rs.add ("10.163.97.17")
{"ok": 1}
View status:
Stoners:PRIMARY > rs.status ()
{
"set": "stoners"
Date: ISODate ("2017-08-18T09:49:18.307Z")
"myState": 1
"term": NumberLong (1)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"appliedOpTime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"durableOpTime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
}
"members": [
{
"_ id": 0
"name": "10.163.97.15 purl 27017"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 144
"optime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2017-08-18T09:49:15Z")
"infoMessage": "could not find member to sync from"
ElectionTime: Timestamp (1503049643, 2)
ElectionDate: ISODate ("2017-08-18T09:47:23Z")
"configVersion": 3
"self": true
}
{
"_ id": 1
"name": "10.163.97.16 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 86
"optime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"optimeDurable": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2017-08-18T09:49:15Z")
OptimeDurableDate: ISODate ("2017-08-18T09:49:15Z")
LastHeartbeat: ISODate ("2017-08-18T09:49:17.787Z")
LastHeartbeatRecv: ISODate ("2017-08-18T09:49:16.774Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.15 purl 27017"
"configVersion": 3
}
{
"_ id": 2
"name": "10.163.97.17 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 52
"optime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"optimeDurable": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2017-08-18T09:49:15Z")
OptimeDurableDate: ISODate ("2017-08-18T09:49:15Z")
LastHeartbeat: ISODate ("2017-08-18T09:49:17.787Z")
LastHeartbeatRecv: ISODate ("2017-08-18T09:49:16.883Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.15 purl 27017"
"configVersion": 3
}
]
"ok": 1
}
4. Write test data
The primary node writes data:
Stoners:PRIMARY > use hr
Switched to db hr
Stoners:PRIMARY > db.emp.insert ({"num": 1})
WriteResult ({"nInserted": 1})
Stoners:PRIMARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
Directly query the replica node and report an error:
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
Error: error: {
"ok": 0
"errmsg": "not master and slaveOk=false"
"code": 13435
"codeName": "NotMasterNoSlaveOk"
}
Set copy node to be readable:
Stoners:SECONDARY > db.getMongo (). SetSlaveOk ()
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
Another replica node:
Stoners:SECONDARY > db.getMongo (). SetSlaveOk ()
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
5. Test failover
Close the primary node first:
Stoners:PRIMARY > use admin
Switched to db admin
Stoners:PRIMARY > db.shutdownServer ()
Server should be down...
One of the replica nodes becomes the primary node:
Stoners:SECONDARY >
Stoners:PRIMARY >
Stoners:PRIMARY > rs.status ()
{
"set": "stoners"
Date: ISODate ("2017-08-18T10:03:49.050Z")
"myState": 1
"term": NumberLong (2)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
"appliedOpTime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
"durableOpTime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
}
"members": [
{
"_ id": 0
"name": "10.163.97.15 purl 27017"
"health": 0
"state": 8
"stateStr": "(not reachable/healthy)
"uptime": 0
"optime": {
"ts": Timestamp (0,0)
"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 ("2017-08-18T10:03:47.314Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:01:03.792Z")
"pingMs": NumberLong (0)
"lastHeartbeatMessage": "Connection refused"
"configVersion":-1
}
{
"_ id": 1
"name": "10.163.97.16 purl 27017"
"health": 1
"state": 1
"stateStr": "PRIMARY"
Uptime: 929
"optime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:03:44Z")
ElectionTime: Timestamp (1503050473, 1)
ElectionDate: ISODate ("2017-08-18T10:01:13Z")
"configVersion": 3
"self": true
}
{
"_ id": 2
"name": "10.163.97.17 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
Uptime: 845
"optime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
"optimeDurable": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:03:44Z")
OptimeDurableDate: ISODate ("2017-08-18T10:03:44Z")
LastHeartbeat: ISODate ("2017-08-18T10:03:47.252Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:03:47.998Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.16 purl 27017"
"configVersion": 3
}
]
"ok": 1
}
Insert data on the primary node:
Stoners:PRIMARY > use hr
Switched to db hr
Stoners:PRIMARY > db.emp.insert ({"num": 2})
WriteResult ({"nInserted": 1})
Stoners:PRIMARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
{"_ id": ObjectId ("5996bbcb5945237d11f3052d"), "num": 2}
View in another replica node:
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
{"_ id": ObjectId ("5996bbcb5945237d11f3052d"), "num": 2}
Now start and shut down the node:
[root@D2-POMS15] # mongod-f / usr/local/mongodb/mongodb.conf
Automatically become a replica node after startup:
Stoners:SECONDARY > rs.status ()
{
"set": "stoners"
Date: ISODate ("2017-08-18T10:08:25.401Z")
"myState": 2
"term": NumberLong (2)
"syncingTo": "10.163.97.17 purl 27017"
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"appliedOpTime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"durableOpTime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
}
"members": [
{
"_ id": 0
"name": "10.163.97.15 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 49
"optime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:09:34Z")
"syncingTo": "10.163.97.17 purl 27017"
"configVersion": 3
"self": true
}
{
"_ id": 1
"name": "10.163.97.16 purl 27017"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 48
"optime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"optimeDurable": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:09:34Z")
OptimeDurableDate: ISODate ("2017-08-18T10:09:34Z")
LastHeartbeat: ISODate ("2017-08-18T10:08:24.973Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:08:23.753Z")
"pingMs": NumberLong (0)
ElectionTime: Timestamp (1503050473, 1)
ElectionDate: ISODate ("2017-08-18T10:01:13Z")
"configVersion": 3
}
{
"_ id": 2
"name": "10.163.97.17 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 48
"optime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"optimeDurable": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:09:34Z")
OptimeDurableDate: ISODate ("2017-08-18T10:09:34Z")
LastHeartbeat: ISODate ("2017-08-18T10:08:24.973Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:08:23.520Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.16 purl 27017"
"configVersion": 3
}
]
"ok": 1
}
The data is also synchronized.
Stoners:SECONDARY > db.getMongo (). SetSlaveOk ()
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
{"_ id": ObjectId ("5996bbcb5945237d11f3052d"), "num": 2}
2. Install, configure and start MongoDB on three nodes
[root@D2-POMS15 ~] # rpm-qa | grep openssl
Openssl098e-0.9.8e-17.el6_2.2.x86_64
Openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~] # mkdir / usr/local/mongodb
[root@D2-POMS15] # tar-xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15] # mv mongodb-linux-x86_64-rhel62-3.4.7Universe * / usr/local/mongodb/
[root@D2-POMS15 ~] # vim .bash _ profile
# .bash _ profile
# Get the aliases and functions
If [- f ~ / .bashrc]; then
. ~ / .bashrc
Fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
Export PATH=$PATH:/usr/local/mongodb/bin/
Export PATH
[root@D2-POMS15] #. .bash _ profile
[root@D2-POMS15 ~] # mkdir / usr/local/mongodb/db
[root@D2-POMS15 ~] # mkdir / usr/local/mongodb/log
[root@D2-POMS15 ~] # touch / usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~] # vim / usr/local/mongodb/mongodb.conf
Dbpath=/usr/local/mongodb/db
Logpath=/usr/local/mongodb/log/mongodb.log
Logappend=true
Noauth=true
Port=27017
Fork=true
ReplSet=stoners
[root@D2-POMS15] # mongod-f / usr/local/mongodb/mongodb.conf
About to fork child process, waiting until server is ready for connections.
Forked process: 11580
Child process started successfully, parent exiting
[root@D2-POMS15 ~] # mongo
MongoDB shell version v3.4.7
Connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
[root@D2-POMS15] # mongod-f / usr/local/mongodb/mongodb.conf
[root@D2-POMS16] # mongod-f / usr/local/mongodb/mongodb.conf
[root@D2-POMS17] # mongod-f / usr/local/mongodb/mongodb.conf
3. Configure the replica set at any node
Initialize the replica set configuration.
Syntax:
{
_ id:
Members: [
{_ id: 0, host:}
{_ id: 1, host:}
{_ id: 2, host:}
]
}
Configure one of the nodes as the syntax for quorum:
{
_ id: "my_replica_set"
Members: [
{_ id: 0, host: "rs1.example.net:27017"}
{_ id: 1, host: "rs2.example.net:27017"}
{_ id: 2, host: "rs3.example.net", arbiterOnly: true}
]
}
> rs.initiate ({_ id: "stoners", members: [{_ id:0,host: "10.163.97.15 virtual 27017"}]})
{"ok": 1}
Stoners:SECONDARY > rs.conf ()
{
"_ id": "stoners"
"version": 1
"protocolVersion": NumberLong (1)
"members": [
{
"_ id": 0
"host": "10.163.97.15 purl 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": 60000
"getLastErrorModes": {
}
"getLastErrorDefaults": {
"w": 1
"wtimeout": 0
}
"replicaSetId": ObjectId ("5996b49a33625594149428f2")
}
}
Add a node:
Stoners:PRIMARY > rs.add ("10.163.97.16")
{"ok": 1}
Stoners:PRIMARY > rs.add ("10.163.97.17")
{"ok": 1}
View status:
Stoners:PRIMARY > rs.status ()
{
"set": "stoners"
Date: ISODate ("2017-08-18T09:49:18.307Z")
"myState": 1
"term": NumberLong (1)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"appliedOpTime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"durableOpTime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
}
"members": [
{
"_ id": 0
"name": "10.163.97.15 purl 27017"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 144
"optime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2017-08-18T09:49:15Z")
"infoMessage": "could not find member to sync from"
ElectionTime: Timestamp (1503049643, 2)
ElectionDate: ISODate ("2017-08-18T09:47:23Z")
"configVersion": 3
"self": true
}
{
"_ id": 1
"name": "10.163.97.16 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 86
"optime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"optimeDurable": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2017-08-18T09:49:15Z")
OptimeDurableDate: ISODate ("2017-08-18T09:49:15Z")
LastHeartbeat: ISODate ("2017-08-18T09:49:17.787Z")
LastHeartbeatRecv: ISODate ("2017-08-18T09:49:16.774Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.15 purl 27017"
"configVersion": 3
}
{
"_ id": 2
"name": "10.163.97.17 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 52
"optime": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
"optimeDurable": {
Ts: Timestamp (1503049755, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2017-08-18T09:49:15Z")
OptimeDurableDate: ISODate ("2017-08-18T09:49:15Z")
LastHeartbeat: ISODate ("2017-08-18T09:49:17.787Z")
LastHeartbeatRecv: ISODate ("2017-08-18T09:49:16.883Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.15 purl 27017"
"configVersion": 3
}
]
"ok": 1
}
4. Write test data
The primary node writes data:
Stoners:PRIMARY > use hr
Switched to db hr
Stoners:PRIMARY > db.emp.insert ({"num": 1})
WriteResult ({"nInserted": 1})
Stoners:PRIMARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
Directly query the replica node and report an error:
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
Error: error: {
"ok": 0
"errmsg": "not master and slaveOk=false"
"code": 13435
"codeName": "NotMasterNoSlaveOk"
}
Set copy node to be readable:
Stoners:SECONDARY > db.getMongo (). SetSlaveOk ()
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
Another replica node:
Stoners:SECONDARY > db.getMongo (). SetSlaveOk ()
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
5. Test failover
Close the primary node first:
Stoners:PRIMARY > use admin
Switched to db admin
Stoners:PRIMARY > db.shutdownServer ()
Server should be down...
One of the replica nodes becomes the primary node:
Stoners:SECONDARY >
Stoners:PRIMARY >
Stoners:PRIMARY > rs.status ()
{
"set": "stoners"
Date: ISODate ("2017-08-18T10:03:49.050Z")
"myState": 1
"term": NumberLong (2)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
"appliedOpTime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
"durableOpTime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
}
"members": [
{
"_ id": 0
"name": "10.163.97.15 purl 27017"
"health": 0
"state": 8
"stateStr": "(not reachable/healthy)
"uptime": 0
"optime": {
"ts": Timestamp (0,0)
"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 ("2017-08-18T10:03:47.314Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:01:03.792Z")
"pingMs": NumberLong (0)
"lastHeartbeatMessage": "Connection refused"
"configVersion":-1
}
{
"_ id": 1
"name": "10.163.97.16 purl 27017"
"health": 1
"state": 1
"stateStr": "PRIMARY"
Uptime: 929
"optime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:03:44Z")
ElectionTime: Timestamp (1503050473, 1)
ElectionDate: ISODate ("2017-08-18T10:01:13Z")
"configVersion": 3
"self": true
}
{
"_ id": 2
"name": "10.163.97.17 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
Uptime: 845
"optime": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
"optimeDurable": {
Ts: Timestamp (1503050624, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:03:44Z")
OptimeDurableDate: ISODate ("2017-08-18T10:03:44Z")
LastHeartbeat: ISODate ("2017-08-18T10:03:47.252Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:03:47.998Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.16 purl 27017"
"configVersion": 3
}
]
"ok": 1
}
Insert data on the primary node:
Stoners:PRIMARY > use hr
Switched to db hr
Stoners:PRIMARY > db.emp.insert ({"num": 2})
WriteResult ({"nInserted": 1})
Stoners:PRIMARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
{"_ id": ObjectId ("5996bbcb5945237d11f3052d"), "num": 2}
View in another replica node:
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
{"_ id": ObjectId ("5996bbcb5945237d11f3052d"), "num": 2}
Now start and shut down the node:
[root@D2-POMS15] # mongod-f / usr/local/mongodb/mongodb.conf
Automatically become a replica node after startup:
Stoners:SECONDARY > rs.status ()
{
"set": "stoners"
Date: ISODate ("2017-08-18T10:08:25.401Z")
"myState": 2
"term": NumberLong (2)
"syncingTo": "10.163.97.17 purl 27017"
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"appliedOpTime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"durableOpTime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
}
"members": [
{
"_ id": 0
"name": "10.163.97.15 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 49
"optime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:09:34Z")
"syncingTo": "10.163.97.17 purl 27017"
"configVersion": 3
"self": true
}
{
"_ id": 1
"name": "10.163.97.16 purl 27017"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 48
"optime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"optimeDurable": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:09:34Z")
OptimeDurableDate: ISODate ("2017-08-18T10:09:34Z")
LastHeartbeat: ISODate ("2017-08-18T10:08:24.973Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:08:23.753Z")
"pingMs": NumberLong (0)
ElectionTime: Timestamp (1503050473, 1)
ElectionDate: ISODate ("2017-08-18T10:01:13Z")
"configVersion": 3
}
{
"_ id": 2
"name": "10.163.97.17 purl 27017"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 48
"optime": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
"optimeDurable": {
Ts: Timestamp (1503050974, 1)
"t": NumberLong (2)
}
OptimeDate: ISODate ("2017-08-18T10:09:34Z")
OptimeDurableDate: ISODate ("2017-08-18T10:09:34Z")
LastHeartbeat: ISODate ("2017-08-18T10:08:24.973Z")
LastHeartbeatRecv: ISODate ("2017-08-18T10:08:23.520Z")
"pingMs": NumberLong (0)
"syncingTo": "10.163.97.16 purl 27017"
"configVersion": 3
}
]
"ok": 1
}
The data is also synchronized.
Stoners:SECONDARY > db.getMongo (). SetSlaveOk ()
Stoners:SECONDARY > use hr
Switched to db hr
Stoners:SECONDARY > db.emp.find ()
{"_ id": ObjectId ("5996b96ca4ca3f4911a83ae8"), "num": 1}
{"_ id": ObjectId ("5996bbcb5945237d11f3052d"), "num": 2}
This is the answer to the question about how to implement the replica set in MongoDB. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.