|
一分钟搭建mongodb架构Replica Set&Sharding—ttlsa教程系列之mongodb(七)
在测试试验阶段,我们需要有一个模拟的测试环境来测试应用程序和系统架构各个方面的功能,是否符合需求。在我公司,我常常使用下面的方法来为开发人员搭建mongodb的复制集和分片架构进行测试。我也常用这个方法来模拟线上架构,测试相关内容。
开启一个MongoDB shell,不连接任何mongod
?1# ./mongo --nodb 创建复制集,一个primary两个secondary
?1> replicaSet = newReplSetTest({"nodes": 3}) 启动三个mongod实例
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364> replicaSet.startSet()ReplSetTest Starting SetReplSetTest n is: 0ReplSetTest n: 0ports: [ 31000, 31001, 31002] 31000number{"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: 1ReplSetTest n: 1ports: [ 31000, 31001, 31002] 31001number{"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: 2ReplSetTest n: 2ports: [ 31000, 31001, 31002] 31002number{"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' 复制集初始化
?1234567891011121314151617181920> 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
?123456789101112131415161718192021# ./mongo --nodb> conn1 = newMongo("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条文档
?123>for(i=0; i primaryDB.coll.count()1000 创建第二个连接,连接到secondary
?123456> conn2 = newMongo("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写数据
?12345678910> 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
|
|