MongoDB的数据复制分片(3)

testset:PRIMARY> use test
switched to db test
testset:PRIMARY> for (i=1;i<=1000;i++) db.students.insert({name:"student"+i,age:(i%100)})
WriteResult({ "nInserted" : 1 })
连接到从节点来看一下
[root@node1 ~]# mongo   
MongoDB shell version: 2.6.4   
connecting to: test   
Welcome to the MongoDB shell.   
For interactive help, type "help".   
For more comprehensive documentation, see   
   
Questions? Try the support group   
   
testset:SECONDARY> use test   
switched to db test   
testset:SECONDARY> show collections   
2014-10-11T14:52:57.103+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131   
#提示需要不是主节点,没有slaveOK不让查看,那么便在当前节点指定slaveOK,便可以了
testset:SECONDARY> rs.slaveOk()
testset:SECONDARY> show collections
students
system.indexes
可以使用rs.isMaster()查询主节点是谁
testset:SECONDARY> rs.isMaster()
{
"setName" : "testset",
"setVersion" : 3,
"ismaster" : false,
"secondary" : true,
"hosts" : [
  "node1.linuxidc.com:27017",
  "node3.linuxidc.com:27017",
  "node2.linuxidc.com:27017"
],
"primary" : "node2.linuxidc.com:27017",        #主节点是谁
"me" : "node1.linuxidc.com:27017",              #自己是谁
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2014-10-11T07:00:03.533Z"),
"maxWireVersion" : 2,
"minWireVersion" : 0,
"ok" : 1
}

1.6如果主节点离线,从节点会自动选出主节点

testset:PRIMARY> rs.stepDown()
2014-10-12T17:00:12.869+0800 DBClientCursor::init call() failed
2014-10-12T17:00:12.896+0800 Error: error doing query: failed at src/mongo/shell/query.js:81
2014-10-12T17:00:12.914+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2014-10-12T17:00:12.945+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok
testset:SECONDARY>

可以看到主节点的primary变成了secondary

其他两个节点中的一个变为主节点,这就是自动转移

副本集的重新选举的条件有:心态信息,优先级,optime,网络连接等

1.7还可以修改优先级来实现主从切换

testset:PRIMARY> rs.conf()            #查看配置
{
"_id" : "testset",
"version" : 3,
"members" : [
  {
  "_id" : 0,
  "host" : "node2.linuxidc.com:27017"
  },
  {
  "_id" : 1,
  "host" : "node1.linuxidc.com:27017"
  },
  {
  "_id" : 2,
  "host" : "node3.linuxidc.com:27017"
  }
]
}
testset:PRIMARY> cfg=rs.conf()            #将配置信息保存在变量中
{
"_id" : "testset",
"version" : 3,
"members" : [
  {
  "_id" : 0,
  "host" : "node2.linuxidc.com:27017"
  },
  {
  "_id" : 1,
  "host" : "node1.linuxidc.com:27017"
  },
  {
  "_id" : 2,
  "host" : "node3.linuxidc.com:27017"
  }
]
}
testset:PRIMARY> cfg.members[1].priority=2            #修改id=1的主机优先级为2
2
testset:PRIMARY> rs.reconfig(cfg)                              #应用cfg文件
2014-10-12T05:15:59.916-0400 DBClientCursor::init call() failed
2014-10-12T05:15:59.988-0400 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2014-10-12T05:16:00.015-0400 reconnect 127.0.0.1:27017 (127.0.0.1) ok
reconnected to server after rs command (which is normal)
testset:SECONDARY>                                                    #主动变为从节点
而id=1的是node1.linuxidc.com主机,自己变为primary主机
testset:SECONDARY>
testset:PRIMARY>

1.8,如何要设定仲裁节点

rs.addArb(hostportstr)表示将一个节点加进来时就是仲裁节点。

我们移除当前的node3节点

testset:PRIMARY> rs.remove("node3.linuxidc.com")

删除/mongodb/data下的数据,重新初始化,

testset:PRIMARY> rs.addArb("node3.linuxidc.com")

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/65f9bad25a5a9b6521a141109fddbd95.html