设为首页 加入收藏

TOP

mongodb replication参数(一)
2014-11-24 07:16:28 来源: 作者: 【 】 浏览:6
Tags:mongodb replication 参数
mongodb replication参数

1:master-slave replication
主从复制:一个服务启动的时候加上--master参数,另一个服务启动时加上--slave和--source参数就可以实现同步了。
一个master服务可以有一个或者多个slave,每一个salve都知道master的地址,如图:

举例:
启动master
$ mkdir -p ~/dbs/master
$ ./mongod --dbpath ~/dbs/master --port 10000 --master
建立相关目录,启动的时候加上--master即可
启动slave
$ mkdir -p ~/dbs/slave
$ ./mongod --dbpath ~/dbs/slave --port 10001 --slave --source localhost:10000
建立目录,启动这里的localhost就是上面master的地址,不管是一个slave还是多个,都是从master进行复制
Adding and Removing Sources:
$ ./mongod --slave --dbpath ~/dbs/slave --port 27018 --没有指定source
启动之后如果要加入一个slave,localhost:27017为master
> use local
> db.sources.insert({"host" : "localhost:27017"})
2:replica sets 复制集
这个是1.6开发的新功能,与之前的主从复制比起来,功能更加强大,多了故障转移和自动修复节点,每个DB的数据完全一致。
建立相关目录:
数据目录:mkdir -p /mongodb1.8/data1/
mkdir -p /mongodb1.8/data2/
mkdir -p /mongodb1.8/data3/
日志文件:touch /mongodb1.8/RS.log /mongodb1.8/RS1.log /mongodb1.8/RS2.log
创建主从KEY文件:mkdir -p /mongodb1.8/key
echo "this is rs1 super secret key" > /mongodb1.8/key/r1
echo "this is rs1 super secret key" > /mongodb1.8/key/r2
echo "this is rs1 super secret key" > /mongodb1.8/key/r3
这里KEY的权限400
chmod 400 /mongodb1.8/key/r1 /mongodb1.8/key/r2 /mongodb1.8/key/r3
启动一个server,记得/etc/hosts文件里面包含localhost记录,否则无法解析
mongod --replSet rs1/localhost:20001 --keyFile /mongodb1.8/key/r1 --fork --port 20001 --dbpath /mongodb1.8/data1/ --logpath=/mongodb1.8/RS.log
mongod --replSet rs1/localhost:20002 --keyFile /mongodb1.8/key/r2 --fork --port 20002 --dbpath /mongodb1.8/data2/ --logpath=/mongodb1.8/RS1.log

mongod --replSet rs1/localhost:20003 --keyFile /mongodb1.8/key/r3 --fork --port 20003 --dbpath /mongodb1.8/data3/ --logpath=/mongodb1.8/RS2.log
mongo localhost:20001/admin
config_rs1 = {_id: 'rs1', members: [
... {_id: 0, host: 'localhost:20001', priority:1}, --成员IP 及端口,priority=1 指PRIMARY
... {_id: 1, host: 'localhost:20002'},
... {_id: 2, host: 'localhost:20003'}]
... }
初始化配置:
rs.initiate(config_rs1);
或者db.runCommand({"replSetInitiate" : {
... "_id" : "rs1",
... "members" : [
... {
... "_id" : 0,
... "host" : "localhost:20001"
... },
... {
... "_id" : 1,
... "host" : "localhost:20002"
... },
... {
... "_id" : 2,
... "host" : "localhost:20003"
... }
... ]}})
rs1:PRIMARY> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([secs]) step down as primary (momentarily) (disconnects)
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() shorthand for db.getMongo().setSlaveOk()
db.isMaster() check who is primary
或者使用命令:
db.runCommand({})
* { isMaster : 1 }
* { replSetGetStatus : 1 }
* { replSetInitiate : }
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hive中使用标准sql实现分组内排序 下一篇在一张表中查询出一个字段相同,一..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)