MongoDB Replica Collection

Source: Internet
Author: User

#简述: Replica collection (Replica sets), a replication feature based on the master/slave replication mechanism, but with the addition of automatic failover and recovery features, a cluster can support up to 7 servers, and any node can be the primary node. All writes are distributed to the master node, and the read operation can be performed on any node;
#系统环境OSCentOS6.5server1192.168.3.100:27017server1192.168.3.100:27018node1192.168.3.101:27017node2192.168.3.102 : 27017
#配置副本集方法之一 (Automatically select a database as the primary database) #server1启动节点mkdir  /application/mongodb/database /application/mongodb/logs /application/mongodb/bin/mongod --rest --replset fuben/192.168.3.100:27017 --port  27017 --dbpath=/application/mongodb/database --logpath=/application/mongodb/logs/mongodb.log -- FORK#NODE1 Boot node mkdir /application/mongodb/database /application/mongodb/logs/application/mongodb/bin/ mongod --rest --replset fuben/192.168.3.101:27017 --port 27017 --dbpath=/ Application/mongodb/database --logpath=/application/mongodb/logs/mongodb.log --fork#node2 Boot node mkdir  /application/mongodb/database /application/mongodb/logs/application/mongodb/bin/mongod --rest  --replset fuben/192.168.3.102:27017 --port 27017 --dbpath=/application/mongodb/ Database --logpath=/application/mongodb/logs/mongodb.log --fork#server1 Start the quorum node mkdir /application /mongodb/arb/application/mongodb/bin/mongod --rest --replset fuben --dbpath=/application/mongodb/arb --port  27018 --logpath=/application/mongodb/logs/arb.log --fork
 #Server1登陆mongo/application/mongodb/bin/mongo --port 27017# Initialize node (action on Server1) > rs.initiate ({_id: "Fuben", Members:[{_id:1,host: "192.168.3.100:27017"},{_id:2,host: " 192.168.3.101:27017 "},{_id:3,host:" 192.168.3.102:27017 "},{_id:4,host:" 192.168.3.100:27018 "," ArbiterOnly ": true} ]}); {"Info"  :  "Config now saved locally.  should come online in"  about a minute. "," OK " : 1} 
#查看副本集状态fuben:P Rimary> rs.status () {"Set"  :  "Fuben", "Date"  : isodate ("2015-05-18t13 : 27:21z ")," MyState " : 1," members " : [{" _id " : 1," name " : " 192.168.3.100:27017 "," Health " : 1," state " : 1," Statestr " : " PRIMARY ", #主" uptime "  : 352, "Optime"  : timestamp (1431955576, 1), "Optimedate"  : isodate (" 2015-05-18t13:26:16z ")," Electiontime " : timestamp (1431955587, 1)," Electiondate " :  Isodate ("2015-05-18t13:26:27z"), "self"  : true},{"_id"  : 2, "name"  :  " 192.168.3.101:27017 "," Health " : 1," state " : 2," Statestr " : " secondary ", #从" uptime " : 64," Optime " : timestamp (1431955576, 1)," Optimedate " : isodate (" 2015-05-18t13:26:16z ")," Lastheartbeat " : isodate (" 2015-05-18t13:27:21z ")," Lastheartbeatrecv " :  isodate ("2015-05-18t13:27:20z"), "Pingms"  :&NBsp;0, "syncingto"  :  "192.168.3.100:27017"},{"_id"  : 3, "name"  :  " 192.168.3.102:27017 "," Health " : 1," state " : 2," Statestr " : " secondary ", #从" uptime " : 62," Optime " : timestamp (1431955576, 1)," Optimedate " : isodate (" 2015-05-18t13:26:16z ")," Lastheartbeat " : isodate (" 2015-05-18t13:27:21z ")," Lastheartbeatrecv " :  isodate ("2015-05-18t13:27:21z"), "Pingms"  : 0, "syncingto"  :  "192.168.3.100:27017"},{" _id " : 4," "Name"  :  "192.168.3.100:27018", "Health"  : 1, "state"  : 7, " Statestr " : " Arbiter ", #仲裁" uptime " : 62," Lastheartbeat " : isodate (" 2015-05-18t13 : 27:21z ")," Lastheartbeatrecv " : isodate (" 2015-05-18t13:27:21z ")," Pingms " : 0}]," OK " :  1}
#在PRIMARY上面操作, build a table from database, only Read permission Fuben:primary> show dbs;local1.078125gbfuben:primary> use  testdb;switched to db testsbfuben:primary> db.document01.insert ({"Name": "WSQ", " Sex ":" M "," Age ":";fuben:primary> show tables;document01system.indexesfuben:primary>  "}) Db.document01.find (); {  "_id"  : objectid ("5511551aa51f85ff07b6df0c"),  "name"  :  "wsq",  "Sex"   :  "M",  "Age"  :  "&nbsp";} #node1登陆mongo/application/mongodb/bin/mongo --port  27017# settings are readable from the library (performed from the library) fuben:secondary> show dbs;local1.078125gbtestdb0.203125gbfuben:secondary>  use testdb;switched to db testdbfuben:SECONDARY> show tables; tue mar 24 19:53:32.513 error: {  "$err"  :  "Not master and"  slaveok=false ", " code " : 13435 } at src/mongo/shell/query.js:128# Because there is no permission to read Fuben:secondarY> rs.slaveok (); #如果不执行该步骤从库上不可读数据fuben:secondary> show tables; Document01system.indexesfuben:secondary> db.document01.find (); {  "_id"  : objectid ("5511551aa51f85ff07b6df0c"),  "name"  :  "wsq",  "Sex"   :  "M",  "Age"  :  "&nbsp";}
 #配置副本集方法之二 (manually select a database as the primary database) #server1启动节点mkdir  -p /mongodb/data/ db /mongodb/data/logsmongod --rest --replset fuben/192.168.3.171:27017 --master  --port 27017 --dbpath=/mongodb/data/db --logpath=/mongodb/data/logs/mongodb.log --fork# Node1 Boot node mkdir -p /mongodb/data/db /mongodb/data/logsmongod --rest --replset  fuben/192.168.3.204:27017 --port 27017 --dbpath=/mongodb/data/db --logpath=/mongodb/data/ Logs/mongodb.log --fork#node2 Boot node mkdir -p /mongodb/data/db /mongodb/data/logsmongod - -rest --replset fuben/192.168.3.108:27017 --port 27017 --dbpath=/mongodb/data/db  --logpath=/mongodb/data/logs/mongodb.log --fork 
 #初始化节点: (login to any one of the node operations can be) [[email protected] /]# mongo  --port 27017mongodb shell version: 2.4.12connecting to: 127.0.0.1:27017/test>  rs.initiate ({_id: "Fuben", Members:[{_id:1,host: "192.168.3.171:27017", Priority:2},{_id:2,host: " 192.168.3.204:27017 ", Priority:3},{_id:3,host:" 192.168.3.108:27017 ", Priority:4}]}); {"Info"  :  "Config now saved locally.  should come online in"  about a minute. "," OK " : 1} #priority   is set priority, default priority is 1, can be 1-1000 number 
