mongodb 副本集

分别在节点上配置好参数后如下
hapame-db:~ # cat /etc/mongod.conf |grep jieshi
replSet = jieshi
重启MongoDB服务器,并保证服务器没有别的数据库(除admin
local),如果有其他数据库,在这一步rs.initiate(hapameconfig),会报错。
任意登入其中一台MongoDB服务器,做一下配置:
hapame-db:~ # mongo
MongoDB shell version: 1.6.5-rc
connecting to: test
> show dbs
admin
local
> hapameconfig= {_id:"jieshi", members: [
... {_id:0,host:"192.168.16.234"},
... {_id:1,host:"192.168.16.103"},
... {_id:2,host:"192.168.16.216"}
... ]}
{
 "_id" : "jieshi",
 "members" : [
  {
   "_id" : 0,
   "host" : "192.168.16.234"
  },
  {
   "_id" : 1,
   "host" : "192.168.16.103"
  },
  {
   "_id" : 2,
   "host" : "192.168.16.216"
  }
 ]
}
> rs.initiate(hapameconfig)
{
 "info" : "Config now saved locally.  Should come online in about a minute.",
 "ok" : 1
}
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 1, "members" : [
 {
  "_id" : 0,
  "host" : "192.168.16.234"
 },
 {
  "_id" : 1,
  "host" : "192.168.16.103"
 },
 {
  "_id" : 2,
  "host" : "192.168.16.216"
 }
] }
在192.168.16.234服务器上:(SUSE Linux Enterprise Server 11 (x86_64))
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : true,
 "secondary" : false,
 "hosts" : [
  "192.168.16.234",
  "192.168.16.216",
  "192.168.16.103"
 ],
 "ok" : 1
}
> rs.status()
{
 "set" : "jieshi",
 "date" : "Fri Dec 10 2010 12:48:46 GMT+0800 (CST)",
 "myState" : 1,
 "members" : [
  {
   "_id" : 0,
   "name" : "hapame-db:27017",
   "health" : 1,
   "state" : 1,
   "self" : true
  },
  {
   "_id" : 1,
   "name" : "192.168.16.103",
   "health" : 1,
   "state" : 2,
   "uptime" : 133,
   "lastHeartbeat" : "Fri Dec 10 2010 12:48:45 GMT+0800 (CST)"
  },
  {
   "_id" : 2,
   "name" : "192.168.16.216",
   "health" : 1,
   "state" : 2,
   "uptime" : 127,
   "lastHeartbeat" : "Fri Dec 10 2010 12:48:45 GMT+0800 (CST)"
  }
 ],
 "ok" : 1
}
在192.168.16.103服务器上:(CentOS release 5.5)
[root@jieshiyeskey ~]# cat /etc/mongod.conf |grep jieshi
replSet=jieshi
[root@jieshiyeskey mongo]# mongo
MongoDB shell version: 1.6.4
connecting to: test
> use local
switched to db local
> show collections
me
oplog.rs
replset.minvalid
system.indexes
system.replset
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 1, "members" : [
 {
  "_id" : 0,
  "host" : "192.168.16.234"
 },
 {
  "_id" : 1,
  "host" : "192.168.16.103"
 },
 {
  "_id" : 2,
  "host" : "192.168.16.216"
 }
] }
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : false,
 "secondary" : true,
 "hosts" : [
  "192.168.16.103",
  "192.168.16.216",
  "192.168.16.234"
 ],
 "primary" : "192.168.16.234",
 "ok" : 1
}
> rs.status()
{
 "set" : "jieshi",
 "date" : "Fri Dec 10 2010 12:52:17 GMT+0800 (CST)",
 "myState" : 2,
 "members" : [
  {
   "_id" : 0,
   "name" : "192.168.16.234",
   "health" : 1,
   "state" : 1,
   "uptime" : 344,
   "lastHeartbeat" : "Fri Dec 10 2010 12:52:16 GMT+0800 (CST)"
  },
  {
   "_id" : 1,
   "name" : "jieshiyeskey.hapaworld.com.cn:27017",
   "health" : 1,
   "state" : 2,
   "self" : true
  },
  {
   "_id" : 2,
   "name" : "192.168.16.216",
   "health" : 1,
   "state" : 2,
   "uptime" : 338,
   "lastHeartbeat" : "Fri Dec 10 2010 12:52:16 GMT+0800 (CST)"
  }
 ],
 "ok" : 1
}
在192.168.16.216服务器上:(FreeBSD 8.1-RELEASE)
[root@jieshi ~]# cat /usr/local/etc/mongodb.conf |grep jieshi
replSet=jieshi
[root@jieshi /var/db/mongodb]# mongo
MongoDB shell version: 1.6.3
connecting to: test
> show dbs
admin
local
> use local
switched to db local
> show collections
me
oplog.rs
replset.minvalid
system.indexes
system.replset
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 1, "members" : [
 {
  "_id" : 0,
  "host" : "192.168.16.234"
 },
 {
  "_id" : 1,
  "host" : "192.168.16.103"
 },
 {
  "_id" : 2,
  "host" : "192.168.16.216"
 }
] }
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : false,
 "secondary" : true,
 "hosts" : [
  "192.168.16.216",
  "192.168.16.103",
  "192.168.16.234"
 ],
 "primary" : "192.168.16.234",
 "ok" : 1
}
> rs.status()
{
 "set" : "jieshi",
 "date" : "Fri Dec 10 2010 04:58:15 GMT+0800 (CST)",
 "myState" : 2,
 "members" : [
  {
   "_id" : 0,
   "name" : "192.168.16.234",
   "health" : 1,
   "state" : 1,
   "uptime" : 101,
   "lastHeartbeat" : "Fri Dec 10 2010 04:58:14 GMT+0800 (CST)"
  },
  {
   "_id" : 1,
   "name" : "192.168.16.103",
   "health" : 1,
   "state" : 2,
   "uptime" : 101,
   "lastHeartbeat" : "Fri Dec 10 2010 04:58:14 GMT+0800 (CST)"
  },
  {
   "_id" : 2,
   "name" : "jieshi.hapame.com.cn:27017",
   "health" : 1,
   "state" : 2,
   "self" : true
  }
 ],
 "ok" : 1
}
@@添加新成员192.168.16.211:(Red Hat Enterprise Linux Server release 6.0 (Santiago))
[root@6 mongo]# cat /etc/mongod.conf  |grep jieshi
replSet=jieshi
root@6 mongo]# /etc/init.d/mongod start
Starting mongod: forked process: 8131
all output going to: /var/log/mongo/mongod.log
                                                           [  OK  ]
