MongoDB分片
集群搭建 环境准备
mongo1(127.0.0.1) mongo2(127.0.0.1) mongo3(127.0.0.1)
config1(端口:27018) config2(端口:27028) config3(端口:27038)
mongos1(端口:27019) mongos2(端口:27029) mongos3(端口:27039)
Shard1(端口:27001) Shard1(端口:27021) Shard1(端口:27031)
Shard2(端口:27002) Shard2(端口:27022) Shard2(端口:27032)
Shard3(端口:27003) Shard3(端口:27023) Shard3(端口:27033)
实际当中,mong1,mongo2 ,mongo3应当是在三台不同的服务器,mongos应当与config部署在不同的服务器,各shard组副本集应当部署在不同的服务器,
config可以与shard片部署在同一服务器,也就是说要达到性能最优,至少应当有6台服务器,mongos用3台,config和3组Shard用3台。
本文中就以本地Windows环境为例,
用不同的端口作为伪服务器演示搭建配置过程
目录结构
文件配置
mongo1
config
net:
port: 27018 #config1的端口,mongo2下的为27028,mongo3下的为27038
bindIp: 0.0.0.0 #允许连接地址,这里统一设置为允许任何ip连接
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo1\log\config.log #config的日志存储地址,mongo2,mongo3下路径调整即可
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo1\data\config #config的数据存储地址,mongo2、mongo3下做相应的调整
journal:
enabled: true #数据故障恢复和持久化,肯定是开启
#processManagement:
# fork: true #linux下打开此设置,Windows环境下后台启动需要注册服务,
# pidFilePath: /mongo/config/run/mongod.pid
#副本集名称设置
replication:
replSetName: configs
#分片集群角色,配置服务器的固定值
sharding:
clusterRole: configsvr
#这里是集群安全认证配置,首次配置启动集群不打开此配置,先留着,后面再说
#security:
#keyFile: D:\tool\mongodbmy\mongo\mongodb-keyfile.file
#authorization: enabled
shard1.conf
net:
port: 27001 #mongo1下其他两个分片shard2为27002,shard3为27003,mongo2、mongo3下对应上面的服务编排表设置端口
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo1\log\shard1.log #mongo1下其他两个分片shard2为shard2.log,shard3为shard3.log,mongo2、mongo3下对应做调整
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo1\data\shard1 #mongo1下其他两个分片shard2为shard2,shard3为shard3,mongo2、mongo3下对应做调整
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard1/run/mongod.pid
replication:
replSetName: shard1 #副本集名称,下其他两个分片对应调整shard2为shard2,shard3为shard3
sharding:
clusterRole: shardsvr #集群中角色归属
shard2.conf
net:
port: 27002
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo1\log\shard2.log
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo1\data\shard2
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard2/run/mongod.pid
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
shard3.conf
net:
port: 27003
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo1\log\shard3.log
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo1\data\shard3
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard3/run/mongod.pid
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
mongos.conf
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo1\log\mongos.log
# network interfaces
net:
port: 27019
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
#注意!此处配置的连接地址为配置服务器config Serve的副本集名称和所在服务器地址以及端口
sharding:
configDB: configs/127.0.0.1:27018,127.0.0.1:27028,127.0.0.1:27038
mongo2
config
net:
port: 27028 #config1的端口,mongo2下的为27028,mongo3下的为27038
bindIp: 0.0.0.0 #允许连接地址,这里统一设置为允许任何ip连接
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo2\log\config.log #config的日志存储地址,mongo2,mongo3下路径调整即可
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo2\data\config #config的数据存储地址,mongo2、mongo3下做相应的调整
journal:
enabled: true #数据故障恢复和持久化,肯定是开启
#processManagement:
# fork: true #linux下打开此设置,Windows环境下后台启动需要注册服务,
# pidFilePath: /mongo/config/run/mongod.pid
#副本集名称设置
replication:
replSetName: configs
#分片集群角色,配置服务器的固定值
sharding:
clusterRole: configsvr
#这里是集群安全认证配置,首次配置启动集群不打开此配置,先留着,后面再说
#security:
# keyFile: D:\tool\mongodbmy\mongo\mongodb-keyfile.file
# authorization: enabled
shard1.conf
net:
port: 27021 #mongo1下其他两个分片shard2为27002,shard3为27003,mongo2、mongo3下对应上面的服务编排表设置端口
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo2\log\shard1.log #mongo1下其他两个分片shard2为shard2.log,shard3为shard3.log,mongo2、mongo3下对应做调整
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo2\data\shard1 #mongo1下其他两个分片shard2为shard2,shard3为shard3,mongo2、mongo3下对应做调整
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard1/run/mongod.pid
replication:
replSetName: shard1 #副本集名称,下其他两个分片对应调整shard2为shard2,shard3为shard3
sharding:
clusterRole: shardsvr #集群中角色归属
shard2.conf
net:
port: 27022
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo2\log\shard2.log
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo2\data\shard2
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard2/run/mongod.pid
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
shard3.conf
net:
port: 27023
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo2\log\shard3.log
storage:
dbPath: D