通过我们了解了mongoDB的分片集群,本文将带大家搭建一个mongoDB的分片集群。
下载
1 2 3 4 5 6
| cd /usr/local/src/ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.6.6.tgz tar zxvf mongodb-linux-x86_64-amazon-3.6.6.tgz mv mongodb-linux-x86_64-amazon-3.6.6 ../ cd ~ ln -s /usr/local/mongodb-linux-x86_64-amazon-3.6.6/ mongo
|
搭建config集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| mkdir -p /data/mongodb/config/ cat > /data/mongodb/config.conf <<EOF sharding: clusterRole: configsvr
replication: replSetName: configServer oplogSizeMB: 2048
net: port: 27018 bindIpAll: true
systemLog: destination: file logRotate: reopen path: /data/mongodb/config.log logAppend: true
storage: dbPath: /data/mongodb/config/ journal: enabled: false directoryPerDB: true
processManagement: fork: true EOF
|
1 2 3 4
| cd ~/mongo/bin/ ./mongod --config /data/mongodb/config.conf ./mongo localhost:27018/admin rs.initiate({_id:"configServer",configsvr:true,members:[{_id:0,host:"172.32.220.135:27018"},{_id:1,host:"172.32.210.111:27018"},{_id:2,host:"172.32.210.133:27018"}]})
|
搭建shard集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| mkdir -p /data/mongodb/shard1 cat > /data/mongodb/shard1.conf <<EOF sharding: clusterRole: shardsvr
replication: replSetName: shard1 oplogSizeMB: 2048
net: port: 27017 bindIpAll: true
systemLog: destination: file logRotate: reopen path: /data/mongodb/shard1.log logAppend: true
storage: dbPath: /data/mongodb/shard1/ journal: enabled: false directoryPerDB: true
processManagement: fork: true
setParameter: wiredTigerConcurrentWriteTransactions: 512 EOF
|
1 2 3 4
| cd ~/mongo/bin/ ./mongod --config /data/mongodb/shard1.conf ./mongo localhost:27017/admin rs.initiate({_id:"shard1",members:[{_id:0,host:"172.32.220.114:27017"},{_id:1,host:"172.32.220.18:27017"},{_id:2,host:"172.32.210.133:27017",arbiterOnly:true}]})
|
搭建mongos集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| mkdir -p /data/mongodb/mongos cat > /data/mongodb/mongos.conf <<EOF sharding: configDB: configServer/172.32.220.135:27018,172.32.210.111:27018,172.32.210.133:27018
net: port: 27019 bindIpAll: true
systemLog: destination: file logRotate: reopen path: /data/mongodb/mongos.log logAppend: true
processManagement: fork: true EOF
|
添加分片
1 2 3 4
| cd ~/mongo/bin/ ./mongos --config /data/mongodb/mongos.conf ./mongo localhost:27019/admin sh.addShard("shard1/172.32.220.114:27017");
|
添加分片有时候会超时,出现
WriteConcernFailed: waiting for replication timed out。
多试几次即可。
删除分片
1
| db.runCommand({removeShard: "shard3"})
|
查询分片的副本情况
登录到分片,然后rs.status()
1
| db.runCommand({"moveprimary":"rtms","to":"shard1"})
|
新增复制集成员
1
| rs.add("172.32.220.18:27017");
|
参考 http://www.mongoing.com/docs/tutorial/expand-replica-set.html
删除复制集成员
1
| rs.remove("172.32.220.18:27017");
|
参考 http://www.mongoing.com/docs/tutorial/remove-replica-set-member.html