MongoDB主从复制小实验(四)
2 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节点,查询结果:
[plain]
[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)执行修改操作,结果如下:
[plain]
> 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)执行删除操作,结果如下:
[plain]
> db.test.remove(); //master节点删除数据
> db.test.find(); //master节点查看数据
>
> db.test.find(); //slave1节点查看数据
>
> db.test.find(); //slave2节点查看数据
>
(9)在slave节点上进行数据插入和修改:
[plain]
> 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节点的信息。