[root@6 mongo]# tail /var/log/mongo/mongod.log
Fri Dec 10 13:18:50 [startReplSets] replSet can‘t get local.system.replset config from self or any seed (EMPTYCONFIG)
Fri Dec 10 13:18:54 [snapshotthread] cpu: elapsed:4000  writelock: 0%
Fri Dec 10 13:18:58 [snapshotthread] cpu: elapsed:4000  writelock: 0%
Fri Dec 10 13:19:00 [startReplSets] replSet can‘t get local.system.replset config from self or any seed (EMPTYCONFIG)
Fri Dec 10 13:19:02 [snapshotthread] cpu: elapsed:4000  writelock: 0%
Fri Dec 10 13:19:06 [snapshotthread] cpu: elapsed:4000  writelock: 0%
Fri Dec 10 13:19:10 [snapshotthread] cpu: elapsed:4000  writelock: 0%
Fri Dec 10 13:19:10 [startReplSets] replSet can‘t get local.system.replset config from self or any seed (EMPTYCONFIG) ##每个节点上都会报这个警告
Fri Dec 10 13:19:14 [snapshotthread] cpu: elapsed:4000  writelock: 0%
Fri Dec 10 13:19:18 [snapshotthread] cpu: elapsed:4000  writelock: 0%
要在primary 上才能添加新成员:(当前为192.168.16.234为primary)
hapame-db:~ # mongo
MongoDB shell version: 1.6.5-rc1
connecting to: test
> rs.add("192.168.16.211")
{ "ok" : 1 }
> rs.conf()
{
 "_id" : "jieshi",
 "version" : 2,
 "members" : [
  {
   "_id" : 0,
   "host" : "192.168.16.234"
  },
  {
   "_id" : 1,
   "host" : "192.168.16.103"
  },
  {
   "_id" : 2,
   "host" : "192.168.16.216"
  },
  {
   "_id" : 3,
   "host" : "192.168.16.211"
  }
 ]
}
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : true,
 "secondary" : false,
 "hosts" : [
  "192.168.16.234",
  "192.168.16.211",
  "192.168.16.216",
  "192.168.16.103"
 ],
 "ok" : 1
}
> rs.status()
{
 "set" : "jieshi",
 "date" : "Fri Dec 10 2010 13:23:10 GMT+0800 (CST)",
 "myState" : 1,
 "members" : [
  {
   "_id" : 0,
   "name" : "hapame-db:27017",
   "health" : 1,
   "state" : 1,
   "self" : true
  },
  {
   "_id" : 1,
   "name" : "192.168.16.103",
   "health" : 1,
   "state" : 2,
   "uptime" : 46,
   "lastHeartbeat" : "Fri Dec 10 2010 13:23:08 GMT+0800 (CST)"
  },
  {
   "_id" : 2,
   "name" : "192.168.16.216",
   "health" : 1,
   "state" : 2,
   "uptime" : 46,
   "lastHeartbeat" : "Fri Dec 10 2010 13:23:08 GMT+0800 (CST)"
  },
  {
   "_id" : 3,
   "name" : "192.168.16.211",
   "health" : 1,
   "state" : 2,
   "uptime" : 38,
   "lastHeartbeat" : "Fri Dec 10 2010 13:23:08 GMT+0800 (CST)"
  }
 ],
 "ok" : 1
}
>
在192.168.16.211上查看如下:
[[email protected] mongo]# mongo
MongoDB shell version: 1.6.4
connecting to: test
> show dbs
admin
local
> use local
switched to db local
> show collections
me
oplog.rs
replset.minvalid
system.indexes
system.replset
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 2, "members" : [
 {
  "_id" : 0,
  "host" : "192.168.16.234"
 },
 {
  "_id" : 1,
  "host" : "192.168.16.103"
 },
 {
  "_id" : 2,
  "host" : "192.168.16.216"
 },
 {
  "_id" : 3,
  "host" : "192.168.16.211"
 }
] }
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : false,
 "secondary" : true,
 "hosts" : [
  "192.168.16.211",
  "192.168.16.216",
  "192.168.16.103",
  "192.168.16.234"
 ],
 "primary" : "192.168.16.234",
 "ok" : 1
}
> rs.status()
{
 "set" : "jieshi",
 "date" : "Fri Dec 10 2010 13:24:54 GMT+0800 (CST)",
 "myState" : 2,
 "members" : [
  {
   "_id" : 0,
   "name" : "192.168.16.234",
   "health" : 1,
   "state" : 1,
   "uptime" : 142,
   "lastHeartbeat" : "Fri Dec 10 2010 13:24:52 GMT+0800 (CST)"
  },
  {
   "_id" : 1,
   "name" : "192.168.16.103",
   "health" : 1,
   "state" : 2,
   "uptime" : 142,
   "lastHeartbeat" : "Fri Dec 10 2010 13:24:52 GMT+0800 (CST)"
  },
  {
   "_id" : 2,
   "name" : "192.168.16.216",
   "health" : 1,
   "state" : 2,
   "uptime" : 142,
   "lastHeartbeat" : "Fri Dec 10 2010 13:24:52 GMT+0800 (CST)"
  },
  {
   "_id" : 3,
   "name" : "6.hapame.com:27017",
   "health" : 1,
   "state" : 2,
   "self" : true
  }
 ],
 "ok" : 1
}
测试:
在192.168.16.234上:
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : true,   #表示为primary
 "secondary" : false,
 "hosts" : [
  "192.168.16.234",
  "192.168.16.211",
  "192.168.16.216",
  "192.168.16.103"
 ],
 "ok" : 1
}
在primary可以有全部的权限,但在辅上就没有全部的权限如下:
 > show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
