OS:CentOS7
MongoDB 版本:3.6
安装
创建Mongo的yum源文件
vi /etc/yum.repos.d/mongodb-org-3.6.repo
替换为阿里源
[mongodb-org-3.6] name=MongoDB Repository baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.6/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
安装mongo
sudo yum install -y mongodb-org
创建节点数据目录
$ mkdir -p /data/rs-{a,b}-{1,2,3}
启动节点mongo服务
mongod --shardsvr --replSet shard-a --dbpath /data/rs-a-1 --port 30000 --logpath /data/rs-a-1.log --fork mongod --shardsvr --replSet shard-a --dbpath /data/rs-a-2 --port 30001 --logpath /data/rs-a-2.log --fork mongod --shardsvr --replSet shard-a --dbpath /data/rs-a-3 --port 30002 --logpath /data/rs-a-3.log --fork mongod --shardsvr --replSet shard-b --dbpath /data/rs-b-1 --port 30100 --logpath /data/rs-b-1.log --fork mongod --shardsvr --replSet shard-b --dbpath /data/rs-b-2 --port 30101 --logpath /data/rs-b-2.log --fork mongod --shardsvr --replSet shard-b --dbpath /data/rs-b-3 --port 30102 --logpath /data/rs-b-3.log --fork
初始化副本集
mongo localhost:30000 > rs.initiate() 将节点加入副本集中 shard-a:PRIMARY> rs.add("localhost:30001") shard-a:PRIMARY> rs.addArb("localhost:30002") mongo localhost:30100 > rs.initiate() shard-b:OTHER> rs.add("localhost:30101") shard-b:PRIMARY> rs.addArb("localhost:30102")
创建配置服务节点
创建数据目录 mkdir /data/config-{1,2,3} 启动配置节点 mongod --configsvr --replSet cfgReplSet --dbpath /data/config-1 --port 27019 --logpath /data/config-1.log --fork mongod --configsvr --replSet cfgReplSet --dbpath /data/config-2 --port 27020 --logpath /data/config-2.log --fork mongod --configsvr --replSet cfgReplSet --dbpath /data/config-3 --port 27021 --logpath /data/config-3.log --fork 配置服务器初始化副本集 mongo localhost:27019 >rs.initiate({_id:"cfgReplSet",configsvr:true,members:[{_id:0,host:"localhost:27019"},{_id:1,host:"localhost:27020"},{_id:2,host:"localhost:27021"}]})
启动Mongo路由
mongos --configdb cfgReplSet/localhost:27019,localhost:27020,localhost:27021 --logpath /data/mongos.log --fork --port 40000
配置集群
mongo localhost:40000 分片设置 sh.addShard("shard-a/localhost:30000,localhost:30001") sh.addShard("shard-b/localhost:30100,localhost:30101") 验证分片结果 mongos> db.getSiblingDB("config").shards.find() { "_id" : "shard-a", "host" : "shard-a/localhost:30000,localhost:30001", "state" : 1 } { "_id" : "shard-b", "host" : "shard-b/localhost:30100,localhost:30101", "state" : 1 } 使用admin数据库验证 mongos> use admin switched to db admin mongos> db.runCommand({listshards:1}) { "shards" : [ { "_id" : "shard-a", "host" : "shard-a/localhost:30000,localhost:30001", "state" : 1 }, { "_id" : "shard-b", "host" : "shard-b/localhost:30100,localhost:30101", "state" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1524381570, 2), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1524381570, 2) }
数据库分片
sh.enableSharding("mydb") mongos> db.getSiblingDB("config").databases.find() { "_id" : "mydb", "primary" : "shard-b", "partitioned" : true } 分片mydb中的集合myCol,使用id,username作为分片键 mongos> sh.shardCollection("mydb.myCol",{username:1,_id:1}) { "collectionsharded" : "mydb.myCol", "collectionUUID" : UUID("d2ecc073-ed7e-4940-bee5-9e1a45694f23"), "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1524382128, 14), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1524382128, 14) }
集群分片验证
基于《MongoDB Cookbook, Second Edition.pdf》中脚本修改,批量插入数据,将脚本修改为基于MongoDB 3.6的版本
ruby load.rb 100
mongos> sh.status() { "_id" : "cloud-docs", "primary" : "shard-a", "partitioned" : true } cloud-docs.spreadsheets shard key: { "username" : 1, "_id" : 1 } unique: false balancing: true chunks: shard-a 3 shard-b 2 { "username" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "username" : "Abbott", "_id" : ObjectId("5adc9b211a5680820f0d28b8") } on : shard-b Timestamp(2, 0) { "username" : "Abbott", "_id" : ObjectId("5adc9b211a5680820f0d28b8") } -->> { "username" : "Islas", "_id" : ObjectId("5adc9b381a5680820f0d3161") } on : shard-b Timestamp(3, 0) { "username" : "Islas", "_id" : ObjectId("5adc9b381a5680820f0d3161") } -->> { "username" : "Schlabach", "_id" : ObjectId("5adc9b511a5680820f0d3b7c") } on : shard-a Timestamp(3, 1) { "username" : "Schlabach", "_id" : ObjectId("5adc9b511a5680820f0d3b7c") } -->> { "username" : "Velasques", "_id" : ObjectId("5adc9b381a5680820f0d31a1") } on : shard-a Timestamp(2, 4) { "username" : "Velasques", "_id" : ObjectId("5adc9b381a5680820f0d31a1") } -->> { "username" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard-a Timestamp(1, 3)
分片shard-a是cloud-docs数据库的主分片,shard-a有三个块,shard-b有2块,同时在结果中显示数据位于哪个分片中,数据增多,分片中的块也将继续增大
ruby load.rb 800 分片中的块数 chunks: shard-a 9 shard-b 6
数据的分片结果
{ "username" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "username" : "Abbott", "_id" : ObjectId("5adc9b211a5680820f0d28b8") } on : shard-a Timestamp(5, 0) { "username" : "Abbott", "_id" : ObjectId("5adc9b211a5680820f0d28b8") } -->> { "username" : "Charles", "_id" : ObjectId("5adc9b911a5680820f0d5198") } on : shard-b Timestamp(5, 1) { "username" : "Charles", "_id" : ObjectId("5adc9b911a5680820f0d5198") } -->> { "username" : "Grimmett", "_id" : ObjectId("5adc9ddc1a568089ecb9ac91") } on : shard-b Timestamp(4, 3) { "username" : "Grimmett", "_id" : ObjectId("5adc9ddc1a568089ecb9ac91") } -->> { "username" : "Islas", "_id" : ObjectId("5adc9b381a5680820f0d3161") } on : shard-b Timestamp(4, 4) { "username" : "Islas", "_id" : ObjectId("5adc9b381a5680820f0d3161") } -->> { "username" : "Lafond", "_id" : ObjectId("5adc9f3d1a568089ecba0a26") } on : shard-b Timestamp(4, 5) { "username" : "Lafond", "_id" : ObjectId("5adc9f3d1a568089ecba0a26") } -->> { "username" : "Mccluney", "_id" : ObjectId("5adc9b5b1a5680820f0d3f35") } on : shard-b Timestamp(4, 6) { "username" : "Mccluney", "_id" : ObjectId("5adc9b5b1a5680820f0d3f35") } -->> { "username" : "Mendoza", "_id" : ObjectId("5adc9dcf1a568089ecb9a70b") } on : shard-b Timestamp(4, 7) { "username" : "Mendoza", "_id" : ObjectId("5adc9dcf1a568089ecb9a70b") } -->> { "username" : "Overman", "_id" : ObjectId("5adca05d1a568089ecba7df0") } on : shard-a Timestamp(5, 5) { "username" : "Overman", "_id" : ObjectId("5adca05d1a568089ecba7df0") } -->> { "username" : "Rardin", "_id" : ObjectId("5adc9f031a568089ecb9f30e") } on : shard-a Timestamp(5, 6) { "username" : "Rardin", "_id" : ObjectId("5adc9f031a568089ecb9f30e") } -->> { "username" : "Royal", "_id" : ObjectId("5adc9b321a5680820f0d2f4d") } on : shard-a Timestamp(5, 7) { "username" : "Royal", "_id" : ObjectId("5adc9b321a5680820f0d2f4d") } -->> { "username" : "Schlabach", "_id" : ObjectId("5adc9b511a5680820f0d3b7c") } on : shard-a Timestamp(3, 4) { "username" : "Schlabach", "_id" : ObjectId("5adc9b511a5680820f0d3b7c") } -->> { "username" : "Steffensen", "_id" : ObjectId("5adc9e7b1a568089ecb9e8cb") } on : shard-a Timestamp(5, 2) { "username" : "Steffensen", "_id" : ObjectId("5adc9e7b1a568089ecb9e8cb") } -->> { "username" : "Vasconcellos", "_id" : ObjectId("5adca0171a568089ecba6793") } on : shard-a Timestamp(5, 3) { "username" : "Vasconcellos", "_id" : ObjectId("5adca0171a568089ecba6793") } -->> { "username" : "Velasques", "_id" : ObjectId("5adc9b381a5680820f0d31a1") } on : shard-a Timestamp(5, 4) { "username" : "Velasques", "_id" : ObjectId("5adc9b381a5680820f0d31a1") } -->> { "username" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard-a Timestamp(1, 3)
相关推荐
mongodb 分片集群搭建过程(无副本),详细操作步骤,linux命令
MongoDB4.2分片及副本集群搭建 MongoDB集群 MongoDB分片 MongoDB副本 MongoDB副本集群
MongoDB MongoDB集群搭建 环境集群搭建 MongoDB MongoDB集群搭建 环境集群搭建
mongodb集群搭建
手把手教你搭建mongodb集群--分片
7 MongoDB副本集的搭建.mp4 8 MongoDB副本集故障自动切换.mp4 9 MongoDB副本集各实例的优先级设置.mp4 10 MongoDB副本集的伸缩.mp4 11 MongoDB数据的备份和恢复.mp4 12 Python简单操作MongoDB.mp4 13 Python获取...
(3) 请在报告中使用文字和截图详细描述MongoDB分片集群搭建及配置的主要步骤: - 搭建配置集服务器(Mongod) - 搭建分片集服务器(Mongod) - 搭建路由节点服务器(Mongos) - 添加分片集到集群 - 创建数据库...
自己整理了Mongodb集群搭建的几种方式,按照步骤能够一步步实现~
详细讲解mongodb高性能、高可用集群的搭建步骤,包括副本集与分片。每一步都有解释和说明,包括整个的集群结构,学习mongodb的不要错过这个资源
mongodb3.4集群搭建 CentOS7 mongodb3.4 mongodb个人学习
MongoDB4.2分片及副本集群搭建详细指导手册,详细描述了MongoDB4.2数据库服务器集群的搭建以及副本和分片服务的配置过程及具体操作指令。
13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群...
在网上看到很多朋友关于在linux下搭建mongodb的帖子,而很少有在windows下的,于是,我尝试着搭建了一个windows下的集群,可供参考
简单搭建mongodb集群,可以多实例访问模拟的吧数据库。分别为四台主服务器,两台从服务器,一台仲裁服务器
MongoDB介绍和副本集群搭建
spring+mongodb集群搭建,包括单个实例和集群实例,详情见博客:http://blog.csdn.net/qq_16497617/article/details/52817335
搭建Mongodb分片集群的步骤文档,度娘上很多坑,被坑的可以看看,本文档专门埋坑。
公司单节点升级到分片下,搜索了一圈大多数MongoDB集群的部署方案都是分片+副本集,没有讲述如何从单节点升级到分片+复制集的方法,亲自试验后记录整个过程以及中间的参考内容和疑难解决,以备后查。内容包含复制集+...