MongoDB 学习笔记之 分片和副本集混合运用

时间:2021-06-29 16:24:03

 分片和副本集混合运用:

基本架构图:

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 学习笔记之 分片和副本集混合运用

大功告成!