hapameworld
system.indexes
> db.hapameworld.find()
{ "_id" : ObjectId("4d01bed1356d93b46d48ba34"), "name" : "markgeng" }
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
Fri Dec 10 14:46:57 uncaught exception: error: { "$err" : "not master", "code" : 10107 }

现在关掉192.168.16.234服务器:
hapame-db:~ # ps -ef |grep mongod
root      2377     1  7 12:35 ?        00:09:55 /opt/mongodb/bin/mongod -f /etc/mongod.conf
root     11035 10139  0 14:51 pts/5    00:00:00 grep mongod
hapame-db:~ # killall mongod
hapame-db:~ # ps -ef |grep mongod
root     11039 10139  0 14:51 pts/5    00:00:00 grep mongod
现在192.168.16.211服务器为primary如下:
[[email protected] ~]# mongo
MongoDB shell version: 1.6.4
connecting to: test
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : true,
 "secondary" : false,
 "hosts" : [
  "192.168.16.211",
  "192.168.16.216",
  "192.168.16.103",
  "192.168.16.234"
 ],
 "ok" : 1
}
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
hapameworld
system.indexes
> db.hapameworld.find()
{ "_id" : ObjectId("4d01bed1356d93b46d48ba34"), "name" : "markgeng" }
> db.hapameworld.insert({name:"jieshi"})
> db.hapameworld.find()
{ "_id" : ObjectId("4d01bed1356d93b46d48ba34"), "name" : "markgeng" }
{ "_id" : ObjectId("4d01d812b68ce41287454183"), "name" : "jieshi" }
在辅的服务器上:
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
Fri Dec 10 15:36:13 uncaught exception: error: { "$err" : "not master", "code" : 10107 }
现在开启以前的primary:(192.168.16.234)
hapame-db:~ # mongod -f /etc/mongod.conf
hapame-db:~ # forked process: 12997
all output going to: /opt/mongodb_logs/mongod.log
hapame-db:~ # ps -ef |grep mongod
root     12997     1  0 15:37 ?        00:00:00 mongod -f /etc/mongod.conf
root     13039 12417  0 15:37 pts/2    00:00:00 grep mongod
hapame-db:~ # mongo
MongoDB shell version: 1.6.5-rc1
connecting to: test
> rsMaster()
Fri Dec 10 15:38:10 ReferenceError: rsMaster is not defined (shell):0
> rs.isMaster()
{
 "setName" : "jieshi",
 "ismaster" : false, #已变成辅,primary并不会自己迁移回来
 "secondary" : true,
 "hosts" : [
  "192.168.16.234",
  "192.168.16.211",
  "192.168.16.216",
  "192.168.16.103"
 ],
 "primary" : "192.168.16.211",
 "ok" : 1
}
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
Fri Dec 10 15:38:43 uncaught exception: error: { "$err" : "not master", "code" : 10107 }
时间: 2024-10-24 03:45:01

