MongoDB 主从复制小实验
MongoDB 主从复制小实验
操作环境描述:WIN8 64位操作系统,内装虚拟机为CentOS 5.5 32位系统。
操作描述:跟其他关系型数据库类似,在主库进行数据操作,将数据同步到从节点,从节点可以配置多个。在本次试验中,master节点为主节点,我们在master节点中的对数据的插入、修改、删除操作都会同步到slave1和slave2节点。
以下为实验步骤:
(1)建立数据目录,数据目录用来存放过程中的数据。
[root@h3 dbs]# pwd
/var/lib/mongo/dbs
[root@h3 dbs]# ls
master slave1 slave2
[root@h3 dbs]#
(2)启动master节点:
启动master
[root@h3 dbs]# mongod --dbpath /var/lib/mongo/dbs/master --port 10000 --master --rest --nojournal
Fri Aug 2 14:10:25.498 [initandlisten] MongoDB starting : pid=19491 port=10000 dbpath=/var/lib/mongo/dbs/master master=1 32-bit host=h3
Fri Aug 2 14:10:25.498 [initandlisten]
Fri Aug 2 14:10:25.498 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri Aug 2 14:10:25.498 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri Aug 2 14:10:25.498 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Fri Aug 2 14:10:25.499 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Fri Aug 2 14:10:25.499 [initandlisten]
Fri Aug 2 14:10:25.500 [initandlisten] db version v2.4.5
Fri Aug 2 14:10:25.500 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b
Fri Aug 2 14:10:25.500 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Fri Aug 2 14:10:25.500 [initandlisten] allocator: system
Fri Aug 2 14:10:25.500 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/master", master: true, nojournal: true, port: 10000, rest: true }
Fri Aug 2 14:10:25.540 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/master/local.ns, filling with zeroes...
Fri Aug 2 14:10:25.542 [FileAllocator] creating directory /var/lib/mongo/dbs/master/_tmp
Fri Aug 2 14:10:26.094 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/master/local.ns, size: 16MB, took 0.545 secs
Fri Aug 2 14:10:26.094 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/master/local.0, filling with zeroes...
Fri Aug 2 14:10:26.763 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/master/local.0, size: 16MB, took 0.667 secs
Fri Aug 2 14:10:26.785 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 12 45ms
Fri Aug 2 14:10:26.796 [initandlisten] ******
Fri Aug 2 14:10:26.796 [initandlisten] creating replication oplog of size: 50MB...
Fri Aug 2 14:10:26.799 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/master/local.1, filling with zeroes...
Fri Aug 2 14:10:30.116 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/master/local.1, size: 64MB, took 3.314 secs
Fri Aug 2 14:10:30.243 [initandlisten] ******
Fri Aug 2 14:10:30.245 [initandlisten] waiting for connections on port 10000
Fri Aug 2 14:10:30.247 [websvr] admin web console waiting for connections on port 11000
Fri Aug 2 14:16:52.604 [initandlisten] connection accepted from 127.0.0.1:33749 #1 (1 connection now open)
Fri Aug 2 14:16:53.885 [slaveTracking] build index local.slaves { _id: 1 }
Fri Aug 2 14:16:53.894 [slaveTracking] build index done. scanned 0 total records. 0.008 secs
(3)启动slave1节点:
[root@h3 ~]# mongod -dbpath /var/lib/mongo/dbs/slave1 --port 10001 --slave --rest --nojournal --source localhost:10000
Fri Aug 2 14:16:50.050 [initandlisten] MongoDB starting : pid=19513 port=10001 dbpath=/var/lib/mongo/dbs/slave1 slave=1 32-bit host=h3
Fri Aug 2 14:16:50.051 [initandlisten]
Fri Aug 2 14:16:50.052 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri Aug 2 14:16:50.053 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri Aug 2 14:16:50.053 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Fri Aug 2 14:16:50.054 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Fri Aug 2 14:16:50.055 [initandlisten]
Fri Aug 2 14:16:50.055 [initandlisten] db version v2.4.5
Fri Aug 2 14:16:50.056 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b
Fri Aug 2 14:16:50.056 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Fri Aug 2 14:16:50.057 [initandlisten] allocator: system
Fri Aug 2 14:16:50.058 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/slave1", nojournal: true, port: 10001, rest: true, slave: true, source: "localhost: 10000" }
Fri Aug 2 14:16:50.080 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/slave1/local.ns, filling with zeroes...
Fri Aug 2 14:16:50.081 [FileAllocator] creating directory /var/lib/mongo/dbs/slave1/_tmp
Fri Aug 2 14:16:50.520 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/slave1/local.ns, size: 16MB, took 0.412 secs
Fri Aug 2 14:16:50.521 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/slave1/local.0, filling with zeroes...
Fri Aug 2 14:16:51.512 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/slave1/local.0, size: 16MB, took 0.989 secs
Fri Aug 2 14:16:51.520 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 14 41ms
Fri Aug 2 14:16:51.524 [websvr] admin web console waiting for connections on port 11001
Fri Aug 2 14:16:51.525 [initandlisten] waiting for connections on port 10001
Fri Aug 2 14:16:52.577 [replslave] build index local.sources { _id: 1 }
Fri Aug 2 14:16:52.583 [replslave] build index done. scanned 0 total records. 0.004 secs
Fri Aug 2 14:16:52.595 [replslave] repl: syncing from host:localhost:10000
Fri Aug 2 14:16:52.657 [replslave] build index local.me { _id: 1 }
Fri Aug 2 14:16:52.662 [replslave] build index done. scanned 0 total records. 0.004 secs
Fri Aug 2 14:19:24.330 [replslave] repl: checkpoint applied 15 operations
Fri Aug 2 14:19:24.330 [replslave] repl: syncedTo: Aug 2 14:19:14 51fb4f62:1
(4)启动slave2节点:
[root@h3 ~]# mongod -dbpath /var/lib/mongo/dbs/slave2 --port 10002 --slave --rest --nojournal --source localhost:10000
Fri Aug 2 14:22:07.693 [initandlisten] MongoDB starting : pid=19596 port=10002 dbpath=/var/lib/mongo/dbs/slave2 slave=1 32-bit host=h3
Fri Aug 2 14:22:07.694 [initandlisten]
Fri Aug 2 14:22:07.695 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri Aug 2 14:22:07.696 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri Aug 2 14:22:07.696 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Fri Aug 2 14:22:07.697 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Fri Aug 2 14:22:07.698 [initandlisten]
Fri Aug 2 14:22:07.698 [initandlisten] db version v2.4.5
Fri Aug 2 14:22:07.699 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b
Fri Aug 2 14:22:07.700 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Fri Aug 2 14:22:07.700 [initandlisten] allocator: system
Fri Aug 2 14:22:07.701 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/slave2", nojournal: true, port: 10002, rest: true, slave: true, source: "localhost:10000" }
Fri Aug 2 14:22:07.713 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/slave2/local.ns, filling with zeroes...
Fri Aug 2 14:22:07.714 [FileAllocator] creating directory /var/lib/mongo/dbs/slave2/_tmp
Fri Aug 2 14:22:07.997 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/slave2/local.ns, size: 16MB, took 0.278 secs
Fri Aug 2 14:22:07.999 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/slave2/local.0, filling with zeroes...
Fri Aug 2 14:22:08.373 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/slave2/local.0, size: 16MB, took 0.373 secs
Fri Aug 2 14:22:08.381 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 668ms
Fri Aug 2 14:22:08.383 [initandlisten] waiting for connections on port 10002
Fri Aug 2 14:22:08.386 [websvr] admin web console waiting for connections on port 11002
Fri Aug 2 14:22:09.391 [replslave] build index local.sources { _id: 1 }
Fri Aug 2 14:22:09.397 [replslave] build index done. scanned 0 total records. 0.005 secs
Fri Aug 2 14:22:09.398 [replslave] repl: syncing from host:localhost:10000
Fri Aug 2 14:22:09.405 [replslave] build index local.me { _id: 1 }
Fri Aug 2 14:22:09.409 [replslave] build index done. scanned 0 total records. 0.003 secs
(5)链接master,插入一条数据:
[root@h3 ~]# mongo localhost:10000
MongoDB shell version: 2.4.5
connecting to: localhost:10000/test
Server has startup warnings:
Mon Aug 12 08:23:21.713 [initandlisten]
Mon Aug 12 08:23:21.713 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Mon Aug 12 08:23:21.713 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Mon Aug 12 08:23:21.714 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Mon Aug 12 08:23:21.714 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Mon Aug 12 08:23:21.714 [initandlisten]
> use test;
switched to db test
> db.test.find();
> db.test.insert({'host': '1000'});
> db.test.find();
{ "_id" : ObjectId("52082b99d30e16a12c0f4277"), "host" : "1000" }
>
(6)连接slave1节点,查询结果:
[root@h3 ~]# mongo localhost:10001
MongoDB shell version: 2.4.5
connecting to: localhost:10001/test
Server has startup warnings:
Mon Aug 12 08:23:34.870 [initandlisten]
Mon Aug 12 08:23:34.872 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Mon Aug 12 08:23:34.872 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Mon Aug 12 08:23:34.872 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Mon Aug 12 08:23:34.872 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Mon Aug 12 08:23:34.872 [initandlisten]
> use test;
switched to db test
> db.test.find();
{ "_id" : ObjectId("52082b99d30e16a12c0f4277"), "host" : "1000" }
>
(7)连接slave2节点,查询结果:
[root@h3 ~]# mongo localhost:10002
MongoDB shell version: 2.4.5
connecting to: localhost:10002/test
Server has startup warnings:
Mon Aug 12 08:23:38.074 [initandlisten]
Mon Aug 12 08:23:38.075 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Mon Aug 12 08:23:38.076 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Mon Aug 12 08:23:38.077 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Mon Aug 12 08:23:38.078 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Mon Aug 12 08:23:38.078 [initandlisten]
> use test;
switched to db test
> db.test.find();
{ "_id" : ObjectId("52082b99d30e16a12c0f4277"), "host" : "1000" }
>
(8)执行修改操作,结果如下:
> db.test.update({'host': '1000'}, {'server': 'master'}); // master节点修改数据
> db.test.find() //master节点查看数据
{ "_id" : ObjectId("52082b99d30e16a12c0f4277"), "server" : "master" }
>
> db.test.find(); // slave1 节点查看数据
{ "_id" : ObjectId("52082b99d30e16a12c0f4277"), "server" : "master" }
>
> db.test.find(); // slave2节点查看数据
{ "_id" : ObjectId("52082b99d30e16a12c0f4277"), "server" : "master" }
>
(9)执行删除操作,结果如下:
> db.test.remove(); //master节点删除数据
> db.test.find(); //master节点查看数据
> > db.test.find(); //slave1节点查看数据
> > db.test.find(); //slave2节点查看数据
>
(9)在slave节点上进行数据插入和修改:
> db.test.insert({"host": "10001"});
not master
> db.test.remove();
not master
>
备注:
(1)MongoDB通过以上方式配置的主从同步只能在master端对数据进行新增、修改、删除操作,在slave节点只能进行查看操作。
(2)启动服务参数解释:
--dbpath /var/lib/mongo/dbs/master 指定数据存放目录为 /var/lib/mongo/dbs/master
--port 10000 指定监听端口为10000
--master 说明开启的是master服务进程
--slave 说明开启的是slave服务进程
--source localhost:10000 指定数据源为的地址为 localhost:100000
(3)服务启动后,可以通过:http://localhost:11000 查看master相关信息;查看http://localhost:10001、http://localhost:10002查看slave节点的信息。
MongoDB 主从复制小实验的更多相关文章
- 转】MongoDB主从复制实验 master/slave
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! Posted: May 31, 2013 Ta ...
- Mongodb主从复制/ 副本集/分片集群介绍
前面的文章介绍了Mongodb的安装使用,在 MongoDB 中,有两种数据冗余方式,一种 是 Master-Slave 模式(主从复制),一种是 Replica Sets 模式(副本集). Mong ...
- Mongodb主从复制 及 副本集+分片集群梳理
转载努力哥原文,原文连接https://www.cnblogs.com/nulige/p/7613721.html 介绍了Mongodb的安装使用,在 MongoDB 中,有两种数据冗余方式,一种 是 ...
- MongoDB 主从复制及 自动故障转移
1.MongoDB 主从复制 MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从 ...
- PBR综合小实验视频-狮子XL
这个是上学时候录的一个策略路由小实验
- ubuntu下格式化内存当硬盘使的小实验
内存虚拟硬盘(ramdisk)是指通过软件技术,将物理内存进行分割,将一部分内存通过虚拟技术转变为硬盘以较大幅度提升计算机数据读取速度和保护硬盘. 在ubuntu下的dev下有ram相关的文件,这些文 ...
- PS小实验-去除水印
PS小实验-去除水印 水印是一些品牌商覆盖在图片或视频上的一个商标logo或小文本,比如大家最讨厌的百度logo,作者本人也是比较讨厌水印的,让好端端的一张图片变得美中不足. 个人觉得用photosh ...
- 1.4 Crack小实验
0_day 第一章 基础知识 1.4 Crack小实验 <0day_2th>王清 著 电子书 下载链接:https://pan.baidu.com/s/11TgibQSC3-kYwCInm ...
- arduino新入手体验:三个小实验
新入手体验:三个小实验 一:一个LED闪烁 控制要求:1个LED灯,每隔50ms闪烁一次 实物连接图: 控制代码: //2018.6/11 ;//定义数字接口10,对应 void setup() { ...
随机推荐
- (原创)LAMP教程1-下载虚拟机软件
(原创)LAMP教程1 从今天开始会在我的博客更新LAMP教程,第一章节就是安装虚拟机,因为不可能所有的人都有机会操作服务器,所以今天我打算教大家用虚拟机安装配置当下比较流行的框架,lamp. 好了费 ...
- 【windows核心编程】IO完成端口(IOCP)复制文件小例前简单说明
1.关于IOCP IOCP即IO完成端口,是一种高伸缩高效率的异步IO方式,一个设备或文件与一个IO完成端口相关联,当文件或设备的异步IO操作完成的时候,去IO完成端口的[完成队列]取一项,根据完成键 ...
- 开通GitHub以及使用笔记
把小游戏的代码和博客迁移到GitHub上,路径是:https://github.com/GAMTEQ,欢迎访问 以下是使用GITHUB的一些命令 504 cd code 506 mkdir Fai ...
- CodeForce---Educational Codeforces Round 3 Load Balancing 正题
看到这题是我的想法就是肯定跟平均值有关但是接下来就不知道怎么做了 看完大神的正解数之后,原来发现是这么简单,但是就是不知道为啥一定是平均值和平均值加1,而不是平均值和平均值减1: 好啦下面就贴出大神的 ...
- [转] Web前端优化之 内容篇
原文网址: http://lunax.info/archives/3090.html Yahoo! 的 Exceptional Performance team 在 Web 前端方面作出了卓越的贡献. ...
- 给MyEclipse 10增加SVN功能
1.在myeclipse的安装目录下 myeclipse 10文件夹下的 dropins文件夹新建一个文件夹 svn. 2.然后下载SVN插件:svn插件网站:http://subclipse.tig ...
- HDU5744:Keep On Movin(字符串)
题意: 给出t组测试数据,每组给出正整数n表示有n种字符,接下来给出n个数表示该种字符的数目,操作一下,使得可以构造的最小回文串字符数目最大且输出. 分析: 如果每个字符出现次数都是偶数, 那么答案显 ...
- World’s Smallest h.264 Encoder
转载 http://www.cardinalpeak.com/blog/worlds-smallest-h-264-encoder/ View from the Peak World’s Smalle ...
- Linux服务器集群系统(一)(转)
add by zhj:虽然是2002年的文章,但读来还是收益良多.在 章文嵩:谈LVS及阿里开源背后的精彩故事 中LVS发起人及主要贡献者谈了LVS的开发过程及阿里开源的一些故事 原文:http:// ...
- WebService学习之三:spring+cxf整合
步骤一:spring项目(java web项目)引入CXF jar包 步骤二:创建webservice服务器 1)创建一个服务接口 package com.buss.app.login; import ...