#验证fuben:P rimary> rs.status (); {"Set"  :  "Fuben", "Date"  : isodate ("2015-03-24t12:12:14z"), "MyState"  : 1, "members " : [{" _id " : 1", "name"  :  "192.168.10.171:27017", "Health"  : 1, "state"  : 2, "Statestr"  :  "secondary", #从 "uptime"  : 195, "Optime"  : timestamp ( 1427198934, 1), "Optimedate"  : isodate ("2015-03-24t12:08:54z"), "Lastheartbeat"  :  Isodate ("2015-03-24t12:12:13z"), "Lastheartbeatrecv"  : isodate ("2015-03-24t12:12:13z"), "PingMs"   :  0, "syncingto"  :  "192.168.10.108:27017"},{"_id"  : 2, "name"  :  " 192.168.10.204:27017 "," Health " : 1," state " : 2," Statestr " : " secondary ", #从" Uptime " : 195," Optime " : timestamp (1427198934, 1)," Optimedate " : isodate (" 2015-03-24t12:08:54z ")," Lastheartbeat " : isodate (" 2015-03-24t12:12:13z ")," Lastheartbeatrecv " : isodate ("2015-03-24t12:12:13z"), "Pingms"  : 0, "syncingto"  :  "192.168.10.171:27017"},{ "_id"  : 3, "name"  :  "192.168.10.108:27017", "Health"  : 1, "state"  : 1, " Statestr " : " PRIMARY ", #主" uptime " : 498," Optime " : timestamp (1427198934, 1), "Optimedate"  : isodate ("2015-03-24t12:08:54z"), "Self"  : true}], "OK"  : 1}
#查看当前主库fuben:secondary> Rs.ismaster (); {"SetName": "Fuben", "setversion": 1, "IsMaster": false, "secondary": True, "hosts": ["192.168.3.102:27017", " 192.168.3.101:27017 "," 192.168.3.100:27017 "]," arbiters ": [" 192.168.3.100:27018 "]," PRIMARY ":" 192.168.3.100:27017 " , "Me": "192.168.3.102:27017", "maxbsonobjectsize": 16777216, "maxmessagesizebytes": 48000000, "maxwritebatchsize": "LocalTime": Isodate ("2015-05-18t13:36:41.854z"), "Maxwireversion": 2, "minwireversion": 0, "OK": 1}


This article is from the "Wu Shanqiang" blog, make sure to keep this source http://shanqiangwu.blog.51cto.com/8067564/1653968

MongoDB Replica Collection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.