共2臺服務器,每臺上面各一個路由,一個配置,一個分片,mongodb版本3.4
服務器1:10.2.4.214
服務器2:10.2.4.215
1.首先編寫2臺服務器的配置文件,並將配置文件中的路徑文件夾建立好
214的路由配置文件mongodb_rout.conf:
net: port: 5000 ipv6: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/route.log" logAppend: true processManagement: fork: true sharding: configDB: rsConf/10.2.4.214:5100,10.2.4.215:5100 ...
214的配置配置文件mongodb_cfg.conf:
net: port: 5100 ipv6: true storage: dbPath: "/data/mongodb/formal_5000/data/config" directoryPerDB: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/config.log" logAppend: true storage: journal: enabled: true processManagement: fork: true sharding: clusterRole: configsvr replication: replSetName: rsConf ...
214的分片配置文件mongodb_s0.conf:
storage: dbPath: "/data/mongodb/formal_5000/data/s0" directoryPerDB: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/s0.log" logAppend: true net: port: 5010 ipv6: true processManagement: fork: true storage: journal: enabled: true replication: replSetName: rsShardA sharding: clusterRole: shardsvr ...
215的路由配置文件mongodb_rout.conf:
net: port: 5000 ipv6: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/route.log" logAppend: true processManagement: fork: true sharding: configDB: rsConf/10.2.4.214:5100,10.2.4.215:5100 ...
215的配置配置文件mongodb_cfg.conf:
net: port: 5100 ipv6: true storage: dbPath: "/data/mongodb/formal_5000/data/config" directoryPerDB: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/config.log" logAppend: true storage: journal: enabled: true processManagement: fork: true sharding: clusterRole: configsvr replication: replSetName: rsConf ...
215的分片配置文件mongodb_s0.conf:
storage: dbPath: "/data/mongodb/formal_5000/data/s0" directoryPerDB: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/s0.log" logAppend: true net: port: 5010 ipv6: true processManagement: fork: true storage: journal: enabled: true replication: replSetName: rsShardB sharding: clusterRole: shardsvr ...
在2臺服務器上執行下面命令建立文件夾,否則mongodb無法啓動
mkdir -p /data/mongodb/formal_5000/log/ mkdir -p /data/mongodb/formal_5000/data/config/ mkdir -p /data/mongodb/formal_5000/data/s0
2.啓動2臺配置服務,並配置成副本集
#在2臺服務器上都啓動配置服務 numactl --interleave=all mongod -f /etc/mongodb/formal_5000/mongodb_cfg.conf #進入其中一臺服務進行配置 mongo --port 5100 config={_id:"rsConf",members:[ {_id:0,host:"10.2.4.214:5100"}, {_id:1,host:"10.2.4.215:5100"} ]} rs.initiate(config) #查看副本集配置是否成功 rs.status()
3.啓動2臺分片和路由服務,並配置
#在2臺服務器上都啓動分片服務 numactl --interleave=all mongod -f /etc/mongodb/formal_5000/mongodb_s0.conf #在2臺服務器上都啓動路由服務 numactl --interleave=all mongos -f /etc/mongodb/formal_5000/mongodb_rout.conf #進入214分片並配置分片成副本集的primary mongo --port 5010 config={_id:"rsShardA",members:[ {_id:0,host:"10.2.4.214:5010"} ]} rs.initiate(config) #進入215分片並配置分片成副本集的primary mongo --port 5010 config={_id:"rsShardB",members:[ {_id:0,host:"10.2.4.215:5010"} ]} rs.initiate(config) #進入路由並配置分片 mongo --port 5000 use admin db.runCommand({addshard:"rsShardA/10.2.4.214:5010",name:"shard_0",maxSize:0}) db.runCommand({addshard:"rsShardB/10.2.4.215:5010",name:"shard_1",maxSize:0}) #查看分片配置情況 sh.status()
4.設置monitor_center可分片
use monitor_center use admin db.runCommand({enablesharding:"monitor_center"})
5.使用Studio-3T連接路由、配置和分片並建立用戶(這裏教程省略)
6.關閉數據庫
#先關路由,進入2臺服務器的路由 mongo --port 5000 use admin db.shutdownServer() #再關分片,進入2臺服務器的分片 mongo --port 5010 use admin db.shutdownServer({force:true}) #最後關配置,進入2臺服務器的配置 mongo --port 5100 use admin db.shutdownServer()
7.將keyfile加入到2臺服務器的路由、配置、分片的配置文件中的最後一行裏,如下所示:
214的路由配置文件mongodb_rout.conf:
net: port: 5000 ipv6: true systemLog: destination: file path: "/data/mongodb/formal_5000/log/route.log" logAppend: true processManagement: fork: true sharding: configDB: rsConf/10.2.4.214:5100,10.2.4.215:5100 security: keyFile: "/data/mongodb/formal_5000/key/mongodb_key" ...
其他配置文件類似,都需要添加keyfile
#生成keyFile mkdir -p /data/mongodb/formal_5000/key/ cd /data/mongodb/formal_5000/key echo -e "formal mongodb keyFile" > mongodb_key chmod 600 /data/mongodb/formal_5000/key/mongodb_key
8.最後依次啓動配置、分片、路由
numactl --interleave=all mongod -f /etc/mongodb/formal_5000/mongodb_cfg.conf numactl --interleave=all mongod -f /etc/mongodb/formal_5000/mongodb_s0.conf numactl --interleave=all mongos -f /etc/mongodb/formal_5000/mongodb_rout.conf
PS:
1.平衡器
sh.setBalancerState(true) #啓動平衡器 sh.setBalancerState(false) #關閉平衡器 sh.getBalancerState() #查看平衡器的狀態 sh.isBalancerRunning() #查看平衡器當前是否在運行 #設置平衡器啓動時間範圍,首先確保平衡器是啓動狀態 db.settings.update({ _id: "balancer" },{ $set: { activeWindow : { start : "21:00", stop : "08:00" } } },{ upsert: true }) #取消平衡器啓動時間範圍 db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } })
2.建表
use monitor_center db.createCollection("origdata_20171024") #建表 db.origdata_20171024.createIndex({ mac: 1, time: 1},{ background: true}) #建索引 use admin db.runCommand({shardcollection:"monitor_center.origdata_20171024",key:{mac:1,time:1}}) #集合是否允許分片 db.runCommand({moveChunk:"monitor_center.origdata_20171024",bounds:[{mac:MinKey,time:MinKey},{mac:MaxKey,time:MaxKey}],to:"shard_1"}) #將集合手動移到分片1
3.表的遷移
sh.enableBalancing("monitor_center.origdata_20171024") #開啓該集合的數據平衡 sh.disableBalancing("monitor_center.origdata_20171024") #關閉該集合的數據平衡 db.getSiblingDB("config").collections.findOne({_id : "monitor_center.origdata_20171024"}).noBalance #檢查該集合是否啓動了數據平衡