准备两台机器192.168.100.87,192.168.100.91
1.搭建分片,副本集rep1,rep2
分别在每个机器创建目录/root/mongo/rep1/log,/root/mongo/rep1/data,/root/mongo/rep1/conf,conf中创建文件mongo.cfg
systemLog:
destination: file
path: /root/mongo/repl1/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /root/mongo/repl1/data
directoryPerDB: true
wiredTiger:
engineConfig:
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
net:
port: 37017
bindIp: 0.0.0.0
replication:
oplogSizeMB: 2048
replSetName: rep1
sharding:
clusterRole: shardsv
./mongod -f /root/mongo/repl1/conf/mongo.cf分别启动每个mongo服务
mongo --port 37017连接一台机器
config = {_id: 'rep1', members: [
{_id: 0, host: '192.168.100.87:37017'},
{_id: 1, host: '192.168.100.91:37017'}
]
}
rs.initiate(config)
rs.conf()
rs.status()
rep2同rep1配置
2. 配置config副本,按下面的配置三个服务
分别在每个机器创建目录/root/mongo/config1/log,/root/mongo/config1/data,/root/mongo/config1/conf,conf中创建文件mongo.cfg
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /root/mongo/config1/log/config.log
# Where and how to store data.
storage:
dbPath: /root/mongo/config1/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true
# network interfaces
net:
port: 47017
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: configs
sharding:
clusterRole: configsvr
同rep1配置副本
3.配置路由
分别在每个机器创建目录/root/mongo/mongos/log,/root/mongo/mongos/conf,conf中创建文件mongo.cfg
分别配置
systemLog:
destination: file
logAppend: true
path: /root/mongo/mongos/log/mongo.log
processManagement:
fork: true
net:
port: 57017
bindIp: 0.0.0.0
sharding:
configDB: configs/192.168.100.87:47017,192.168.100.87:47018,192.168.100.91:47017
登陆任意一个路由
use admin
sh.addShard("rep1/192.168.100.87:37017,192.168.100.91:37017")
sh.addShard("rep2/192.168.100.87:37018,192.168.100.91:37018")
sh.status()
4.使用分片
use testShard
for(i=1;i<=50000;i++){db.user.insert({"name":"zhangsan"+i,"age":18})}
sh.enableSharding("testShard")
sh.status()能查看到分片信息
参考:https://blog.csdn.net/hezhixiang/article/details/82905786
https://blog.csdn.net/kevinmcy/article/details/82712074