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 : <config> }
* { replSetReconfig: <config> }
* { replSetStepDown : <seconds> }
* { replSetFreeze : <seconds> }
查看状态:
rs.status()
rs.isMaster()
主从日志oplpg
rs就是通过oplog的记录写操作,他是一个固定长度的copped collection,存放于local数据库下,oplog的参数是可以通过--oplogSize来调整的,可以查看db.oplog.rs.find(),看到操作记录。
db.printReplicationInfo()查看元数据信息
db.printSlaveReplicationInfo()查看slave的同步状态
在local库下db.system.replset.find(),可以查询到主从库的信息=rs.conf()
默认从库是不可读的,如果需要来分担主库压力,可以打开读,举例:
[mongo@172_16_3_216 ~]$ mongo 127.0.0.1:20002
MongoDB shell version: 1.8.4
connecting to: 127.0.0.1:20002/test
myrs:SECONDARY> show collections
Mon Nov 14 15:20:30 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }
myrs:SECONDARY> db.get
db.getCollection( db.getLastErrorCmd( db.getName( db.getProfilingStatus( db.getSisterDB(
db.getCollectionNames( db.getLastErrorObj( db.getPrevError( db.getReplicationInfo(
db.getLastError( db.getMongo( db.getProfilingLevel( db.getSiblingDB(
myrs:SECONDARY> db.getMongo().setSlaveOk()
not master and slaveok=false
myrs:SECONDARY> show collections
hank
system.indexes
myrs:SECONDARY> db.hank.find()
{ "_id" : ObjectId("4ec0bdf9a9888d970cdc6197"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfba9888d970cdc6198"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfba9888d970cdc6199"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfca9888d970cdc619a"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfca9888d970cdc619b"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfca9888d970cdc619c"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0be05a9888d970cdc619d"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be06a9888d970cdc619e"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be06a9888d970cdc619f"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be07a9888d970cdc61a0"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be07a9888d970cdc61a1"), "name" : "zhang", "age" : 80 }
这样就可以读了