MongoDB replica set (2) Construction
Preparations
Environment: CentOS6.5 64-bit
MongoDB 3.0.1 64-bit
1. Start the mongod Service
[Root @ localhost data] #/usr/local/mongodb3.0.1/bin/mongod -- port 40144 -- dbpath =/data/mongodb3.0.1/data/-- logpath =/data/mongodb3.0.1/log/ mongodb. log -- directoryperdb -- maxConns 100 -- logappend -- replSet repl/192.168.66.89: 50144 -- fork
[Root @ localhost data] #/usr/local/mongodb3.0.1/bin/mongod -- port 50144 -- dbpath =/data/mongodb3.0.1/db_50144/-- logpath =/data/mongodb3.0.1/db_50144/ mongodb. log -- directoryperdb -- maxConns 100 -- logappend -- replSet repl/192.168.66.89: 40144 -- fork
[Root @ localhost data] #/usr/local/mongodb3.0.1/bin/mongod -- port 60144 -- dbpath =/data/mongodb3.0.1/db_60144/-- logpath =/data/mongodb3.0.1/db_60144/ mongodb. log -- directoryperdb -- maxConns 100 -- logappend -- replSet repl/192.168.66.89: 40144 -- fork
Parameters:
-- OplogSize: the size of the log operation file (5% of the remaining disk space by default)
-- Dbpath: Data File Path
-- Logpath: Log File Path
-- Port number. The default value is 27017.
-- ReplSet: the name of the replica set. The parameter of each node in a replica set must use a replica set name. Here is test.
-- ReplSet test/is followed by the ip address and port of other standard nodes.
-- MaxConns maximum number of connections
-- Fork background running
-- The logappend log file is used cyclically. If the log file is full, the new log will overwrite the latest log.
Log on to the mongodb database from any node
[Root @ localhost data] #/usr/local/mongodb3.0.1/bin/mongo -- port 40144
MongoDB shell version: 3.0.1
Connecting to: 127.0.0.1: 40144/test
Server has startup warnings:
T09: 06: 42.270 + 0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
T09: 06: 42.270 + 0800 I CONTROL [initandlisten]
T09: 06: 42.270 + 0800 I CONTROL [initandlisten]
2015-05-08T09: 06: 42.270 + 0800 I CONTROL [initandlisten] ** WARNING:/sys/kernel/mm/transparent_hugepage/enabled is 'alway '.
T09: 06: 42.270 + 0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
T09: 06: 42.270 + 0800 I CONTROL [initandlisten]
2015-05-08T09: 06: 42.270 + 0800 I CONTROL [initandlisten] ** WARNING:/sys/kernel/mm/transparent_hugepage/defrag is 'alway '.
T09: 06: 42.270 + 0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
T09: 06: 42.270 + 0800 I CONTROL [initandlisten]
# Use the admin Database
> Use admin;
Switched to db admin
# Define replica set configuration variables and initialize them. The _ id: "replSet" here must be the same as the preceding command parameter "-- replSet repl ".
> Db. runCommand ({"replSetInitiate": {"_ id": "repl", "members": [{"_ id": 1, "host": "192.168.66.89: 40144 "},{" _ id ": 2," host ":" 192.168.66.89: 50144 "},{" _ id ": 3," host ":" 192.168.66.89: 60144 "}]});
{"OK": 1}
# View replica set information
Repl: OTHER> db. _ adminCommand ("replSetGetStatus ");
{
"Set": "repl ",
"Date": ISODate ("2015-05-08T01: 18: 55.619Z "),
"MyState": 1,
"Members ":[
{
"_ Id": 1,
"Name": "192.168.66.89: 40144 ",
"Health": 1,
"State": 1,
"StateStr": "PRIMARY ",
"Uptime": 776,
"Optime": Timestamp (1431047920, 1 ),
"OptimeDate": ISODate ("2015-05-08T01: 18: 40Z "),
"ElectionTime": Timestamp (1431047923, 1 ),
"ElectionDate": ISODate ("2015-05-08T01: 18: 43Z "),
"ConfigVersion": 1,
"Self": true
},
{
"_ Id": 2,
"Name": "192.168.66.89: 50144 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 15,
"Optime": Timestamp (1431047920, 1 ),
"OptimeDate": ISODate ("2015-05-08T01: 18: 40Z "),
"LastHeartbeat": ISODate ("2015-05-08T01: 18: 53.20.z "),
"LastHeartbeatRecv": ISODate ("2015-05-08T01: 18: 53.20.z "),
"PingMs": 0,
"LastHeartbeatMessage": "cocould not find member to sync from ",
"ConfigVersion": 1
},
{
"_ Id": 3,
"Name": "192.168.66.89: 60144 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 15,
"Optime": Timestamp (1431047920, 1 ),
"OptimeDate": ISODate ("2015-05-08T01: 18: 40Z "),
"LastHeartbeat": ISODate ("2015-05-08T01: 18: 53.20.z "),
"LastHeartbeatRecv": ISODate ("2015-05-08T01: 18: 53.649Z "),
"PingMs": 0,
"LastHeartbeatMessage": "cocould not find member to sync from ",
"ConfigVersion": 1
}
],
"OK": 1
}
Ii. Test
1. Write Data to the master node and query data from other nodes
Repl: PRIMARY> use mtdamai; switched to db mtdamairepl: PRIMARY> db. user. insert ({name: "wangxiuli"}); WriteResult ({"nInserted": 1}) repl: PRIMARY> db. user. find (); {"_ id": ObjectId ("554c237123db30ab1a233e4b"), "name": "wangxiuli"} # repl: SECONDARY> use mtdamai; switched to db mtdamairepl: SECONDARY> db. user. find (); {"_ id": ObjectId ("554c237123db30ab1a233e4b"), "name": "wangxiuli "}
2. Stop the master node
Repl: SECONDARY> rs. status (); {"set": "repl", "date": ISODate ("2015-05-08T02: 51: 25.273Z"), "myState": 2, "members ": [{"_ id": 1, "name": "192.168.66.89: 40144", "health": 0, "state": 8, "stateStr ": "(not reachable/healthy)", "uptime": 0, "optime": Timestamp (0, 0), "optimeDate": ISODate ("1970-01-01T00: 00: 00Z ")," lastHeartbeat ": ISODate (" 2015-05-08T02: 51: 23.984Z ")," Expiration ": ISODate (" 2015-05-08T02: 50: 55.975Z ")," pingMs ": 0, "lastHeartbeatMessage": "Failed attempt to connect to 192.168.66.89: 40144; couldn't connect to server 192.168.66.89: 40144 (192.168.66.89), connection attempt failed", "configVersion":-1 }, {"_ id": 2, "name": "192.168.66.89: 50144", "health": 1, "state": 1, "stateStr": "PRIMARY ", "uptime": 5563, "optime": Timestamp (1431053169, 2), "optimeDate": ISODate ("2015-05-08T02: 46: 09Z"), "lastHeartbeat ": ISODate ("2015-05-08T02: 51: 24.227Z"), "lastHeartbeatRecv": ISODate ("2015-05-08T02: 51: 24.120Z"), "pingMs": 0, "electionTime": Timestamp (1431053458, 1), "electionDate": ISODate ("2015-05-08T02: 50: 58Z"), "configVersion": 1 },{ "_ id": 3, "name": "192.168.66.89: 60144 "," health ": 1," state ": 2," stateStr ":" SECONDARY "," uptime ": 5733," optime ": Timestamp (1431053169, 2 ), "optimeDate": ISODate ("2015-05-08T02: 46: 09Z"), "configVersion": 1, "self": true}], "OK ": 1} # check that the master node has been re-elected and then insert data for verification repl: PRIMARY> db. user. insert ({name: "chenglong"}); WriteResult ({"nInserted": 1}) repl: PRIMARY> db. user. find (); {"_ id": ObjectId ("554c237123db30ab1a233e4b"), "name": "wangxiuli"} {"_ id": ObjectId ("updated "), "name": "chenglong"} # verify that the standby node cannot write repl: SECONDARY> db. user. insert ({name: "liudehua"}); WriteResult ({"writeError": {"code": undefined, "errmsg": "not master "}}) # If the queried data already exists, it means that the data has been synchronized to other nodes repl: SECONDARY> use mtdamai; switched to db mtdamairepl: SECONDARY> db. user. find (); Error: error: {"$ err": "not master and slaveOk = false", "code": 13435} repl: SECONDARY> rs. slaveOk (); repl: SECONDARY> db. user. find (); {"_ id": ObjectId ("554c237123db30ab1a233e4b"), "name": "wangxiuli"} {"_ id": ObjectId ("updated "), "name": "chenglong"} # restart the disabled node and change it to secondaryrepl: SECONDARY> rs. status (); {"set": "repl", "date": ISODate ("2015-05-08T07: 54: 20.607Z"), "myState": 2, "syncingTo ": "192.168.66.89: 50144", "members": [{"_ id": 1, "name": "192.168.66.89: 40144", "health": 1, "state ": 2, "stateStr": "SECONDARY", "uptime": 19, "optime": Timestamp (1431053600, 1), "optimeDate": ISODate ("2015-05-08T02: 53: 20Z ")," lastHeartbeat ": ISODate (" 2015-05-08T07: 54: 19.079Z ")," lastHeartbeatRecv ": ISODate (" 2015-05-08T07: 54: 425557z ")," pingMs ": 0, "syncingTo": "192.168.66.89: 60144", "configVersion": 1 },{ "_ id": 2, "name": "192.168.66.89: 50144", "health ": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 23738, "optime": Timestamp (1431053600, 1), "optimeDate ": ISODate ("2015-05-08T02: 53: 20Z"), "lastHeartbeat": ISODate ("2015-05-08T07: 54: 425152z"), "lastHeartbeatRecv": ISODate ("2015-05-08T07: 54: 20.105Z ")," pingMs ": 0," electionTime ": Timestamp (1431053458, 1)," electionDate ": ISODate (" 2015-05-08T02: 50: 58Z ")," configVersion ": 1 },{ "_ id": 3, "name": "192.168.66.89: 60144", "health": 1, "state": 2, "stateStr ": "SECONDARY", "uptime": 23908, "optime": Timestamp (1431053600, 1), "optimeDate": ISODate ("2015-05-08T02: 53: 20Z"), "syncingTo ": "192.168.66.89: 50144", "configVersion": 1, "self": true}], "OK": 1} # The data is also synchronized to repl: SECONDARY> use mtdamai; switched to db mtdamairepl: SECONDARY> db. user. find (); {"_ id": ObjectId ("554c237123db30ab1a233e4b"), "name": "wangxiuli"} {"_ id": ObjectId ("updated "), "name": "chenglong "}