First, MongoDB Replica sets (replica set) is simply the master-slave cluster with automatic fault recovery function, the most obvious difference between master-slave cluster and replica set is that the replica set has no fixed "master node": The whole cluster will elect a "master node" When it is not working properly, it changes to other nodes, which improves the stability of the system.
The architecture of the MongoDB Replica sets is as shown in the figure:
As shown in the figure above, the MongoDB Replica sets is structured like a cluster, which can be used as a cluster because he does the same thing as the cluster implementation: if one of the nodes fails, the other nodes will take over the business immediately. Without the need for down operation
Second, the configuration steps of MongoDB Replica sets:
1: The first thing to do is to start three nodes
Describe the parameters involved
--oplogsize Log operation file is large
--dbpath Data File path
--logpath log file path
--port port number, default is 27017. This is the port number I'm using here.
--replset the name of the replica set, the parameter for each node in a replica sets is given a copy set name, here is test.
--replset test/This is followed by the IP and port of the other standard node
--maxconns Maximum number of connections
--fork Background Run
--logappend log files are recycled, and if the log file is full, the new log is overwritten with the longest log.
--keyfile identifies the authentication private key for the same cluster
Where you start the node must be added oplogsize parameters to set its size, otherwise on the 64-bit operating system mongodb,oplogs are quite large-may be 5% of the disk space, to set a reasonable value according to the situation.
2: Start three nodes
Start the first node:/usr/local/mongodb/bin/mongod--port 28010--oplogsize--dbpath=/sdb1/liujijun/data/data/r0/--logpath=/ Sdb1/liujijun/data/log/r0.log--replset rs1/127.0.0.1:28011,127.0.0.1:28012--maxconns--fork--logappend
Start a second node:/usr/local/mongodb/bin/mongod--port 28012--oplogsize--dbpath=/sdb1/liujijun/data/data/r2/--logpath=/ Sdb1/liujijun/data/log/r2.log--replset rs1/127.0.0.1:28010,127.0.0.1:28011--maxconns--fork--logappend
Start a third node:/usr/local/mongodb/bin/mongod--port 28011--oplogsize--dbpath=/sdb1/liujijun/data/data/r1/--logpath=/ Sdb1/liujijun/data/log/r1.log--replset rs1/127.0.0.1:28010,127.0.0.1:28012--maxconns--fork--logappend
3: Chain succession to select a node
#/usr/local/mongodb/bin/mongo--port 28010
> config={_id: ' rs1 ', Members: [{_id:0, Host: ' localhost:28010 '},{_id:1, Host: ' localhost:28011 '},{_id:2, host : ' localhost:28012 '}]}//configuration node information
{
"_id": "Rs1",
"Members": [
{
"_id": 0,
"Host": "localhost:28010"
},
{
"_id": 1,
"Host": "localhost:28011"
},
{
"_id": 2,
"Host": "localhost:28012"
}
]
}
> rs.initiate (config)//This process may have to wait a minute or two of time.
{
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}
> Rs.status ()
{
"Set": "Rs1",
"Date": Isodate ("2012-02-19t06:01:34z"),
"MyState": 1,
"Members": [
{
"_id": 0,
"Name": "localhost:28010",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Optime": {
"T": 1329630925000,
"I": 1
},
"Optimedate": Isodate ("2012-02-19t05:55:25z"),
"Self": true
},
{
"_id": 1,
"Name": "localhost:28011",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 367,
"Optime": {
"T": 1329630925000,
"I": 1
},
"Optimedate": Isodate ("2012-02-19t05:55:25z"),
"Lastheartbeat": Isodate ("2012-02-19t06:01:33z"),
"Pingms": 0
},
{
"_id": 2,
"Name": "localhost:28012",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 367,
"Optime": {
"T": 1329630925000,
"I": 1
},
"Optimedate": Isodate ("2012-02-19t05:55:25z"),
"Lastheartbeat": Isodate ("2012-02-19t06:01:33z"),
"Pingms": 0
}
],
"OK": 1
}
Primary>
Configuration to this end.
Note:
If you want to read and write the separation: Db.getmongo (). The Setslaveok () command implements the object from which the library can be queried. It is best not to set the user name and password when using the replica set, as this will affect the efficiency, the privilege system, and the resource consumption, which requires a lot of computation.