1分でmongodbアーキテクチャReplica Set&Sharding---ttlsaチュートリアルシリーズのmongodb(7)を構築


テストテストの段階では、アプリケーションとシステムアーキテクチャの各方面の機能をテストし、ニーズに合っているかどうかをテストするシミュレーションのテスト環境が必要です.当社では、開発者のmongodbのレプリケーションセットとスライスアーキテクチャを構築するために、次の方法をよく使用してテストしています.私もこの方法を使ってオンラインアーキテクチャをシミュレートし、関連内容をテストします.MongoDB shellを開き、mongodを接続しません.
# ./mongo --nodb
レプリケーションセット、primary 2 secondaryを作成
> replicaSet = new ReplSetTest({"nodes" : 3})
mongodインスタンスを3つ起動
> 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'
レプリケーション・セットの初期化
> replicaSet.initiate()
{
        "replSetInitiate" : {
                "_id" : "testReplSet",
                "members" : [
                        {
                                "_id" : 0,
                                "host" : "nd0302012029:31000"
                        },
                        {
                                "_id" : 1,
                                "host" : "nd0302012029:31001"
                        },
                        {
                                "_id" : 2,
                                "host" : "nd0302012029:31002"
                        }
                ]
        }
}
は、3つのインスタンスを起動し、それぞれ31000 31001 31002ポートでリスニングします.
# 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を開く
# ./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件のドキュメントを挿入
> for (i=0; i<1000; i++) { primaryDB.coll.insert({count: i}) }
> primaryDB.coll.count()
1000
secondaryに接続する2番目の接続を作成
> 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読み取り可能
> 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でデータを書きたい
> 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クライアント書き込みテストレプリケーションセットを受信しないautomatic failover機能:shutdown 31000インスタンス
> 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になるかを確認
> 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になる
# ./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を閉じる
> replicaSet.stopSet()
sharding簡易構築方法参照:http://www.ttlsa.com/html/1787.html転載は出典を明記してください:1分でmongodbアーキテクチャReplica Set&Shardingを構築しますhttp://www.ttlsa.com/html/1830.html