mongodb 副本集的相关文章

MongoDB副本集

简介 mongodb复制(replication)是将数据同步在多个服务器的过程.主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致.复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并保证数据的安全性.复制还允许您从硬件故障和服务中断中恢复数据. 而副本集(replica set)是从mongodb 1.6 提供的新功能,比复制功能要强大一些并增加了故障自动切换和自动修复成员节点,

Mongodb副本集实现

MongoDB副本集概述 以下图片摘自MongoDB官方文档:http://docs.mongodb.org/manual/core/replication-introduction/ Primary节点接收客户端所有的写操作,整个副本集只会有一个primary节点.MongoDB副本集提供严格的一致性.主节点将所有的操作写入一个叫oplog的capped collection(这个collection的大小一般为磁盘剩余空间的5%,不同的系统可能不一样,详见http://docs.mongod

zabbix使用Python实现监控MongoDB副本集状态

公司有 Windows 和 Linux 服务器,都搭建了 MongoDB 副本集,并且都要在 zabbix 平台中实现监控.Linux 系统直接使用 shell 脚本即可实现,但是 Windows 系统的不太好实现,我这里使用 Python 来实现.下面脚本同样适用于Linux系统(在 Windows server 2012 和 Centos7.3 系统都验证成功) 思路: 1.安装Python2.7 2.采用 Python 的 pymongo 模块来连接 mongodb 数据库,并认证授权 3

