一分钟搭建mongodb架构Replica Set&Sharding—ttlsa教程系列之mongodb(七)

默北 mongodb ttlsa教程系列1321,629字数 6991阅读23分18秒阅读模式

在测试试验阶段,我们需要有一个模拟的测试环境来测试应用程序和系统架构各个方面的功能,是否符合需求。在我公司,我常常使用下面的方法来为开发人员搭建mongodb的复制集和分片架构进行测试。我也常用这个方法来模拟线上架构,测试相关内容。
开启一个MongoDB shell,不连接任何mongod

# ./mongo --nodb

创建复制集,一个primary两个secondary文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> replicaSet = new ReplSetTest({"nodes" : 3})

启动三个mongod实例文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> 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....
Resetting db path '/data/db/testReplSet-0'

ReplSetTest n is : 1
ReplSetTest n: 1 ports: [ 31000, 31001, 31002 ] 31001 number
{
        "useHostName" : true,
        "oplogSize" : 40,
        "keyFile" : undefined,
        "port" : 31001,
        "noprealloc" : "",
        "smallfiles" : "",
        "rest" : "",
        "replSet" : "testReplSet",
        "dbpath" : "$set-$node",
        "restart" : undefined,
        "pathOpts" : {
                "node" : 1,
                "set" : "testReplSet"
        }
}
ReplSetTest Starting....
Resetting db path '/data/db/testReplSet-1'

ReplSetTest n is : 2
ReplSetTest n: 2 ports: [ 31000, 31001, 31002 ] 31002 number
{
        "useHostName" : true,
        "oplogSize" : 40,
        "keyFile" : undefined,
        "port" : 31002,
        "noprealloc" : "",
        "smallfiles" : "",
        "rest" : "",
        "replSet" : "testReplSet",
        "dbpath" : "$set-$node",
        "restart" : undefined,
        "pathOpts" : {
                "node" : 2,
                "set" : "testReplSet"
        }
}
ReplSetTest Starting....
Resetting db path '/data/db/testReplSet-2'

复制集初始化文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> replicaSet.initiate()
{
        "replSetInitiate" : {
                "_id" : "testReplSet",
                "members" : [
                        {
                                "_id" : 0,
                                "host" : "nd0302012029:31000"
                        },
                        {
                                "_id" : 1,
                                "host" : "nd0302012029:31001"
                        },
                        {
                                "_id" : 2,
                                "host" : "nd0302012029:31002"
                        }
                ]
        }
}

启动了三个实例,分别监听在31000,31001,31002端口上文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

# ps -ef | grep mong
root      1234 25530  0 12:07 pts/3    00:00:00 ./mongo --nodb
root      1302  1234  0 12:08 pts/3    00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongod --oplogSize 40 --port 31000 --noprealloc --smallfiles --rest --replSet testReplSet --dbpath /data/db/testReplSet-0 --setParameter enableTestCommands=1
root      1350  1234  0 12:08 pts/3    00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongod --oplogSize 40 --port 31001 --noprealloc --smallfiles --rest --replSet testReplSet --dbpath /data/db/testReplSet-1 --setParameter enableTestCommands=1
root      1398  1234  0 12:08 pts/3    00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongod --oplogSize 40 --port 31002 --noprealloc --smallfiles --rest --replSet testReplSet --dbpath /data/db/testReplSet-2 --setParameter enableTestCommands=1
# netstat -ntplu | grep mong
tcp        0      0 0.0.0.0:32000               0.0.0.0:*                   LISTEN      1302/mongod         
tcp        0      0 0.0.0.0:32001               0.0.0.0:*                   LISTEN      1350/mongod         
tcp        0      0 0.0.0.0:32002               0.0.0.0:*                   LISTEN      1398/mongod         
tcp        0      0 0.0.0.0:31000               0.0.0.0:*                   LISTEN      1302/mongod         
tcp        0      0 0.0.0.0:31001               0.0.0.0:*                   LISTEN      1350/mongod         
tcp        0      0 0.0.0.0:31002               0.0.0.0:*                   LISTEN      1398/mongod

当前MongoDB shell窗口会有大量的日志信息输出,影响操作,另开启一个MongoDB shell文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

