三台linux服务器,版本CentOS 7.6
IP | 分片shard | 分片端口 |
196.168.3.151 | shard1 | 20001 |
192.168.3.182 | shard2 | 20002 |
192.168.3.184 | shard3 | 20003 |
版本4.2.6及以上 mongo下载
systemctl stop filewalld
在三台机器上分别创建一下目录
#创建相关目录
mkdir /data/mongos; cd /data/mongos
mkdir {logs, config}
mkdir {configsvr, mongos, shard1, shard2, shard3}
在三台机器上的config文件夹中都创建以下shard1.conf, shard2.conf, shard3.conf, configsvr.conf, mongos.conf
#shard1.conf
systemLog:
destination: file
path: /data/mongos/logs/shard1.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongos/shard1/shard1.pid"
net:
bindIp: 0.0.0.0
port: 20001
storage:
dbPath: "/data/mongos/shard1"
wiredTiger:
engineConfig:
cacheSizeGB: 1
journal:
enabled: true
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "shard1"
sharding:
clusterRole: shardsvr
#shard2.conf
systemLog:
destination: file
path: /data/mongos/logs/shard2.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongos/shard2/shard2.pid"
net:
bindIp: 0.0.0.0
port: 20002
storage:
dbPath: "/data/mongos/shard2"
wiredTiger:
engineConfig:
cacheSizeGB: 1
journal:
enabled: true
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "shard2"
sharding:
clusterRole: shardsvr
#shard3.conf
systemLog:
destination: file
path: /data/mongos/logs/shard3.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongos/shard3/shard3.pid"
net:
bindIp: 0.0.0.0
port: 20003
storage:
dbPath: "/data/mongos/shard3"
wiredTiger:
engineConfig:
cacheSizeGB: 1
journal:
enabled: true
setParameter:
enableLocalhostAuthBypass: true
replication:
replSetName: "shard3"
sharding:
clusterRole: shardsvr
#configsvr.conf
systemLog:
destination: file
path: /data/mongos/logs/configsvr.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongos/configsvr/configsvr.pid"
net:
bindIp: 0.0.0.0
port: 27017
storage:
dbPath: "/data/mongos/configsvr"
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
sharding:
clusterRole: configsvr
replication:
oplogSizeMB: 2048
replSetName: csvr
#mongos.conf
systemLog:
destination: file
path: /data/mongos/logs/configsvr.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongos/configsvr/configsvr.pid"
net:
bindIp: 0.0.0.0
port: 27017
storage:
dbPath: "/data/mongos/configsvr"
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
sharding:
clusterRole: configsvr
replication:
oplogSizeMB: 2048
replSetName: csvr
[root@localhost config]# more mongos.conf
systemLog:
destination: file
path: /data/mongos/logs/mongos.log
logAppend: true
processManagement:
fork: true
pidFilePath: /data/mongos/mongos.pid
net:
bindIp: 0.0.0.0
port: 27020
sharding:
configDB: csvr/192.168.3.151:27017,192.168.3.184:27017,192.168.3.182:27017
#每一个服务器上都要执行
mongod -f ./config/shard1.conf
mongod -f ./config/shard2.conf
mongod -f ./config/shard3.conf
选择任意一台服务器,执行以下操作
mongo --port 20001
#在mongo shell中执行初始化
rs.initiate({_id:"shard1", members:[{_id:0,host:"192.168.3.151:20001"},{_id:1,host:"192.168.3.182:20001"},{_id:2,host:"192.168.3.184:20001"}]})
mongo --port 20002
#在mongo shell中执行初始化
rs.initiate({_id:"shard2", members:[{_id:0,host:"192.168.3.151:20001"},{_id:1,host:"192.168.3.182:20001"},{_id:2,host:"192.168.3.184:20001"}]})
mongo --port 20003
#在mongo shell中执行初始化
rs.initiate({_id:"shard3", members:[{_id:0,host:"192.168.3.151:20001"},{_id:1,host:"192.168.3.182:20001"},{_id:2,host:"192.168.3.184:20001"}]})
初始化之后,集群会自动分配primary和second
在三台服务器上执行
#启动配置服务器
mongod -f ./config/configsvr.conf
#进入mongo shell
mongo --port 27017
#初始化
rs.initiate({_id:"csvr", members:[{_id:0,host:"192.168.3.151:27017"},{_id:1,host:"192.168.3.182:27017"},{_id:2,host:"192.168.3.184:27017"}]})
在三台机器上启动mongos
#启动mongos
mongos -f ./config/mongos.conf
任意一台服务器执行
mongo --port 27020
#在mongo shell中执行
sh.addShard("shard1/192.168.3.151:20001,192.168.3.182:20001,192.168.3.184:20001")
sh.addShard("shard2/192.168.3.151:20002,192.168.3.182:20002,192.168.3.184:20002")
sh.addShard("shard3/192.168.3.151:20003,192.168.3.182:20003,192.168.3.184:20003")