什么是复制?
保障数据的安全性
数据高可用性 (24*7)
灾难恢复
无需停机维护(如备份,重建索引,压缩)
分布式读取数据
故障自动转移
副本集配置
新建如下文件夹
启动三个mongodb节点(一个仲裁节点,两个对等节点)
第一个成员
./mongod --replSet myapp --dbpath /usr/local/mongodb-linux-x86_64-3.4.4/nodes/01/ --logpath /usr/local/mongodb-linux-x86_64-3.4.4/nodes/01/1.log --port 40000 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 3099
child process started successfully, parent exiting
第二个成员
./mongod --replSet myapp --dbpath /usr/local/mongodb-linux-x86_64-3.4.4/nodes/02/ --logpath /usr/local/mongodb-linux-x86_64-3.4.4/nodes/02/2.log --port 40001 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 3126
child process started successfully, parent exiting
仲裁成员
./mongod --replSet myapp --dbpath /usr/local/mongodb-linux-x86_64-3.4.4/nodes/arbiter/ --logpath /usr/local/mongodb-linux-x86_64-3.4.4/nodes/arbiter/3.log --port 40002 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 3154
child process started successfully, parent exiting
配置副本集节点
随便连接上其中一个非仲裁节点 , 这里连接上4000端口的,执行shell
执行命令
rs.initiate(
{
"_id":"myapp",
"members":[
{
"_id":0,
"host":"192.168.7.127:40000"
}
]
}
);
执行结果
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "localhost.localdomain:40000",
"ok" : 1
}
在添加另外一个节点+
rs.add("192.168.7.127:40001");
{
"ok" : 1
}
添加仲裁节点
rs.add("192.168.7.127:40002", {"arbiterOnly":true})
{
"ok" : 1
}
查看节点信息
db.isMaster()
{
"hosts" : [
"localhost.localdomain:40000",
"192.168.7.127:40001"
],
"arbiters" : [
"192.168.7.127:40002"
],
"setName" : "myapp",
"setVersion" : 3,
"ismaster" : true,
"secondary" : false,
"primary" : "localhost.localdomain:40000",
"me" : "localhost.localdomain:40000",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1494225341, 1),
"t" : NumberLong("1")
},
"lastWriteDate" : ISODate("2017-05-08T06:35:41.000Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2017-05-08T06:35:42.244Z"),
"maxWireVersion" : 5,
"minWireVersion" : 0,
"readOnly" : false,
"ok" : 1
}
更加详细的信息查看
rs.status()
{
"set" : "myapp",
"date" : ISODate("2017-05-08T06:36:54.793Z"),
"myState" : 1,
"term" : NumberLong("1"),
"heartbeatIntervalMillis" : NumberLong("2000"),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1494225411, 1),
"t" : NumberLong("1")
},
"appliedOpTime" : {
"ts" : Timestamp(1494225411, 1),
"t" : NumberLong("1")
},
"durableOpTime" : {
"ts" : Timestamp(1494225411, 1),
"t" : NumberLong("1")
}
},
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:40000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1382,
"optime" : {
"ts" : Timestamp(1494225411, 1),
"t" : NumberLong("1")
},
"optimeDate" : ISODate("2017-05-08T06:36:51.000Z"),
"electionTime" : Timestamp(1494224538, 2),
"electionDate" : ISODate("2017-05-08T06:22:18.000Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.7.127:40001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 522,
"optime" : {
"ts" : Timestamp(1494225411, 1),
"t" : NumberLong("1")
},
"optimeDurable" : {
"ts" : Timestamp(1494225411, 1),
"t" : NumberLong("1")
},
"optimeDate" : ISODate("2017-05-08T06:36:51.000Z"),
"optimeDurableDate" : ISODate("2017-05-08T06:36:51.000Z"),
"lastHeartbeat" : ISODate("2017-05-08T06:36:54.541Z"),
"lastHeartbeatRecv" : ISODate("2017-05-08T06:36:54.548Z"),
"pingMs" : NumberLong("0"),
"syncingTo" : "localhost.localdomain:40000",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "192.168.7.127:40002",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 128,
"lastHeartbeat" : ISODate("2017-05-08T06:36:54.541Z"),
"lastHeartbeatRecv" : ISODate("2017-05-08T06:36:51.512Z"),
"pingMs" : NumberLong("0"),
"configVersion" : 3
}
],
"ok" : 1
}
测试复制数据同步
添加一条测试数据到 40000
查看40001节点
到这里 就说明成功配置了 副本集
说明1:主节点 可以读写数据,从节点只能读取数据,无法写数据.
{
"code" : 10107,
"codeName" : "NotMaster",
"errmsg" : "not master",
"message" : "not master",
"name" : "MongoError",
"ok" : 0
}
说明2 : 主节点 挂掉 ,从节点自动升级为主节点,这时候可以进行读写,重启旧的主节点,那么旧的主节点改成从节点
副本集在驱动中的应用
当一个连接指向一个副本集的时候,会发送 isMaster 来自动辨别 是否为主节点.
如果主节点挂了,会重新获取主节点,从而进行自动转移故障.
List servers = new ArrayList();
servers.add(new ServerAddress("192.168.7.127", 40000));
servers.add(new ServerAddress("192.168.7.127", 40001));
MongoClient client = new MongoClient(servers);
移除节点
rs.remove("ip:port")