MongoDB单机版副本集搭建--测试、开发

环境及版本:Centos 6.5  MongoDB2.4.13

1.开启一个MongoDB shell,不连接任何mongod

# ./mongo --nodb
2.创建复制集,一个primary两个secondary

> replicaSet = new ReplSetTest({"nodes" : 3})
{
        "name" : "testReplSet",
        "useHostName" : true,
        "host" : "Centos201",
        "numNodes" : 3,
        "oplogSize" : 40,
        "useSeedList" : false,
        "bridged" : false,
        "ports" : [
                31000,
                31001,
                31002
        ],
        "keyFile" : undefined,
        "shardSvr" : false,
        "startPort" : 31000,
        "nodeOptions" : {
                "n0" : undefined,
                "n1" : undefined,
                "n2" : undefined
        },
        "nodes" : [ ],
        "liveNodes" : {
                "master" : null,
                "slaves" : [ ]
        },
        ..................<span style="color:#FF0000;">后面省略好多行</span>

3.启动三个mongod实例

      三个实例分别监听31000 31001 31002端口

> replicaSet.startSet()
ReplSetTest Starting Set
ReplSetTest n is : 0
ReplSetTest n: 0 ports: [ 31000, 31001, 31002 ] 31000 number
{
        "useHostName" : true,
        "oplogSize" : 40,
        "keyFile" : undefined,
        "port" : 31000,
        "noprealloc" : "",
        "smallfiles" : "",
        "rest" : "",
        "replSet" : "testReplSet",
        "dbpath" : "$set-$node",
        "restart" : undefined,
        "pathOpts" : {
                "node" : 0,
                "set" : "testReplSet"
        }
}
ReplSetTest Starting....
<pre name="code" class="javascript"> ..................<span style="color:#FF0000;">后面省略好多行</span>
报错:
 m31000| Mon Mar 30 19:23:58.505 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
 m31002| Mon Mar 30 19:24:07.549 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
 m31001| Mon Mar 30 19:24:08.043 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

 

4.配置config 并初始化

config = { _id:"testReplSet", members:[
{_id:0,host:"localhost:31000"},
{_id:1,host:"localhost:31001"},
{_id:2,host:"localhost:31002"}]
}

> replicaSet.initiate(config)
{
        "replSetInitiate" : {
                "_id" : "testReplSet",
                "members" : [
                        {
                                "_id" : 0,
                                "host" : "localhost:31000"
                        },
                        {
                                "_id" : 1,
                                "host" : "localhost:31001"
                        },
                        {
                                "_id" : 2,
                                "host" : "localhost:31002"
                        }
                ]
        }
}
 m31000| Mon Mar 30 19:26:32.971 [conn1] replSet replSetInitiate admin command received from client
 m31000| Mon Mar 30 19:26:32.972 [conn1] replSet replSetInitiate config object parses ok, 3 members specified
 m31001| Mon Mar 30 19:26:32.973 [initandlisten] connection accepted from 127.0.0.1:53337 #2 (2 connections now open)
 m31002| Mon Mar 30 19:26:32.974 [initandlisten] connection accepted from 127.0.0.1:48052 #2 (2 connections now open)
 m31000| Mon Mar 30 19:26:32.974 [conn1] replSet replSetInitiate all members seem up
 m31000| Mon Mar 30 19:26:32.974 [conn1] ******
 m31000| Mon Mar 30 19:26:32.974 [conn1] creating replication oplog of size: 40MB...
 m31000| Mon Mar 30 19:26:32.975 [FileAllocator] allocating new datafile /data/db/testReplSet-0/local.1, filling with zeroes...
 m31000| Mon Mar 30 19:26:32.987 [FileAllocator] done allocating datafile /data/db/testReplSet-0/local.1, size: 64MB,  took 0.011 secs
 m31000| Mon Mar 30 19:26:32.987 [conn1] ******
 m31000| Mon Mar 30 19:26:32.987 [conn1] replSet info saving a newer config version to local.system.replset
 m31000| Mon Mar 30 19:26:32.988 [conn1] replSet saveConfigLocally done
 m31000| Mon Mar 30 19:26:32.988 [conn1] replSet replSetInitiate config now saved locally.  Should come online in about a minute.
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
 m31002| Mon Mar 30 19:26:37.552 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
 m31001| Mon Mar 30 19:26:38.047 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
 m31000| Mon Mar 30 19:26:38.508 [rsStart] replSet I am localhost:31000
 m31000| Mon Mar 30 19:26:38.508 [rsStart] replSet STARTUP2
 m31000| Mon Mar 30 19:26:38.509 [rsHealthPoll] replSet member localhost:31002 is up
 m31000| Mon Mar 30 19:26:38.509 [rsHealthPoll] replSet member localhost:31001 is up
 m31000| Mon Mar 30 19:26:39.509 [rsSync] replSet SECONDARY
<pre name="code" class="javascript"><pre name="code" class="javascript"> ..................<span style="color:#FF0000;">后面省略好多行</span>

 

 5.查看测试: 

   重新打开一个shell,查看运行状态

   ps -ef | grep mong

root      2324     1  0 17:21 ?        00:00:00 /usr/sbin/certmonger -S -p /var/run/certmonger.pid
root     12900  2599  0 19:15 pts/0    00:00:00 ./mongo --nodb
root     12943 12900  0 19:23 pts/0    00:00:01 /root/mongodb/mongodb-linux-x86_64-2.4.13/bin/mongod --oplogSize 40 --port 31000 --noprealloc --smallfiles --rest --replSet testReplSet --dbpath /data/db/testReplSet-0 --setParameter enableTestCommands=1
root     13037 12900  0 19:23 pts/0    00:00:01 /root/mongodb/mongodb-linux-x86_64-2.4.13/bin/mongod --oplogSize 40 --port 31001 --noprealloc --smallfiles --rest --replSet testReplSet --dbpath /data/db/testReplSet-1 --setParameter enableTestCommands=1
root     13132 12900  0 19:23 pts/0    00:00:01 /root/mongodb/mongodb-linux-x86_64-2.4.13/bin/mongod --oplogSize 40 --port 31002 --noprealloc --smallfiles --rest --replSet testReplSet --dbpath /data/db/testReplSet-2 --setParameter enableTestCommands=1
root     13412  3033  0 19:31 pts/1    00:00:00 grep mong
6.创建第一个链接

./mongo --nodb
conn1 = new Mongo("localhost:31000")
primaryDB = conn1.getDB("test") 
primaryDB.isMaster()
{
        "setName" : "testReplSet",
        "ismaster" : true,//primaryDB为主节点
        "secondary" : false,
        "hosts" : [
                "localhost:31000",
                "localhost:31002",
                "localhost:31001"
        ],
        "primary" : "localhost:31000",
        "me" : "localhost:31000",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2015-03-30T13:27:08.290Z"),
        "ok" : 1
}
7.插入数据

> for (i=0; i<1000; i++) { primaryDB.coll.insert({count: i}) }
> primaryDB.coll.count()
1000

8.创建第二个链接

> conn2 = new Mongo("localhost:31001")
connection to localhost:31001
> secondaryDB = conn2.getDB("test")
test
> secondaryDB.coll.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 } 
//无法读取第二个节点的数据(自我保护)

