且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【Mongodb】Master-Slave 复制

更新时间:2022-02-11 19:38:17

mongodb的主从复制是最常见的复制模式,这种模式是非常灵活的并且可以用来做备份,failover 故障切换, 拓展读等等,最常见的方式是一主一备或者一主多备,每个slave 都知道主库的地址 即主库的ip地址和端口号。
如下图:
【Mongodb】Master-Slave 复制
                                                一主一备模式
【Mongodb】Master-Slave 复制
                                                   一主多备 模式
mongodb 启动master 的方式是 
./mongod --master 
启动slave 模式执行
./mongod --slave --source master_address (主库的ip地址和端口)
例如 例子是在同一个主机:
主库:./mongod --master --dbpath=/opt/monogdata/data --port=60000
从库:./mongod --slave --dbpath=/opt/monogdata/slavedata/ --port=60010 --source=127.0.0.1:60000
异机的例子:
主库:./mongod --master --dbpath=/opt/monogdata/data --port=60000
从库:./mongod --slave --dbpath=/opt/monogdata/slavedata/ --port=60010 --source=10.250.7.220:60000
所有的slave库必须从一个主库进行复制,到目前为止,现在的版本还不支持从备库进行复制即级联形式的复制,因为slaves 并不保持他们自己的oplog。对于一对多形式的主从复制对于从库并没有数量的限制,但是设置从库时必须考虑主库IO方面的压力。
下面介绍一些比较重要的且常见的选项
--slavedelay 
  延迟应用主库的oplog,和oracle 的dataguard 中的 delay 关键字的作用一直,单位为秒。如果在主库进行误操作,设置延迟应用,这样可以一个维护窗口进行恢复数据。
--fastsync 
 Start a slave from a snapshot of the master node. This option allows a slave to bootstrap much faster than doing a full sync, 
 if its data directory is initialized with a snapshot of the master’s data. 
##以主库的一个快照为基础,启动一个slave。当快照中已经保存了主库的数据字典,这样可以比从库进行完全同步要快##
--autoresync 
   ##自动执行一个完整的同步操作##
  Automatically perform. a full resync if this slave gets out of sync with the master 
--oplogSize 
  ##主库的oplog的大小以M 为单位。## 
添加和删除源库
之前说过可以在开始启动的时候直接指定 --source 来标识主库,也可以在启动slave之后,在local 库中添加主(源)库的信息,比如:
加入在本机上已经有一个主库在localhost:27017端口,我们可以启动一个不带 --source参数的slave 
$ ./mongod --slave --dbpath ~/dbs/slave --port 27018 
启动之后,执行添加操作:
> use local 
> db.sources.insert({"host" : "localhost:27017"}) 
例子:
在主库:
[mongodb@rac2 bin]$ ./mongod --master -port=27017 --dbpath=/opt/mongodata  &
[1] 6715
[mongodb@rac2 bin]$ Sun Oct 30 17:34:08 [initandlisten] MongoDB starting : pid=6715 port=27017 dbpath=/opt/mongodata master=1 64-bit host=rac2
[mongodb@rac2 bin]$ ./mongo 127.0.0.1:27017
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27017/test
> db.yql.insert({id:1,val:"this is on master!"});
> db.yql.find();
{ "_id" : ObjectId("4ead1b21da034cce224fca6f"), "id" : 1, "val" : "this is on master!" }
> db.yql.insert({id:1,val:"this is on master and this message will be send to slave!"});
> db.yql.find();
{ "_id" : ObjectId("4ead1b21da034cce224fca6f"), "id" : 1, "val" : "this is on master!" }
{ "_id" : ObjectId("4ead1bd7da034cce224fca70"), "id" : 1, "val" : "this is on master and this message will be send to slave!" }
在备库
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27018
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27018/test
> use local
switched to db local
> db.sources.insert({"host" : "10.250.7.249:27017"}); 
> db.sources.find();
{ "_id" : ObjectId("4ead1b68b868f3c88da3b06f"), "host" : "10.250.7.249:27017", "source" : "main", "syncedTo" : { "t" : 1319967596000, "i" : 1 }, "dbsNextPass" : { "test" : true }, "incompleteCloneDbs" : { "test" : true } }
> use test
switched to db test
> db.yql.find();
{ "_id" : ObjectId("4ead1b21da034cce224fca6f"), "id" : 1, "val" : "this is on master!" }
> db.yql.find();
{ "_id" : ObjectId("4ead1b21da034cce224fca6f"), "id" : 1, "val" : "this is on master!" }
{ "_id" : ObjectId("4ead1bd7da034cce224fca70"), "id" : 1, "val" : "this is on master and this message will be send to slave!" }
如果你要更换master ,比如从 10.250.7.249 更换为 10.249.197.220 则执行
> db.sources.insert({"host" : "10.250.7.220:27017"}); 
> db.sources.remove({"host" : "10.250.7.249:27017"});
####但我实验的过程中是如下情况,remove 删除不了!???#####
> db.sources.find();
{ "_id" : ObjectId("4ead250e6c7e8620f17d8ebb"), "host" : "10.250.7.249:27017", "source" : "main", "syncedTo" : { "t" : 1319970116000, "i" : 1 } }
> db.sources.remove({"host" : "10.250.7.249:27017"}); 
> db.sources.find();
{ "_id" : ObjectId("4ead25ed6c7e8620f17d8ebc"), "host" : "10.250.7.249:27017", "source" : "main", "syncedTo" : { "t" : 1319970276000, "i" : 1 } }
注意:如果从库从不同的主库上接受日志,对于同样的集合collections,mongodb会自动合并他们,当然这样做并不保证完全正确。对于此种情况,***指定不同的命名空间。