Mongodb Sharding 集群配置
配置config
/usr/local/mongodb/bin/mongod --configsvr --dbpath /usr/local/mongodb/config1 --port --logpath=/usr/local/mongodb/config1/config.log --replSet configs & /usr/local/mongodb/bin/mongod --configsvr --dbpath /usr/local/mongodb/config2 --port --logpath=/usr/local/mongodb/config2/config.log --replSet configs & /usr/local/mongodb/bin/mongod --configsvr --dbpath /usr/local/mongodb/config3 --port --logpath=/usr/local/mongodb/config3/config.log --replSet configs & rs.initiate({
_id:"configs", // replSet指定的名称
members:[{
_id:,
host:"127.0.0.1:28001" // 主节点ip与端口
}]
}) rs.add("127.0.0.1:28002");
rs.add("127.0.0.1:28003"); 配置路由设置
/usr/local/mongodb/bin/mongos --port --configdb="configs/127.0.0.1:28001,127.0.0.1:28002,127.0.0.1:28003" --logpath /usr/local/mongodb/mongos/mongos.log 配置sharding 节点
/usr/local/mongodb/bin/mongod -shardsvr -dbpath=/usr/local/mongodb/c1 -port -logpath=/usr/local/mongodb/c1/.log &
/usr/local/mongodb/bin/mongod -shardsvr -dbpath=/usr/local/mongodb/c2 -port -logpath=/usr/local/mongodb/c2/.log &
/usr/local/mongodb/bin/mongod -shardsvr -dbpath=/usr/local/mongodb/c3 -port -logpath=/usr/local/mongodb/c3/.log & 路由配置
/usr/local/mongodb/bin/mongo --port
db.runCommand({ addshard:"127.0.0.1:29017" })
db.runCommand({ addshard:"127.0.0.1:29018" })
db.runCommand({ addshard:"127.0.0.1:29019" }) db.runCommand({"enablesharding": "test"}) 对chunk的移动
db.adminCommand({moveChunk : "test.yhl", find : {id:{$gt:}}, to : "shard0002"});
修改chunk大小
mongos> db.settings.save( { _id:"chunksize", value: 1 } )
配置Sharding:
登录到mongos,添加Shard节点
在为collection分片前,必须让该集合所属的数据库具有分片的功能,一旦你开启了某个数据库的分片,MongoDB会分配一个主片。
./bin/mongo --port 40000
> use admin #此操作需要连接admin库
> db.runCommand({ addshard:"localhost:27017" }) #添加 Shard Server 或者用 sh.addshard()命令来添加,下同;
{ "shardAdded" : "shard0000", "ok" : 1 }
> db.runCommand({ addshard:"localhost:27018" })
{ "shardAdded" : "shard0001", "ok" : 1 }
> db.runCommand({ enablesharding:"test" }) #设置分片存储的数据库
{ "ok" : 1 }
> db.runCommand({ shardcollection: "test.users", key: { id:1 }})
# 设置分片的集合名称。且必须指定Shard Key,系统会自动创建索引,然后根据这个shard Key来计算。
# 实际中尤其不要轻易选择自增_id作为片键,片键也可以是多个字段的组合。
{ "collectionsharded" : "test.users", "ok" : 1 }
> sh.status(); #查看片的状态
> printShardingStatus(db.getSisterDB("config"),1); # 查看片状态(完整版);
> db.stats(); # 查看所有的分片服务器状态
#查看分片后的情况
> use config
switched to db config
> db.databases.find()
{ "_id" : "test", "primary" : "shard0000", "partitioned" : true }
> db.chunks.find()
{ "_id" : "test.user-_id_MinKey", "ns" : "test.user", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : { "$maxKey" : 1 } }, "shard" : "shard0000", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("5677cc4015fdf4f1ffbb15bd") }
如上建库的时候要登陆mongos来建,建好一个库,mongos来自动给你分配是建立在哪个shard上,但你也可以运行命令来把它移动到别的shard上。例如:在mongos上建立了一个库叫recsys0库。运行db.printShardingStatus()命令我们可以看到这个recsys0是建到了哪个shard,如果建到了shard4,而你想改到shard1,那么可以运行命令,db.runCommand( { movePrimary: “recsys0”, to: “shard1” }),这个库就会从shard4挪到shard1上。可通过db.printShardingStatus()再次检查。
删除分片:
db.runCommand({"removeshard":"192.168.245.131:27017"})
对已存在数据分片:
假设现在存在一个数据很大的children数据库,在192.168.245.129:27019上面,需要将这些数据进行分片。
1)连接到mongos实例,将192.168.245.129:27019添加到分片集群中。
mongos> sh.addShard("192.168.245.129:27019")
{ "shardAdded" : "shard0004", "ok" : 1 }
注意集群分片中不能与新添加的分片中有相同的数据库,否则会报错。
在需要的数据库上开启分片功能
mongos> sh.enableSharding("children")
{ "ok" : 1 }
对children数据库下的集合进行分片。注意:对已存在的数据进行分片,一定要保证shard key字段是索引。
mongos> sh.shardCollection("children.children",{"user_id":1})
shard key:
以上分片都使用了MongoDB本身提供了Auto-Sharding的功能,Auto-Sharding的一些缺陷:
- 如果选择了单一的Sharding Key,会造成分片不均衡,无法充分利用每个分片集群的能力。为了弥补单一Sharding Key的缺点,引入复合Sharing Key,然而复合Sharding Key会造成性能的消耗。
- count值计算不准确的问题,数据Chunk在分片之间迁移时,特定数据可能会被计算2次,造成count值计算偏大的问题;
- Balancer的稳定性&智能性问题,Sharing的迁移发生时间不确定,一旦发生数据迁移会造成整个系统的吞吐量急剧下降。为了应对Sharding迁移的不确定性,我们可以强制指定在业务访问的低峰期做Sharding迁移。
手动分片:
由程序来判断:
关闭自动分片:sh.stopBalancer()
只是在4个不同的shard上建立4个库。至于读写操作会对应哪个库,由程序来判断。
MongoDB还带一种手动预分片:
用split命令对空集合进行手动的切分
mongos> use admin
switched to db admin
mongos> db.runCommand({"enablesharding":"myapp"})
mongos> db.runCommand({"shardcollection":"myapp.users","key":{"email":1}})
for ( var x=97; x<97+26; x++ ){
for( var y=97; y<97+26; y+=6 ) {
var prefix = String.fromCharCode(x) + String.fromCharCode(y);
db.runCommand( { split : "myapp.users" , middle : { email : prefix } } );
}
}
利用moveChunk命令手动的移动分割的块:
var shServer = [ "sh0.example.net", "sh1.example.net", "sh2.example.net", "sh3.example.net", "sh4.example.net" ];
for ( var x=97; x<97+26; x++ ){
for( var y=97; y<97+26; y+=6 ) {
var prefix = String.fromCharCode(x) + String.fromCharCode(y);
db.adminCommand({moveChunk : "myapp.users", find : {email : prefix}, to : shServer[(y-97)/6]})
}
}
每个分片设置成副本集:
作者:bluebule
链接:https://www.jianshu.com/p/baed80df9300
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Mongodb Sharding 集群配置的更多相关文章
- Mongo 3.6.1版本Sharding集群配置
Mongo低版本和高版本的sharding集群配置,细节不太一样.目前网上的配置文档大都是针对低版本的.本人在配置3.6.1版本的mongosharding集群的过程中,碰到不少问题,官方文档没有直观 ...
- mongoDB Replica集群配置(1主+1从+1仲裁)
1.mongoDB节点介绍 主节点(Primary) 在复制集中,主节点是唯一能够接收写请求的节点.MongoDB在主节点进行写操作,并将这些操作记录到主节点的oplog中.而从节点将会从oplog复 ...
- mongodb sharding集群搭建
创建虚拟机,如果是使用copy的方式安装系统,记得修改机器名,否则所有的机器名称都一样,会造成安装失败 同时关闭掉防火墙,将所有的机器的时间调成一致,master和slave的heartbeat间隔不 ...
- MongoDB 分片集群配置
本文测试环境为 CentOS 7 和 MongoDB 最新版 (4.0.12) 使用 root 操作 (实际操作中使用非 root 账户启动报错) 零.服务器分配 服务器 102 服务器 103 服务 ...
- 【华为云技术分享】MongoDB经典故障系列五:sharding集群执行sh.stopBalancer()命令被卡住怎么办?
[摘要] MongoDB sharding集群执行sh.stopBalancer()命令时被卡住怎么办?别慌,华为云数据库来给您支招,收下这份方案指南,让您分分钟远离被自建MongoDB数据库支配的恐 ...
- elasticsearch与mongodb分布式集群环境下数据同步
1.ElasticSearch是什么 ElasticSearch 是一个基于Lucene构建的开源.分布式,RESTful搜索引擎.它的服务是为具有数据库和Web前端的应用程序提供附加的组件(即可搜索 ...
- MongoDB高可用集群配置的方案
>>高可用集群的解决方案 高可用性即HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性. ...
- Mongodb中Sharding集群
随着mongodb数据量的增多,可能会达到单个节点的存储能力限制,以及application较大的访问量也会导致单个节点无法承担,所以此时需要构建集群环境,并通过sharding方案将整个数据集拆分成 ...
- 网易云MongoDB分片集群(Sharding)服务已上线
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. MongoDB sharding cluster(分片集群)是MongoDB提供的数据在线水平扩展方案,包括 ...
随机推荐
- LeetCode_443. String Compression
443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...
- consul reconnect_timeout
reconnect_timeout这将控制从集群中彻底删除发生故障的节点需要多长时间.默认值为72小时,建议将其设置为至少为节点或网络分区的预期可恢复的最大停机时间的两倍.警告:将此时间设置得太低可能 ...
- springboot2 配置 https
package cn.xiaojf.aibus.configure; import org.apache.catalina.Context; import org.apache.catalina.co ...
- LeetCode 537. 复数乘法(Complex Number Multiplication)
537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...
- [转帖]Windows 7寿终正寝 为何Windows 10屡被吐槽它却无比经典?
Windows 7寿终正寝 为何Windows 10屡被吐槽它却无比经典? https://www.cnbeta.com/articles/tech/908897.htm 是的,一代经典操作系统Win ...
- Java生成菜单树(目录树)的几种方式
本文介绍两种不同生成多级目录树的方式:1. 递归生成,2. map+list 集合生成.最下方会附上完整代码. 生成树的基本规则:子节点的par_id等于父节点的id. 1. 实体类 import ...
- tensorflow-简单的神经网络
本次笔记是关于tensorflow1的代码,由于接触不久没有跟上2.0版本,这个代码是通过简单的神经网络做一个非线性回归任务,(如果用GPU版本的话第一次出错就重启) import tensorflo ...
- pythony--运算符
python运算符 运算符表达式 python运算符: 赋值运算符.算数运算符.关系运算符.逻辑运算符 python表达式: 是将不同的数据(包括变量.函数)用运算符号按一定规则连接起来的一种式子 ...
- 数组中重复的数字(Golang)
使用哈希表 package main import "fmt" func main() { a := [...]int{2,3,1,0,2,5,3} num := make(map ...
- AS3中 is,as,typeof的区别
AS3中 is,as,typeof的区别 . var my_num:Number=9;trace(typeof my_num);var my_object:Array=["语文", ...