复制(mongodb权威指南第9章)

     mongodb管理员最重要的工作莫过于确保复制设置正确,切运转良好。强烈推荐再生产环境中使用mongodb的复制功能,尤其是现在的存储引擎还不支持单机持久性。可以用复制应对故障切换、数据集成、读扩展、热备份、离线批处理的数据源。

9.1 主从复制

  主从复制是mongodb最常用的复制方式。这种方式非常灵活,用于备份、故障恢复、读扩展等。

  搭配一个从节点的主节点   搭配3个从节点的主节点

   最基本的设置方式就是建立一个主节点和一个或者多个子节点,每个子节点要知道主节点的地址。

   运行mongod --master就启动了主服务器

   运行mongod --slave --source master_address则启动了从服务器,就是上面主节点的地址。

   在生产环境中,会有多台服务器,在一台机器上模拟:

   首先,给主节点建立数据目录,并绑定端口,

   C盘下建立data1(c:\data1)文件夹作为主节点的数据,建立data2(C:\data2)目录作为从节点的数据目录

  

 d:\java\mongodb-win32-i386-2.2.3\bin>mongod --dbpath c:\data1 --port 10000 --mas
ter
Thu Mar 21 22:27:37
Thu Mar 21 22:27:37 warning: 32-bit servers don't have journaling enabled by def
ault. Please use --journal if you want durability.
Thu Mar 21 22:27:37
Thu Mar 21 22:27:37 [initandlisten] MongoDB starting : pid=1744 port=10000 dbpat
h=c:\data1 master=1 32-bit host=king-PC
Thu Mar 21 22:27:37 [initandlisten]
Thu Mar 21 22:27:37 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are
limited to about 2 gigabytes of data
Thu Mar 21 22:27:37 [initandlisten] **       see http://blog.mongodb.org/post/13
7788967/32-bit-limitations
Thu Mar 21 22:27:37 [initandlisten] **       with --journal, the limit is lower
Thu Mar 21 22:27:37 [initandlisten]
Thu Mar 21 22:27:37 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Mar 21 22:27:37 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2
f4a96bf08
Thu Mar 21 22:27:37 [initandlisten] build info: windows sys.getwindowsversion(ma
jor=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB
_VERSION=1_49
Thu Mar 21 22:27:37 [initandlisten] options: { dbpath: "c:\data1", master: true,
 port: 10000 }
Thu Mar 21 22:27:37 [initandlisten] Unable to check for journal files due to: bo
ost::filesystem::basic_directory_iterator constructor: ϵͳ�Ҳ���ָ����·����: "c:\d
ata1\journal"
Thu Mar 21 22:27:37 [initandlisten] ******
Thu Mar 21 22:27:37 [initandlisten] creating replication oplog of size: 47MB...
Thu Mar 21 22:27:37 [FileAllocator] allocating new datafile c:/data1/local.ns, f
illing with zeroes...
Thu Mar 21 22:27:37 [FileAllocator] creating directory c:/data1/_tmp
Thu Mar 21 22:27:37 [FileAllocator] done allocating datafile c:/data1/local.ns,
size: 16MB,  took 0.088 secs
Thu Mar 21 22:27:37 [FileAllocator] allocating new datafile c:/data1/local.0, fi
lling with zeroes...
Thu Mar 21 22:27:37 [FileAllocator] done allocating datafile c:/data1/local.0, s
ize: 64MB,  took 0.334 secs
Thu Mar 21 22:27:39 [initandlisten] ******
Thu Mar 21 22:27:39 [initandlisten] waiting for connections on port 10000
Thu Mar 21 22:27:40 [websvr] admin web console waiting for connections on port 1
1000

 

   接着设置从节点,记者要选择不同的目录和端口号,并且用--scource位从节点指明主节点的地址

 D:\java\mongodb-win32-i386-2.2.3\bin>mongod --dbpath c:\data2 --port 10001 --sla
ve --source localhost:10000
Thu Mar 21 22:30:56
Thu Mar 21 22:30:56 warning: 32-bit servers don't have journaling enabled by def
ault. Please use --journal if you want durability.
Thu Mar 21 22:30:56
Thu Mar 21 22:30:56 [initandlisten] MongoDB starting : pid=5388 port=10001 dbpat
h=c:\data2 slave=1 32-bit host=king-PC
Thu Mar 21 22:30:56 [initandlisten]
Thu Mar 21 22:30:56 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are
limited to about 2 gigabytes of data
Thu Mar 21 22:30:56 [initandlisten] **       see http://blog.mongodb.org/post/13
7788967/32-bit-limitations
Thu Mar 21 22:30:56 [initandlisten] **       with --journal, the limit is lower
Thu Mar 21 22:30:56 [initandlisten]
Thu Mar 21 22:30:56 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Mar 21 22:30:56 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2
f4a96bf08
Thu Mar 21 22:30:56 [initandlisten] build info: windows sys.getwindowsversion(ma
jor=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB
_VERSION=1_49
Thu Mar 21 22:30:56 [initandlisten] options: { dbpath: "c:\data2", port: 10001,
slave: true, source: "localhost:10000" }
Thu Mar 21 22:30:56 [initandlisten] Unable to check for journal files due to: bo
ost::filesystem::basic_directory_iterator constructor: ϵͳ�Ҳ���ָ����·����: "c:\d
ata2\journal"
Thu Mar 21 22:30:56 [initandlisten] waiting for connections on port 10001
Thu Mar 21 22:30:56 [websvr] admin web console waiting for connections on port 1
1001
Thu Mar 21 22:30:57 [FileAllocator] allocating new datafile c:/data2/local.ns, f
illing with zeroes...
Thu Mar 21 22:30:57 [FileAllocator] creating directory c:/data2/_tmp
Thu Mar 21 22:30:57 [FileAllocator] done allocating datafile c:/data2/local.ns,
size: 16MB,  took 0.098 secs
Thu Mar 21 22:30:57 [FileAllocator] allocating new datafile c:/data2/local.0, fi
lling with zeroes...
Thu Mar 21 22:30:57 [FileAllocator] done allocating datafile c:/data2/local.0, s
ize: 16MB,  took 0.084 secs
Thu Mar 21 22:30:57 [replslave] build index local.sources { _id: 1 }
Thu Mar 21 22:30:57 [FileAllocator] allocating new datafile c:/data2/local.1, fi
lling with zeroes...
Thu Mar 21 22:30:57 [replslave] build index done.  scanned 0 total records. 0.00
2 secs
Thu Mar 21 22:30:57 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:30:57 [replslave] build index local.me { _id: 1 }
Thu Mar 21 22:30:57 [replslave] build index done.  scanned 0 total records. 0.00
3 secs
Thu Mar 21 22:30:57 [FileAllocator] done allocating datafile c:/data2/local.1, s
ize: 32MB,  took 0.18 secs
Thu Mar 21 22:31:27 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:31:27 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:31:27 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:31:29 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:32:00 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:32:00 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:32:00 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:32:02 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:32:32 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:32:36 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:32:36 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:32:38 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:33:09 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:33:09 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:33:09 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:33:11 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:33:41 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:33:42 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:33:42 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:33:44 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:34:15 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:34:21 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:34:21 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:34:23 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:34:54 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:34:54 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:34:54 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:34:56 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:35:26 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:35:26 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:35:26 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:35:28 [replslave] repl: syncing from host:localhost:10000
Thu Mar 21 22:35:59 [replslave] recv(): message len 1347703880 is too large13477
03880
Thu Mar 21 22:35:59 [replslave] DBClientCursor::init call() failed
Thu Mar 21 22:35:59 [replslave] repl: AssertionException DBClientBase::findN: tr
ansport error: localhost:10000 ns: admin.$cmd query: { handshake: ObjectId('514b
19a190e1a834fa9fb235') }
repl: sleep 2 sec before next pass
Thu Mar 21 22:36:01 [replslave] repl: syncing from host:localhost:10000

    设置后磁盘数据目录的变化:

   

    所有 从节点都从主节点复制数据。目前还没有从从节点复制的机制(菊花链),原因就是从节点不保存自己的oplog.

  一个集群中有多少个从节点并没有明确的限制,但是上千个从节点对单个主机发起查询也会让其吃不消,所以实际中,不超过12个从节点的集群就可以运转良好。

   9.1.1 选项

     主从复制有些有用的选项:

  •     --only    在主节点上指定只复制某个特定的数据库(默认复制所有数据库)
  • --slavedelay   用在从节点上,当应用主节点的操作时增加延时(单位秒)
  • --fastsync   一主节点的数据快照为基础启动从节点。如果数据目录一开始是主节点的数据快照,从节点的这个选项启动要比作完整同步快很多。
  • --autoresync 如果从节点与主节点不同步了,则自动重新同步
  • --oplogSize 主节点oplog的大小,单位MB

 9.1.2 添加及删除源

     启动从节点是可以用--source 指定主节点,也可以在shell中配置这个源。

     假设主节点绑定了localhost:10000,启动从节点是可以不添加源,而是随后向source集合添加节点信息。

     在磁盘c下建立data3 目录。

   

 


D:\java\mongodb-win32-i386-2.2.3\bin>mongod --slave --dbpath c:\data3  --port 10
002
Thu Mar 21 22:58:38
Thu Mar 21 22:58:38 warning: 32-bit servers don't have journaling enabled by def
ault. Please use --journal if you want durability.
Thu Mar 21 22:58:38
Thu Mar 21 22:58:38 [initandlisten] MongoDB starting : pid=5508 port=10002 dbpat
h=c:\data3 slave=1 32-bit host=king-PC
Thu Mar 21 22:58:38 [initandlisten]
Thu Mar 21 22:58:38 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are
limited to about 2 gigabytes of data
Thu Mar 21 22:58:38 [initandlisten] **       see http://blog.mongodb.org/post/13
7788967/32-bit-limitations
Thu Mar 21 22:58:38 [initandlisten] **       with --journal, the limit is lower
Thu Mar 21 22:58:38 [initandlisten]
Thu Mar 21 22:58:38 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Mar 21 22:58:38 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2
f4a96bf08
Thu Mar 21 22:58:38 [initandlisten] build info: windows sys.getwindowsversion(ma
jor=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB
_VERSION=1_49
Thu Mar 21 22:58:38 [initandlisten] options: { dbpath: "c:\data3", port: 10002,
slave: true }
Thu Mar 21 22:58:38 [initandlisten] Unable to check for journal files due to: bo
ost::filesystem::basic_directory_iterator constructor: ϵͳ�Ҳ���ָ����·����: "c:\d
ata3\journal"
Thu Mar 21 22:58:38 [initandlisten] waiting for connections on port 10002
Thu Mar 21 22:58:39 [websvr] admin web console waiting for connections on port 1
1002
Thu Mar 21 22:58:39 [replslave] no source given, add a master to local.sources t
o start replication
Thu Mar 21 22:58:39 [replslave] repl: sleep 20 sec before next pass
Thu Mar 21 22:58:59 [replslave] no source given, add a master to local.sources t
o start replication
Thu Mar 21 22:58:59 [replslave] repl: sleep 20 sec before next pass
Thu Mar 21 22:59:19 [replslave] no source given, add a master to local.sources t
o start replication
Thu Mar 21 22:59:19 [replslave] repl: sleep 20 sec before next pass
Thu Mar 21 22:59:39 [replslave] no source given, add a master to local.sources t
o start replication
Thu Mar 21 22:59:39 [replslave] repl: sleep 20 sec before next pass
Thu Mar 21 22:59:59 [replslave] no source given, add a master to local.sources t
o start replication
Thu Mar 21 22:59:59 [replslave] repl: sleep 20 sec before next pass

    此时磁盘的变化:

  现在可以在shell中运行:将localhost:10000作为源添加到从节点上。

 

 

9.2 副本集

    简单说,副本集(Replica Set)就是有自动故障恢复功能的主从集群。主从集群和副本集最明显的区别是副本及没有固定的“主节点”:整个集群会选择出一个主节点,当期不能工作时则变更到其他节点。然而,而这看上去十分相似:副本集总会有一个活跃节点(primary)和一个或者多个备份节点(secondary).

    副本集最美妙的地方就是所有东西都是自动化的。首先,他为你做很多管理工作,自动提升备份节点称为活跃节点,以确保运转正常。其次,他对于开发者而言,也很容易:仅仅需要位副本集指定一下服务器,驱动程序就会自动找到服务器,在当前的活跃节点死机是自动处理故障恢复这类事情。

 

 

  9.2.1 初始化副本集

  9.2.2 副本集中的节点

  9.2.3 故障切换和活跃节点选举

9.3 在服务器上执行操作

9.4 工作原理

9.5 管理  

 

 

 

 

 

 

 

 

 

    

 

 

 

 

  

  

  

转载于:https://my.oschina.net/kingwjb/blog/115562

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值