# ./mongo --nodb
> conn1 = new Mongo("localhost:31000")
> primaryDB = conn1.getDB("test")  //testReplSet是默认的复制集测试名
test
> primaryDB.isMaster()
{
        "setName" : "testReplSet",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "nd0302012029:31000",
                "nd0302012029:31002",
                "nd0302012029:31001"
        ],
        "primary" : "nd0302012029:31000",
        "me" : "nd0302012029:31000",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-07-28T04:23:49.866Z"),
        "ok" : 1
}

插入1000条文档文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

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

创建第二个连接,连接到secondary文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> conn2 = new Mongo("localhost:31001")
connection to localhost:31001
> secondaryDB = conn2.getDB("test")
test
> secondaryDB.coll.find()  //默认情况下secondary不可读不可写
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

允许secondary可读文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> conn2.setSlaveOk()
> secondaryDB.coll.find()
{ "_id" : ObjectId("51f49d09d0d09f69bbea159a"), "count" : 0 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea159b"), "count" : 1 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea159c"), "count" : 2 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea159d"), "count" : 3 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea159e"), "count" : 4 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea159f"), "count" : 5 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a0"), "count" : 6 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a1"), "count" : 7 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a2"), "count" : 8 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a3"), "count" : 9 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a4"), "count" : 10 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a5"), "count" : 11 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a6"), "count" : 12 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a7"), "count" : 13 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a8"), "count" : 14 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15a9"), "count" : 15 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15aa"), "count" : 16 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15ab"), "count" : 17 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15ac"), "count" : 18 }
{ "_id" : ObjectId("51f49d09d0d09f69bbea15ad"), "count" : 19 }
Type "it" for more
> secondaryDB.coll.count()
1000

尝试想secondary写数据文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> secondaryDB.coll.insert({"count" : 1001})
> secondaryDB.runCommand({"getLastError" : 1})
{
        "err" : "not master",
        "code" : 10058,
        "n" : 0,
        "lastOp" : Timestamp(0, 0),
        "connectionId" : 75,
        "ok" : 1
}

可看到secondary不接收客户端写操作文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

测试复制集的automatic failover功能:
shutdown 31000实例文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> primaryDB.adminCommand({"shutdown" : 1})
# netstat -ntplu | grep mong
tcp        0      0 0.0.0.0:32001               0.0.0.0:*                   LISTEN      1350/mongod         
tcp        0      0 0.0.0.0:32002               0.0.0.0:*                   LISTEN      1398/mongod         
tcp        0      0 0.0.0.0:31001               0.0.0.0:*                   LISTEN      1350/mongod         
tcp        0      0 0.0.0.0:31002               0.0.0.0:*                   LISTEN      1398/mongod

查看哪个实例变成primary文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> secondaryDB.isMaster()
{
        "setName" : "testReplSet",
        "ismaster" : false,
        "secondary" : true,
        "hosts" : [
                "nd0302012029:31001",
                "nd0302012029:31002",
                "nd0302012029:31000"
        ],
        "primary" : "nd0302012029:31002",
        "me" : "nd0302012029:31001",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-07-28T04:35:43.224Z"),
        "ok" : 1
}
> conn3 = new Mongo("localhost:31002")
connection to localhost:31002
> secondaryDB01 = conn3.getDB("test")
test
> secondaryDB01.isMaster()
{
        "setName" : "testReplSet",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "nd0302012029:31002",
                "nd0302012029:31001",
                "nd0302012029:31000"
        ],
        "primary" : "nd0302012029:31002",
        "me" : "nd0302012029:31002",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-07-28T04:37:20.681Z"),
        "ok" : 1
}

可见31002实例变成新的master文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