9.修改第二节点设置查看数据

>  conn2.setSlaveOk()
> secondaryDB.coll.find()
{ "_id" : ObjectId("55194fde66a8277a3d610f22"), "count" : 0 }
{ "_id" : ObjectId("55194fde66a8277a3d610f23"), "count" : 1 }
{ "_id" : ObjectId("55194fde66a8277a3d610f24"), "count" : 2 }
{ "_id" : ObjectId("55194fde66a8277a3d610f25"), "count" : 3 }
{ "_id" : ObjectId("55194fde66a8277a3d610f26"), "count" : 4 }
{ "_id" : ObjectId("55194fde66a8277a3d610f27"), "count" : 5 }
{ "_id" : ObjectId("55194fde66a8277a3d610f28"), "count" : 6 }
{ "_id" : ObjectId("55194fde66a8277a3d610f29"), "count" : 7 }
{ "_id" : ObjectId("55194fde66a8277a3d610f2a"), "count" : 8 }
{ "_id" : ObjectId("55194fde66a8277a3d610f2b"), "count" : 9 }
{ "_id" : ObjectId("55194fde66a8277a3d610f2c"), "count" : 10 }
{ "_id" : ObjectId("55194fde66a8277a3d610f2d"), "count" : 11 }
{ "_id" : ObjectId("55194fde66a8277a3d610f2e"), "count" : 12 }
{ "_id" : ObjectId("55194fde66a8277a3d610f2f"), "count" : 13 }
{ "_id" : ObjectId("55194fde66a8277a3d610f30"), "count" : 14 }
{ "_id" : ObjectId("55194fde66a8277a3d610f31"), "count" : 15 }
{ "_id" : ObjectId("55194fde66a8277a3d610f32"), "count" : 16 }
{ "_id" : ObjectId("55194fde66a8277a3d610f33"), "count" : 17 }
{ "_id" : ObjectId("55194fde66a8277a3d610f34"), "count" : 18 }
{ "_id" : ObjectId("55194fde66a8277a3d610f35"), "count" : 19 }
Type "it" for more

10.测试主节点是否会自动更换

  关闭31000当前主节点

rimaryDB.adminCommand({"shutdown" : 1})
netstat -ntplu | grep mong
tcp        0      0 0.0.0.0:31001               0.0.0.0:*                   LISTEN      13037/mongod
tcp        0      0 0.0.0.0:31002               0.0.0.0:*                   LISTEN      13132/mongod
tcp        0      0 0.0.0.0:32001               0.0.0.0:*                   LISTEN      13037/mongod
tcp        0      0 0.0.0.0:32002               0.0.0.0:*                   LISTEN      13132/mongod
> secondaryDB.isMaster()
{
        "setName" : "testReplSet",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "localhost:31001",
                "localhost:31002",
                "localhost:31000"
        ],
        "primary" : "localhost:31001",
        "me" : "localhost:31001",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2015-03-30T13:59:37.591Z"),
        "ok" : 1
}
主节点变成 secondaryDB节点



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值