MongoDB 學習筆記之 分片和副本集混合運用
分片和副本集混合運用:
基本架構圖:
搭建詳細配置:
3個shard + 3個replicat set + 3個configserver + 3個Mongos
shardrsname |
Primary |
Secondary |
Secondary |
port |
bigdata-sh-a |
bigdata-sh-a1 |
bigdata-sh-a2 |
bigdata-sh-a3 |
28111 |
bigdata-sh-b |
bigdata-sh-b2 |
bigdata-sh-b1 |
bigdata-sh-b3 |
28112 |
bigdata-sh-c |
bigdata-sh-c3 |
bigdata-sh-c2 |
bigdata-sh-c1 |
28113 |
bigdata-cs |
configserver1 |
configserver2 |
configserver3 |
28200 |
在三臺Linux機器的mongo根目錄下建立如下文件夾:
Log:
/usr/local/mongodb/logs/a1
/usr/local/mongodb/logs/b1
/usr/local/mongodb/logs/c1
/usr/local/mongodb/logs/configserver1
DB:
/usr/local/mongodb/db/a1
/usr/local/mongodb/db/b1
/usr/local/mongodb/db/c1
/usr/local/mongodb/db/configserver1
bigdata-sh-a1.conf:
# mongod config systemLog: destination: file logAppend: true path: /usr/local/mongodb/logs/a1/mongodb.log # Where and how to store data. storage: dbPath: /usr/local/mongodb/db/a1 journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs # fork : fork and run in background # pidFilePath:location of pidfile processManagement: fork: true pidFilePath: /usr/local/mongodb/pidFile/mongod-a1.pid # network interfaces # Listen to local interface only, comment to listen on all interfaces. net: port: 28111 bindIp: 0.0.0.0 #security: enabled disabled #security: # keyFile: /mongodb/keyfile # clusterAuthMode: keyFile #operationProfiling: operationProfiling: slowOpThresholdMs: 1000 mode: slowOp #replication: replication: replSetName: bigdata-sh-a #sharding: sharding: clusterRole: shardsvr ## Enterprise-Only Options #auditLog: #snmp:
bigdata-sh-configserver1.conf:
# mongod config systemLog: destination: file logAppend: true path: /usr/local/mongodb/logs/configserver1/mongodb.log # Where and how to store data. storage: dbPath: /usr/local/mongodb/db/configserver1 journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs # fork : fork and run in background # pidFilePath:location of pidfile processManagement: fork: true pidFilePath: /usr/local/mongodb/pidFile/mongod-configserver1.pid # network interfaces # Listen to local interface only, comment to listen on all interfaces. net: port: 28200 bindIp: 0.0.0.0 #security: enabled disabled #security: # keyFile: /mongodb/keyfile # clusterAuthMode: keyFile #operationProfiling: operationProfiling: slowOpThresholdMs: 1000 mode: slowOp #replication: replication: replSetName: bigdata-cs #sharding: sharding: clusterRole: configsvr ## Enterprise-Only Options #auditLog: #snmp:
配置完成後,啟動服務:
./mongod -f ../conf/bigdata-sh-a1.conf
./mongod -f ../conf/bigdata-sh-b1.conf
./mongod -f ../conf/bigdata-sh-c1.conf
./mongod -f ../conf/bigdata-sh-configserver1.conf
./mongod -f ../conf/bigdata-sh-a2.conf
./mongod -f ../conf/bigdata-sh-b2.conf
./mongod -f ../conf/bigdata-sh-c2.conf
./mongod -f ../conf/bigdata-sh-configserver2.conf
./mongod -f ../conf/bigdata-sh-a3.conf
./mongod -f ../conf/bigdata-sh-b3.conf
./mongod -f ../conf/bigdata-sh-c3.conf
./mongod -f ../conf/bigdata-sh-configserver3.conf
創建副本集:
./mongo hadoop1:28111/admin
use admin
rs.initiate()
rs.add("hadoop2:28111")
rs.add("hadoop3:28111")
./mongo hadoop2:28112/admin
rs.initiate()
rs.add("hadoop1:28112")
rs.add("hadoop3:28112")
./mongo hadoop3:28113/admin
rs.initiate()
rs.add("hadoop2:28113")
rs.add("hadoop1:28113")
./mongo hadoop1:28200/admin
rs.initiate()
rs.add("hadoop2:28200")
rs.add("hadoop3:28200")
在三臺linux上啟動Mongos:
./mongos --port 28300 --configdb bigdata-cs/hadoop1:28200,hadoop2:28200,hadoop3:28200 --fork --logpath /usr/local/mongodb/logs/mongos/mongos.log --bind_ip 0.0.0.0
連接其中一臺Mongos,增加分片:
./mongo hadoop1:28300/admin
use admin
sh.addShard("bigdata-sh-a/hadoop1:28111,hadoop2:28111,hadoop3:28111")
sh.addShard("bigdata-sh-b/hadoop1:28112,hadoop2:28112,hadoop3:28112")
sh.addShard("bigdata-sh-c/hadoop1:28113,hadoop2:28113,hadoop3:28113")
sh.enableSharding("shop")
sh.shardCollection("shop.goods",{"goodid": 1});
插入大量數據進行驗證:
for(var i=80002; i<=90001; i++){ db.goods.insert({goodid: i, name: "My first shard"}) }
大功告成!
MongoDB 學習筆記之 分片和副本集混合運用