The previous one introduced the master-slave mode, now MongoDB official has not recommended the use of master-slave mode, the alternative is the use of replica set mode. The replica set cannot operate on a single computer. Need to prepare 3 computers to build. A replica set is a cluster of MongoDB replicas.
The principle of synchronization is that the write operation occurs in the main library, which synchronizes the OpLog log of the main library from the library .
There is no specific main library in the cluster, the main library is elected, and if the main library is down, a main library will be elected.
Here's a look at concrete construction. The use of three computers to build, respectively, is
192.168.0.12:27017 Zhf-maple
192.168.0.11:27017 Zhf-linux
192.168.0.4:27017 Ubuntu
[Email protected]:/home/zhf/Desktop # cat/etc/hosts
127.0.0.1 localhost
192.168.0.12 Zhf-maple
192.168.0.11 Zhf-linux
192.168.0.4 Ubuntu
In the other two computer/etc/hosts file also add the corresponding domain name in
Create Node0,node1,node2 folders on each of the three computers and run the commands separately
Mongod--dbpath/home/zhf/node0--replset Rs0
Mongod--dbpath/home/zhf/node1--replset Rs0
Mongod--dbpath/home/zhf/node2--replset Rs0
Enter any one of the Mongod servers at this point . Use the admin collection. Then run rs.initiate ()
> Use admin
Switched to DB admin
> Rs.initiate ()
{
"Info2": "No configuration explicitly specified-making one",
"Me": "zhf-linux:27017",
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}
Confirm Configuration
> rs.conf ()
{
"_id": "Rs0",
"Version": 1,
"Members": [
{
"_id": 0,
"Host": "zhf-linux:27017"
}
]
}
Test add from Library on primary
Rs0:primary> rs.add (' ubuntu:27017 ')
{"OK": 1}
View status. Health:1 represents normal start, STATESTR can see which is the main, which is from the
Rs0:primary> Rs.status ()
{
"Set": "Rs0",
"Date": Isodate ("2018-01-16t09:38:21z"),
"MyState": 1,
"Members": [
{
"_id": 0,
"Name": "zhf-linux:27017",
"Health": 1,
"State": 1,
" statestr": "PRIMARY",
"Uptime": 587,
"Optime": Timestamp (1516095474, 1),
"Optimedate": Isodate ("2018-01-16t09:37:54z"),
"Electiontime": Timestamp (1516095459, 1),
"Electiondate": Isodate ("2018-01-16t09:37:39z"),
"Self": true
},
{
"_id": 1,
"Name": "ubuntu:27017",
"Health": 1,
"State": 2,
" statestr": "Secondary",
"Uptime": 27,
"Optime": Timestamp (1516095474, 1),
"Optimedate": Isodate ("2018-01-16t09:37:54z"),
"Lastheartbeat": Isodate ("2018-01-16t09:38:20z"),
"Lastheartbeatrecv": Isodate ("2018-01-16t09:38:19z"),
"Pingms": 54,
"Syncingto": "zhf-linux:27017"
}
],
"OK": 1
}
Add it again from the library. At this time, 2 secondary, a primary has been started up.
Rs0:primary> rs.add (' zhf-maple:27017 ')
{"Down": ["zhf-maple:27017"], "OK": 1}
Rs0:primary> Rs.status ()
{
"Set": "Rs0",
"Date": Isodate ("2018-01-16t09:38:42z"),
"MyState": 1,
"Members": [
{
"_id": 0,
"Name": "zhf-linux:27017",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 608,
"Optime": Timestamp (1516095516, 1),
"Optimedate": Isodate ("2018-01-16t09:38:36z"),
"Electiontime": Timestamp (1516095459, 1),
"Electiondate": Isodate ("2018-01-16t09:37:39z"),
"Self": true
},
{
"_id": 1,
"Name": "ubuntu:27017",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 48,
"Optime": Timestamp (1516095516, 1),
"Optimedate": Isodate ("2018-01-16t09:38:36z"),
"Lastheartbeat": Isodate ("2018-01-16t09:38:41z"),
"Lastheartbeatrecv": Isodate ("2018-01-16t09:38:42z"),
"Pingms": 7,
"Syncingto": "zhf-linux:27017"
},
{
"_id": 2,
"Name": "zhf-maple:27017",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 0,
"Optime": Timestamp (0, 0),
"Optimedate": Isodate ("1970-01-01t00:00:00z"),
"Lastheartbeat": Isodate ("2018-01-16t09:38:42z"),
"Lastheartbeatrecv": Isodate ("1970-01-01t00:00:00z"),
"Pingms": 0,
"Lastheartbeatmessage": "Received heartbeat from member with the same member ID as ourself:0"
}
],
Next, update the data on the primary:
s0:primary> Use Maple
Switched to DB Maple
Rs0:primary> Db.insert ({' name ': ' Zhanghongfeng '})
2018-01-16t17:56:06.625+0800 typeerror:property ' Insert ' of object maple is not a function
Rs0:primary> Db.maple.insert ({' name ': ' Zhanghongfeng '})
Writeresult ({"ninserted": 1})
At this point, view the database on secondary
rs0:secondary> Use Maple
Switched to DB Maple
Rs0:secondary> Db.maple.find ()
Error: {"$err": "Not Master and Slaveok=false", "Code": 13435}
The lookup indicates not master and Slaveok=false. The reason is that MongoDB defaults to read and write data from the master node, which is not allowed on the replica node (more cannot be written), and needs to be set to read by the replica node. Set by Db.getmongo (). Setslaveok ()
Rs0:secondary> Db.getmongo (). Setslaveok ()
Rs0:secondary> Db.maple.find ()
{"_id": ObjectId ("5a5dcc538c0f7da3bc2f819f"), "name": "Zhanghongfeng"}
This makes it possible to query the corresponding data, indicating that synchronization is performed on the replica node.
MongoDB Learning: Replica set