MongoDB的主从复制其实很简单,就是在运行主的服务器上开启mongod进程时,加入参数--master即可,
在运行从的服务器上开启mongod进程时,加入--slave 和 --source 指定主即可,
这样,在主数据库更新时,数据被复制到从数据库中
--主节点启动
[root@tmg-73 dbs]# mongod -f mongo_10000.conf
[root@tmg-73 dbs]# cat mongo_10000.conf
fork = true
bind_ip = 127.0.0.1
port = 10000
dbpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/master
logpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/log/mongo.log
logappend = true
journal = true
#auth = true
master=true
[root@tmg-73 dbs]# mongo localhost:10000
--从节点
[root@tmg-73 dbs]# mongod -f mongo_10001.conf
[root@tmg-73 dbs]# vi mongo_10001.conf
fork = true
bind_ip = 127.0.0.1
port = 10001
dbpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/slave1
logpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/log/slave1.log
logappend = true
journal = true
#auth = true
slave=true
source=localhost:10000
--测试主从
> show dbs
> use demo
> show dbs
> db.hits.insert({"url":"","pv":102});
注:可以在启动从时加以下常用参数
–slavedelay 10 #延时复制 单位为秒
–autoresync #自动重新同步
–only #复制指定的数据库,默认复制所有的库
–oplogSize #主节点的oplog日志大小,单位为M,建议设大点(更改oplog大小时,只需停主库,删除local.*,
然后加–oplogSize=* 重新启动即可,*代表大小)
--如果发现主从不同步,从上手动同步
db.runCommand({"resync":1})
--状态查询 #主还是从
db.runCommand({"isMaster":1})
--#查看各Collection状态
db.printCollectionStats();
--#查看主从复制状态
db.printReplicationInfo();
--添加及删除源
启动从节点时可以用--source指定主节点,也可以在shell中配置这个源.
如上例主节点绑定了127.0.0.1:10000.启动从节点时可以不添加源,而后向source集合添加主节点信息:
--可以在shell中,将127.0.0.1:10000作为源添加到从节点上:
[root@tmg-73 dbs]# vi mongo_10003.conf
fork = true
bind_ip = 127.0.0.1
port = 10003
dbpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/slave3
logpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/log/slave3.log
logappend = true
journal = true
slave=true
[root@tmg-73 dbs]# mongo localhost:10003
use local
db.sources.insert({"host":"127.0.0.1:10000"})
show dbs
--副本集
--修改mongod 10000,初始化参数
replSet=blort
--启动mongod 10004
[root@tmg-73 dbs]# mongod -f mongo_10004.conf
--配置如下
[root@tmg-73 dbs]# vi mongo_10004.conf
fork = true
bind_ip = 127.0.0.1
port = 10004
dbpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/replicat1
logpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/log/replicat1.log
logappend = true
journal = true
replSet=blort
--初始化副本集
rs.initiate({"_id" : "blort","members" : [
{"_id" : 1,"host" : "127.0.0.1:10000"},
{"_id" : 2,"host" : "127.0.0.1:10004"}
]})
--增加新节点
--启动新mongod节点
[root@tmg-73 dbs]# cat mongo_10001.conf
fork = true
bind_ip = 127.0.0.1
port = 10001
dbpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/slave1
logpath = /home1/mongodb/mongodb-linux-x86_64-2.2.1/dbs/log/slave1.log
logappend = true
journal = true
replSet=blort
--连接primary节点,执行下面的命令。
rs.add('localhost:10001')
rs.addArb('localhost:10001')
//重新配置
rs.reconfig(rs.conf())
--非主节点不能插入,一般不超过12个节点可运行良好
--db.getMongo().setSlaveOk() 设置slave节点可读
blort:SECONDARY> 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.syncFrom(hostportstr) make a secondary to sync from the given member
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
阅读(661) | 评论(0) | 转发(0) |