这期内容当中小编将会给大家带来有关MongoDB中怎么实现分片与集群,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的循化网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
一、 mongodb分片与集群拓扑图
二、分片与集群的部署
1.Mongodb的安装
分别在以上3台服务器安装好mongodb
安装方法见安装脚本。
2.Mongod 创建单个分片的副本集
10.68.4.209
①建立数据文件夹和日志文件夹
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf dbpath=/data/master logpath=/data/log/mongodb/master/mongodb.log pidfilepath=/var/run/mongo_master.pid #directoryperdb=true logappend=true replSet=policydb port=10002 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #slave.conf dbpath=/data/slave logpath=/data/log/mongodb/slave/mongodb.log pidfilepath=/var/run/mongo_slave.pid #directoryperdb=true logappend=true replSet=policydb port=10001 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #arbiter.conf dbpath=/data/arbiter logpath=/data/log/mongodb/arbiter/mongodb.log pidfilepath=/var/run/mongo_arbiter.pid #directoryperdb=true logappend=true replSet=policydb port=10000 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200
③启动mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、备、仲裁节点
主节点:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002 MongoDB shell version: 2.4.9 connecting to: 10.68.4.209:10002/test > use admin switched to db admin >config={ _id:"policydb", members:[ {_id:0,host:'10.68.4.209:10002',priority:2}, {_id:1,host:'10.68.4.209:10001',priority:1}, ... {_id:2,host:'10.68.4.209:10000',arbiterOnly:true}] }; { "_id" : "policydb", "members" : [ { "_id" : 0, "host" : "10.68.4.209:10002", "priority" : 2 }, { "_id" : 1, "host" : "10.68.4.209:10001", "priority" : 1 }, { "_id" : 2, "host" : "10.68.4.209:10000", "arbiterOnly" : true } ] } rs.initiate(config) #初始化 rs.status() #查看集群状态
10.68.4.29
①建立数据文件夹和日志文件夹
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf dbpath=/data/master logpath=/data/log/mongodb/master/mongodb.log pidfilepath=/var/run/mongo_master.pid #directoryperdb=true logappend=true replSet=policydb2 port=10002 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #slave.conf dbpath=/data/slave logpath=/data/log/mongodb/slave/mongodb.log pidfilepath=/var/run/mongo_slave.pid #directoryperdb=true logappend=true replSet=policydb2 port=10001 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #arbiter.conf dbpath=/data/arbiter logpath=/data/log/mongodb/arbiter/mongodb.log pidfilepath=/var/run/mongo_arbiter.pid #directoryperdb=true logappend=true replSet=policydb2 port=10000 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 ③启动mongodb /etc/init.d/mongodb_master start /etc/init.d/mongodb_slave start /etc/init.d/mongodb_arbiter start
④配置主、备、仲裁节点
主节点:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002 MongoDB shell version: 2.4.9 connecting to: 10.68.4.209:10002/test > use admin switched to db admin > config={ _id:"policydb2", members:[ {_id:0,host:'10.68.4.29:10002',priority:2}, {_id:1,host:'10.68.4.29:10001',priority:1}, ... {_id:2,host:'10.68.4.209:10000',arbiterOnly:true}] }; { "_id" : "policydb", "members" : [ { "_id" : 0, "host" : "10.68.4.29:10002", "priority" : 2 }, { "_id" : 1, "host" : "10.68.4.29:10001", "priority" : 1 }, { "_id" : 2, "host" : "10.68.4.29:10000", "arbiterOnly" : true } ] } rs.initiate(config) #初始化 rs.status() #查看集群状态
10.68.4.30
①建立数据文件夹和日志文件夹
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf dbpath=/data/master logpath=/data/log/mongodb/master/mongodb.log pidfilepath=/var/run/mongo_master.pid #directoryperdb=true logappend=true replSet=policydb3 port=10002 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #slave.conf dbpath=/data/slave logpath=/data/log/mongodb/slave/mongodb.log pidfilepath=/var/run/mongo_slave.pid #directoryperdb=true logappend=true replSet=policydb3 port=10001 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #arbiter.conf dbpath=/data/arbiter logpath=/data/log/mongodb/arbiter/mongodb.log pidfilepath=/var/run/mongo_arbiter.pid #directoryperdb=true logappend=true replSet=policydb3 port=10000 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200
③启动mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、备、仲裁节点
主节点:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002 MongoDB shell version: 2.4.9 connecting to: 10.68.4.209:10002/test > use admin switched to db admin > config={ _id:"policydb3", members:[ {_id:0,host:'10.68.4.30:10002',priority:2}, {_id:1,host:'10.68.4.30:10001',priority:1}, ... {_id:2,host:'10.68.4.30:10000',arbiterOnly:true}] }; { "_id" : "policydb", "members" : [ { "_id" : 0, "host" : "10.68.4.30:10002", "priority" : 2 }, { "_id" : 1, "host" : "10.68.4.30:10001", "priority" : 1 }, { "_id" : 2, "host" : "10.68.4.30:10000", "arbiterOnly" : true } ] } rs.initiate(config) #初始化 rs.status() #查看集群状态
2.Mongod 创建单个分片的配置服务器
① 创建配置目录
10.68.4.209 mkdir /data/config
10.68.4.29 mkdir /data/config
10.68.4.30 mkdir /data/config
②准备配置服务器的配置文件
3个服务器的配置服务器的配置文件一致
#config.conf dbpath=/data/config logpath=/data/log/mongodb/config/mongodb.log pidfilepath=/var/run/mongo_config.pid directoryperdb=true logappend=true port=10003 fork=true configsvr=true
③启动配置服务器
/etc/init.d/mongodb_config start
3.Mongod 创建并配置mongos和开启分片模式
① 创建日志目录
Mkdir -p /data/log/mongodb/mongos/
② 准备mongos的配置文件
#mongos.conf logpath=/data/log/mongodb/mongos/mongodb.log pidfilepath=/var/run/mongo_mongos.pid logappend=true port=10004 fork=true configdb=10.68.4.209:10003,10.68.4.29:10003,10.68.4.30:1000
③ 启动mongos
/etc/init.d/mongodb_mongos start
④ 配置分片
sh.addShard("policydb/10.68.4.209:10002") sh.addShard("policydb2/10.68.4.29:10002") sh.addShard("policydb3/10.68.4.30:10002") sh.enableSharding("policydb") db.runCommand({"shardcollection":"policydb.fullPolicyTextInfo_history", "key":{"key":1}}) db.printShardingStatus() #查看分片状态 sh.status({verbose:true}) sh.status()
3.快速创建副本集和配置服务脚本
上图
1.图1
2.图2 config.conf配置文件
3.图3
上述就是小编为大家分享的MongoDB中怎么实现分片与集群了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。