Mongodb集群搭建
共2台服务器,每台上面各一个路由,一个配置,一个分片,MongoDB版本3.4
太子河ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
服务器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 #检查该集合是否启动了数据平衡
当前题目:Mongodb集群搭建
浏览路径:http://pwwzsj.com/article/gjgjhp.html