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:\software\MongoDB\mongodb_s\mongo2\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\mongo2\log\mongos.log #mongos没有数据存储地址,只有日志输出地址,mongo2、mongo3下做对应的地址调整
# network interfaces
net:
port: 27029 # mongos1的端口,mongos2为27029,mongos3为27039
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
mongo3
config
net:
port: 27038 #config1的端口,mongo2下的为27028,mongo3下的为27038
bindIp: 0.0.0.0 #允许连接地址,这里统一设置为允许任何ip连接
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo3\log\config.log #config的日志存储地址,mongo2,mongo3下路径调整即可
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo3\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: 27031 #mongo1下其他两个分片shard2为27002,shard3为27003,mongo2、mongo3下对应上面的服务编排表设置端口
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo3\log\shard1.log #mongo1下其他两个分片shard2为shard2.log,shard3为shard3.log,mongo2、mongo3下对应做调整
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo3\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: 27032
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo3\log\shard2.log
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo3\data\shard2
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard2/run/mongod.pid
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
shard3.conf
net:
port: 27033
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\software\MongoDB\mongodb_s\mongo3\log\shard3.log
storage:
dbPath: D:\software\MongoDB\mongodb_s\mongo3\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\\mongo3\\log\\mongos.log
# network interfaces
net:
port: 27039 # mongos1的端口,mongos2为27029,mongos3为27039
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
分片集群必须按照一定的顺序启动:config serve,Shard ,Mongos
配置config server
启动服务器
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo1\config\config.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo2\config\config.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo3\config\config.conf
连接服务器配置
[1]然后新开窗口连接到任意一台config server 【真实服务器下,不要忘记指定IP,默认为127.0.0.1】
mongo -host 127.0.0.1 --port 27018
[2]配置配置服务器副本集
config = {
_id : "configs",
members :
[
{_id : 0, host : "127.0.0.1:27018" },
{_id : 1, host : "127.0.0.1:27028" },
{_id : 2, host : "127.0.0.1:27038" }
]
}
[3]初始化副本集
rs.initiate(config)
[4]查看副本集状态
rs.status()
上述【1】【2】【3】【4】四个指令执行完毕后, mongos1/mongos2/mongos3服务器中 data/config 和 logs/config 文件夹中将会生产相应的文件
配置Shard服务
启动服务器
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo1\config\shard1.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo2\config\shard1.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo3\config\shard1.conf
连接服务器配置
【A1】新开窗口连接到mong1文件夹的shard1
mongosh.exe --port 27001
【B1】配置mong1文件夹的shard1的副本集:
config = { _id : "shard1",
members : [
{_id : 0, host : "127.0.0.1:27001"},
{_id : 1, host : "127.0.0.1:27021"},
{_id : 2, host : "127.0.0.1:27031"}
] }
【C1】初始化mong1文件夹的副本集:
rs.initiate(config);
shard2:
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo1\config\shard2.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo2\config\shard2.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo3\config\shard2.conf
【A2】新开窗口连接到mong2文件夹的shard2
mongosh.exe --port 27002
【B2】配置mong2文件夹的shard2的副本集:
config = {
_id : "shard2",
members :
[
{_id : 0,host : "127.0.0.1:27002"},
{_id : 1,host : "127.0.0.1:27022"},
{_id : 2,host : "127.0.0.1:27032"}
]
}
【C2】初始化mong2文件夹的副本集:
rs.initiate(config);
开启分片3副本集:
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo1\config\shard3.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo2\config\shard3.conf
mongod.exe -f D:\software\MongoDB\mongodb_s\mongo3\config\shard3.conf
【A3】新开窗口连接到mong3文件夹的shard3
mongo --port 27003
【B3】配置mong3文件夹的shard3的副本集:
config = {
_id : "shard3",
members :
[
{_id : 0, host : "127.0.0.1:27003"},
{_id : 1, host : "127.0.0.1:27023"},
{_id : 2, host : "127.0.0.1:27033"}
]
}
【C3】初始化mong3文件夹的副本集:
rs.initiate(config);
配置mongos服务器
启动服务器
开启三个窗口:
mongos.exe -f D:\software\MongoDB\mongodb_s\mongo1\config\mongos.conf
mongos.exe -f D:\software\MongoDB\mongodb_s\mongo2\config\mongos.conf
mongos.exe -f D:\software\MongoDB\mongodb_s\mongo3\config\mongos.conf
连接服务器配置
【S1】新开一个窗口:连接任意一个mongos,
mongo --port 27019
【S2】然后使用admin库,启用集群分片:
use admin;
sh.addShard("shard1/127.0.0.1:27001,127.0.0.1:27021,127.0.0.1:27031");
sh.addShard("shard2/127.0.0.1:27002,127.0.0.1:27022,127.0.0.1:27032");
sh.addShard("shard3/127.0.0.1:27003,127.0.0.1:27023,127.0.0.1:27033");
至此,分片集群搭建完成.
测试MongoDB服务器分片功能
#设置一下chunk的大小:
use config;
db.setting.save({"_id":"chunksize","value":1}) # 设置块大小为1M是方便实验,不然需要插入海量数据
#模拟写入数据
use testdb;
#启用数据库分片
sh.enableSharding("testdb")
#创建集合
use testdb;
db.createCollection("user");
db.user.createIndex({"name":"hased"}) # 以"name"作哈希索引分片键
sh.shardCollection( "testdb.user", { "name" : "hashed" } ) #对user集合启用分片
for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"zzx"+i})} #模拟往mytest数据库的user表写入5万数据
sh.status() # 查看分片情况
sh.help() # 查看sh下所有指令
db.settings.help() # 查看数据库分片帮助指令
db.runCommand({listShards:1}) # 查看分片列表,所有分片
测试MongoDB服务器分片功能
#设置一下chunk的大小:
use config;
db.setting.save({"_id":"chunksize","value":1}) # 设置块大小为1M是方便实验,不然需要插入海量数据
#模拟写入数据
use testdb;
#启用数据库分片
sh.enableSharding("testdb")
#创建集合
use testdb;
db.createCollection("user");
db.user.createIndex({"name":"hased"}) # 以"name"作哈希索引分片键
sh.shardCollection( "testdb.user", { "name" : "hashed" } ) #对user集合启用分片
for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"zzx"+i})} #模拟往mytest数据库的user表写入5万数据
sh.status() # 查看分片情况
sh.help() # 查看sh下所有指令
db.settings.help() # 查看数据库分片帮助指令
db.runCommand({listShards:1}) # 查看分片列表,所有分片
如果看到数据平均分布于shard1,shard2,shard3,那么说明我们搭建成功,启用分片成功。