一分鐘搭建mongodb架構Replica Set&Sharding—ttlsa教程系列之mongodb(七)
在測試試驗階段,我們需要有一個模擬的測試環境來測試應用程序和系統架構各個方面的功能,是否符合需求。在我公司,我常常使用下面的方法來爲開發人員搭建mongodb的複製集和分片架構進行測試。我也常用這個方法來模擬線上架構,測試相關內容。
開啓一個MongoDB shell,不連接任何mongod
1 | # ./mongo --nodb |
創建複製集,一個primary兩個secondary
1 | > replicaSet = new ReplSetTest({ "nodes" : 3 }) |
啓動三個mongod實例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | > replicaS et.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' |
複製集初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | > replicaSet.initiate() {
"replSetInitiate" : {
"_id" : "testReplSet" ,
"members" : [
{
"_id" : 0 ,
"host" : "nd0302012029:31000"
},
{
"_id" : 1 ,
"host" : "nd0302012029:31001"
},
{
"_id" : 2 ,
"host" : "nd0302012029:31002"
}
]
} } |
啓動了三個實例,分別監聽在31000,31001,31002端口上
當前MongoDB shell窗口會有大量的日誌信息輸出,影響操作,另開啓一個MongoDB shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # ./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條文檔
1 2 3 | > for (i= 0 ; i<<> > primaryDB.coll.count() 1000 |
創建第二個連接,連接到secondary
1 2 3 4 5 6 | > 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可讀
嘗試想secondary寫數據
1 2 3 4 5 6 7 8 9 10 | > 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實例
查看哪個實例變成primary
可見31002實例變成新的master
關閉replica set
sharding簡易搭建方法參見: http://www.ttlsa.com/html/1787.html
轉載請註明出處:一分鐘搭建mongodb架構Replica Set&Sharding