mongodb副本集维护

mongodb副本集维护主要工作: 1.查看副本集状态(集群状态.同步延迟.单个库的运行状态mongostate) 2.增删节点.停节点shutdown mongodb副本集集群同步机制 数据复制的目的是使数据得到最大的可用性,冗余,避免单点故障. 副本集中同一时刻只有一台服务器是可以写的,primary主库上写,从库同步数据 副本集主从复制也是异步同步的过程.slave从primary上获取日志,然后在自己身上完全顺序的执行日志记录的操作(该日志不记录查询操作,只记录更新操作).被同步的日志就

mongodb 副本集配置与说明

1,副本集的原理 副本集的原理与主从很相似,唯一不同的是,在主节点出现故障的时候,主从配置的从服务器不会自动的变为主服务器,而是要通过手动修改配置.但是副表集就不用,它会自动选出一台服务器做为主节点,从而保障系统的稳定性. 2,副本集新的主节点是怎么选举出来的呢 是通过bully算法来的,也就是一致性协议.具体如下 1):当主节点挂了后,副本集会获得其他从节点的最后更新时间与主服务做对比 2):如果所有从节点的最后更新时间都是很旧,那就选举停止 3):如果副本集中的大部分服务器挂了,包含主节点,

Mongodb副本集实现及读写分离

在前面的文章"Mongodb的主从模式搭建实例"中,我们对如何搭建一个主从结构的Mongodb服务器环境进行了简单的介绍.但是对于主从结构,Mongodb官方并不推荐我们使用了,可能是因为主从模式存在以下两个缺点: (1)主节点不可用之后,无法自动切换到从节点,无法确保业务访问的不间断性: (2)所有的读写操作都是对主节点的,造成主节点的访问压力较大: 因此,Mongodb为我们提供了另外一种推荐的使用方法,那就是使用副本集ReplicaSets.在这篇文章中简单描述一下副本集是如何实

MongoDB 副本集(类似高可用) [三]

MongoDB 副本集(类似高可用)1.节点类型standard:常规节点,它存储一份完整的数据副本,参与选举投票,有可能成为活跃节点.passive:存储了完整的数据副本,参与投票,不能成为活跃节点.arbiter:仲裁节点,只参与投票,不接收复制的数据,也不能成为活跃节点.2.参数说明--dbpath   数据文件路径--logpath  日志文件路径--port        端口号,默认是27017.我这里使用的也是这个端口号.--replSet   复制集的名字,一个replica s

mongodb副本集的内部机制(借鉴lanceyan.com)

针对mongodb的内部机制提出以下几个引导性的问题: 副本集故障转移,主节点是如何选举的?能否手动干涉下架某一台主节点. 官方说副本集数量最好是奇数,为什么? mongodb副本集是如何同步的?如果同步不及时会出现什么情况?会不会出现不一致性? mongodb的故障转移会不会无故自动发生?什么条件会触发?频繁触发可能会带来系统负载加重? Bully算法 mongodb副本集故障转移功能得益于它的选举机制.选举机制采用了Bully算法,可以很方便从分布式节点中选出主节点.一个分布式集群架构中一般

MongoDB副本集搭建及备份恢复

一.MongoDB副本集(repl set)介绍 早起版本使用master-slave,一主一从和MySQL类似,但slave在此架构中为只读,当主库宕机后,从库不能自动切换为主: 目前已经淘汰了master-slave模式,改为副本集,这种模式下有一个主(primary),和多个从(secondary),只读,支持给他们设置权重,当主宕掉后,权重最高的从切换为主: 在此架构中还可以建立一个仲裁(arbiter)的角色,它只负责裁决,而不存储数据 在此架构中读写数据都是在主上,要想实现负载均衡的

java程序连接MongoDB副本集测试

三个节点有一个节点挂掉也不会影响应用程序客户端对整个副本集的读写! [java] view plaincopy public class TestMongoDBReplSet { public static void main(String[] args) { try { List<ServerAddress> addresses = new ArrayList<ServerAddress>(); ServerAddress address1 = new ServerAddress