# ./mongo --port 31002
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:31002/test
testReplSet:PRIMARY> rs.status()
{
        "set" : "testReplSet",
        "date" : ISODate("2013-07-28T04:47:55Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "nd0302012029:31000",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(1374985481, 1000),
                        "optimeDate" : ISODate("2013-07-28T04:24:41Z"),
                        "lastHeartbeat" : ISODate("2013-07-28T04:47:53Z"),
                        "lastHeartbeatRecv" : ISODate("2013-07-28T04:31:55Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 1,
                        "name" : "nd0302012029:31001",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 2360,
                        "optime" : Timestamp(1374985481, 1000),
                        "optimeDate" : ISODate("2013-07-28T04:24:41Z"),
                        "lastHeartbeat" : ISODate("2013-07-28T04:47:54Z"),
                        "lastHeartbeatRecv" : ISODate("2013-07-28T04:47:54Z"),
                        "pingMs" : 0,
                        "syncingTo" : "nd0302012029:31002"
                },
                {
                        "_id" : 2,
                        "name" : "nd0302012029:31002",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2392,
                        "optime" : Timestamp(1374985481, 1000),
                        "optimeDate" : ISODate("2013-07-28T04:24:41Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}

关闭replica set文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

> replicaSet.stopSet()

sharding简易搭建方法参见: https://www.ttlsa.com/html/1787.html文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

转载请注明出处:一分钟搭建mongodb架构Replica Set&Sharding https://www.ttlsa.com/html/1830.html文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/ 文章源自运维生存时间-https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 28/07/2013 13:23:07
  • 转载请务必保留本文链接:https://www.ttlsa.com/mongodb/one-minute-to-build-mongodb-replica-set_sharding-architecture/
评论  13  访客  13
    • 风雨诗轩
      风雨诗轩 0

      我的和你一模一样,但是执行replicaSet.startSet()时报错了:
      ReplSetTest Starting….
      Resetting db path ‘/data/db/testReplSet-0’
      Thu Jan 4 19:38:31.848 shell: started program mongod –oplogSize 40 –port 31000 –noprealloc –smallfiles –rest –replSet testReplSet –dbpath /data/db/testReplSet-0 –setParameter enableTestCommands=1
      m31000| Unable to start program mongod errno:2 No such file or directory
      Could not start mongo program at 31000, process ended

      我用的是centos7,mongodb2.4.0版本。求帮助

      • 匿名
        匿名 9

        请问一下,我照着做为什么会出现下面的错误
        执行> replicaSet = new ReplSetTest({“nodes” : 3})
        > replicaSet.startSet()
        [thread1] shell: started program (sh2663): mongod –oplogSize 40 –port 20000 –noprealloc –smallfiles –replSet testReplSet –dbpath /data/db/testReplSet-0 –setParameter enableTestCommands=1
        d20000| Unable to start program mongod errno:2 No such file or directory
        2016-04-06T18:34:39.506+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:20000, reason: errno:111 Connection refused
        Could not start mongo program at 20000, process ended

        • 学习机器人
          学习机器人 9

          刚看到这篇日志,我是按照权威指南操作的,startSet命令不能执行成功,第一步已经在/data/db中建立了三个文件夹,但是这步启动进程时,出现了错误:
          Unable to start program mongod errno:2 No such file or directory
          2014-08-07T17:06:58.266+0800 warning: Failed to connect to 127.0.0.1:31000, reason: errno:111 Connection refused
          明明有文件夹,却告知没有文件夹,我的系统是centos6.5,还望赐教呀

            • www.ttlsa.com
              www.ttlsa.com 9

              @ 学习机器人 # ./mongo –nodb
              > replicaSet = new ReplSetTest({"nodes" : 3})
              > replicaSet.startSet()
              不需要建目录。 是不是按照这么来做的? 做测试就用这个来搭建环境。

                • 学习机器人
                  学习机器人 9

                  @ www.ttlsa.com 我就是这样做的,奇怪了,就是不行,请问你使用的操作系统是什么,mongodb什么版本的呀

                    • www.ttlsa.com
                      www.ttlsa.com 9

                      @ 学习机器人 centos6.5 mongodb2.6.3

                        • 学习机器人
                          学习机器人 9

                          @ www.ttlsa.com 我也是这个环境,就是不行,我把库的目录放在了,/data/db,别的操作没有问题,就是做副本时不行。

                          • 默北
                            默北

                            @ 学习机器人 按照上面,一步一步的来。

                      • E_M
                        E_M 9

                        @ 学习机器人 请问你是怎么解决这个问题的,我也遇到了不知道怎么解决。

                      • 默北
                        默北

                        简易搭建方法,没指定目录。 生产环境不要这么做。

                          • 默北
                            默北

                            @ 默北 怎么执行的?

                              • 布丁鱼
                                布丁鱼 9

                                @ 默北 replicaSet.startSet()执行了这个语句就报那个错误

                                  • 默北
                                    默北

                                    @ 布丁鱼 一步一步的按照上面的来。

                            评论已关闭!