配置
- 版本:
mongodb-linux-x86_64-rhel70-3.4.15
- 服务器:
CentOS Linux release 7.3.1611 (Core)
服务器信息
IP | Port | replSetName | clusterRole | 备注 |
---|---|---|---|---|
127.0.0.1 | 27020 | rs-mongosvr | configsvr[固定值] | 配置服务器1 |
127.0.0.1 | 27021 | rs-mongosvr | configsvr[固定值] | 配置服务器2 |
127.0.0.1 | 27018 | none[只分片] | shardsvr[固定值] | 分片服务器1 |
127.0.0.1 | 27019 | none[只分片] | shardsvr[固定值] | 分片服务器2 |
127.0.0.1 | 27030 | none[单路由] | none | 路由服务器 |
测试配置文件内容如下:
- 配置服务器1
systemLog:
destination: file
path: "logs20/mongod.log"
logAppend: true
storage:
dbPath: "data20"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27020
setParameter:
enableLocalhostAuthBypass: false
#security:
# authorization: "enabled"
replication:
replSetName: rs-mongosvr
sharding:
clusterRole: configsvr
- 配置服务器2
systemLog:
destination: file
path: "logs21/mongod.log"
logAppend: true
storage:
dbPath: "data21"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27021
setParameter:
enableLocalhostAuthBypass: false
#security:
# authorization: "enabled"
replication:
replSetName: rs-mongosvr
sharding:
clusterRole: configsvr
- 分片服务器1
systemLog:
destination: file
path: "logs18/mongod.log"
logAppend: true
storage:
dbPath: "data18"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27018
#security:
# authorization: "enabled"
#replication:
# replSetName: rs-mongodb
sharding:
clusterRole: shardsvr
- 分片服务器2
systemLog:
destination: file
path: "logs19/mongod.log"
logAppend: true
storage:
dbPath: "data19"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27019
#security:
# authorization: "enabled"
#replication:
# replSetName: rs-mongodb
sharding:
clusterRole: shardsvr
- 路由服务器
systemLog:
destination: file
path: "logs30/mongods.log"
logAppend: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27030
sharding:
configDB: rs-mongosvr/127.0.0.1:27020,127.0.0.1:27021
启动过程:
- 启动配置服务器,使用相同【的副本集名称】和【分片角色】
rs.initiate(
{
_id: "rs-mongosvr",
configsvr: true,
members: [
{ _id : 0, host : "127.0.0.1:27020" },
{ _id : 1, host : "127.0.0.1:27021" }
]
}
)
其他命令使用:rs.help()
查看
-
启动分片服务器,暂时不使用副本集,所以只需要设置【分片角色】
-
启动路由服务器,连接路由服务器端口进行配置:
-
配置路由服务器
sh.addShard( "127.0.0.1:27018")``````sh.addShard( "127.0.0.1:27019")
sh.enableSharding("<database>")
- 对一个collection进行分片:
- 注意如果collection已经有数据,则必须在对collection分片之前使用
db.collection.createIndex()
在片键上建立索引,如果collection是空的则直接对collection进行分片,MongoDB会自动建立索引。
- Hashed:
sh.shardCollection("<database>.<collection>", { <shard key> : "hashed" } )
- Ranged:
sh.shardCollection("<database>.<collection>", { <shard key> : <direction> } )
- 注意如果collection已经有数据,则必须在对collection分片之前使用
手动预先分块:
for(var i=1;i<=10;i++)
{
sh.splitAt('test.user',{user_id:i*10})
}
意思是在user
表上进行预先分块,每当user_id
(user_id
必须是片键)为10,20,30…的时候进行分块,这样在还没有数据的时候块就已经分好了,只等着数据填进来,避免了trunk在服务器上移动导致IO开销增加。
结合复制集
如果需要给分片加上复制集,需要添加如下两个步骤:
- 按照正常方式给每个分片服务器添加各自的副本集。
- 在路由服务器添加分片服务器的时候需要使用:
sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")
(添加的是primary)来添加。