目录

说明

配置说明

gitee

3台机器

端口

安装

下载

解压文件

创建文件夹

目录结构

#按顺序启动

shard集群配置

#配置复制集 shard1,注意ip和端口

#初始化副本集配置

#配置复制集 shard2

#配置复制集 shard3

config集群配置

#启动各个节点:

#创建config集群

mongos节点配置

#启动mongos

#登录到mongos

#添加 分片集群

#查看分片状态

#查看集群状态

使用

#创建分片,启动

开机启动脚本


说明

本文参考了

https://www.cnblogs.com/clsn/p/8214345.html#auto_id_42

http://www.lanceyan.com/tech/arch/mongodb_shard1.html

https://blog.csdn.net/aafeiyang/article/details/82109662

https://www.cnblogs.com/vansky/p/9640268.html

配置说明

gitee

配置文件在gitee上

https://gitee.com/DDDD/mongodb_settings

3台机器

jiba01 10.10.10.100

jiba02 10.10.10.101

jiba03 10.10.10.102

端口

  • mongos端口  21000
  • config集群端口  22000    集群名字 (replSetName)configReplSet
  • 3个分片集群端口
  • shard1:23001 集群名字(replSetName)shard1
  • shard2:23002 集群名字(replSetName)shard2
  • shard3:23003 集群名字(replSetName)shard3

安装

下载

#下载
[root@jiba01 files]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz
--2019-07-09 04:38:48--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 13.249.236.15, 13.249.236.35, 13.249.236.77, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|13.249.236.15|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 84996443 (81M) [application/x-gzip]
Saving to: ‘mongodb-linux-x86_64-4.0.10.tgz’100%[===========================================================================>] 84,996,443  2.95MB/s   in 22s2019-07-09 04:39:13 (3.75 MB/s) - ‘mongodb-linux-x86_64-4.0.10.tgz’ saved [84996443/84996443]

解压文件

[root@jiba01 usr]# tar -zxvf mongodb-linux-x86_64-4.0.10.tgz
mongodb-linux-x86_64-4.0.10/THIRD-PARTY-NOTICES.gotools
mongodb-linux-x86_64-4.0.10/README
mongodb-linux-x86_64-4.0.10/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-4.0.10/MPL-2
mongodb-linux-x86_64-4.0.10/LICENSE-Community.txt
mongodb-linux-x86_64-4.0.10/bin/mongodump
mongodb-linux-x86_64-4.0.10/bin/mongorestore
mongodb-linux-x86_64-4.0.10/bin/mongoexport
mongodb-linux-x86_64-4.0.10/bin/mongoimport
mongodb-linux-x86_64-4.0.10/bin/mongostat
mongodb-linux-x86_64-4.0.10/bin/mongotop
mongodb-linux-x86_64-4.0.10/bin/bsondump
mongodb-linux-x86_64-4.0.10/bin/mongofiles
mongodb-linux-x86_64-4.0.10/bin/mongoreplay
mongodb-linux-x86_64-4.0.10/bin/mongod
mongodb-linux-x86_64-4.0.10/bin/mongos
mongodb-linux-x86_64-4.0.10/bin/mongo
mongodb-linux-x86_64-4.0.10/bin/install_compass

创建文件夹

#创建文件夹
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/conf
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/mongos/log
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/config/data
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/config/log
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard1/data
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard1/log
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard2/data
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard2/log
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard3/data
[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard3/log

目录结构

#按顺序启动

#按顺序启动
mongod -f /usr/mongodb/conf/shard1.conf &
mongod -f /usr/mongodb/conf/shard2.conf &
mongod -f /usr/mongodb/conf/shard3.conf &

#启动的结果

[root@jiba01 conf]# about to fork child process, waiting until server is ready for connections.
forked process: 1466
child process started successfully, parent exiting

shard集群配置

#连接集群 创建shard1,此处可以随意登录3个机器中的一个,但是需要保证port是shard1的port,此处用的是23001(shard1-23001 shard2-23002 shard3-23003)

mongo --host 10.10.10.100 --port 23001 admin

#配置复制集 shard1,注意ip和端口

config = {_id: 'shard1', members: [{_id: 0, host: '10.10.10.100:23001'},{_id: 1, host: '10.10.10.101:23001'},{_id: 2, host: '10.10.10.102:23001',"arbiterOnly":true}]}  

#返回信息

{"_id" : "shard1","members" : [{"_id" : 0,"host" : "10.10.10.100:23001"},{"_id" : 1,"host" : "10.10.10.101:23001"},{"_id" : 2,"host" : "10.10.10.102:23001","arbiterOnly" : true}]
}

#初始化副本集配置

#返回信息 "ok" : 1 是成功,其他的需要看报错进行处理

#返回信息 "ok" : 1 是成功,其他的需要看报错进行处理
{"ok" : 1,"operationTime" : Timestamp(1562623465, 1),"$clusterTime" : {"clusterTime" : Timestamp(1562623465, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
shard1:SECONDARY>

#配置复制集 shard2

#退出重新登录

mongo --host 10.10.10.100 --port 23002 admin
config = {_id: 'shard2', members: [{_id: 0, host: '10.10.10.100:23002'},{_id: 1, host: '10.10.10.101:23002'},{_id: 2, host: '10.10.10.102:23002',"arbiterOnly":true}]}
rs.initiate(config);
#返回结果一样,不再展示

#配置复制集 shard3

#退出重新登录

mongo --host 10.10.10.100 --port 23003 admin
config = {_id: 'shard3', members: [{_id: 0, host: '10.10.10.100:23003'},{_id: 1, host: '10.10.10.101:23003'},{_id: 2, host: '10.10.10.102:23003',"arbiterOnly":true}]}
rs.initiate(config);

#shard集群全部配置完毕

config集群配置

#启动各个节点

3个机器 ###每个机器启动一个###

mongod -f /usr/mongodb/conf/config.conf

#然后登录其中一个节点 创建config集群,注意端口号

mongo --host 10.10.10.100 --port 22000 admin 

#创建config集群

注意 集群的名字 _id需要和配置文件中相同   configReplSet

config = {_id: 'configReplSet', members: [{_id: 0, host: '10.10.10.100:22000'},{_id: 1, host: '10.10.10.101:22000'},{_id: 2, host: '10.10.10.102:22000'}]}
rs.initiate(config);

#config创建完毕

mongos节点配置

#启动mongos

在每个机器上 都启动mongos 此处是mongos命令

mongos -f /usr/mongodb/conf/mongos.conf

#登录到mongos

mongo --host 10.10.10.100 --port 21000 admin

#添加 分片集群

db.runCommand( { addshard : "shard1/10.10.10.100:23001,10.10.10.101:23001,10.10.10.102:23001",name:"shard1"} );
db.runCommand( { addshard : "shard2/10.10.10.100:23002,10.10.10.101:23002,10.10.10.102:23002",name:"shard2"} );
db.runCommand( { addshard : "shard3/10.10.10.100:23003,10.10.10.101:23003,10.10.10.102:23003",name:"shard3"} );

#查看分片状态

#10.10.10.102上的全是仲裁节点,所以不会显示

db.runCommand( { listshards : 1 } )
#返回结果
mongos> db.runCommand( { listshards : 1 } )
{"shards" : [{"_id" : "shard1","host" : "shard1/10.10.10.100:23001,10.10.10.101:23001","state" : 1},{"_id" : "shard2","host" : "shard2/10.10.10.100:23002,10.10.10.101:23002","state" : 1},{"_id" : "shard3","host" : "shard3/10.10.10.100:23003,10.10.10.101:23003","state" : 1}],"ok" : 1,"operationTime" : Timestamp(1562628088, 1),"$clusterTime" : {"clusterTime" : Timestamp(1562628088, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}

#查看集群状态

#返回结果
--- Sharding Status ---sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("5d23ca90f263273c0ac89699")}shards:{  "_id" : "shard1",  "host" : "shard1/10.10.10.100:23001,10.10.10.101:23001",  "state" : 1 }{  "_id" : "shard2",  "host" : "shard2/10.10.10.100:23002,10.10.10.101:23002",  "state" : 1 }{  "_id" : "shard3",  "host" : "shard3/10.10.10.100:23003,10.10.10.101:23003",  "state" : 1 }active mongoses:"4.0.10" : 3autosplit:Currently enabled: yesbalancer:Currently enabled:  yesCurrently running:  noFailed balancer rounds in last 5 attempts:  0Migration Results for the last 24 hours:No recent migrationsdatabases:{  "_id" : "config",  "primary" : "config",  "partitioned" : true }config.system.sessionsshard key: { "_id" : 1 }unique: falsebalancing: truechunks:shard1  1{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)

######################搭建完毕############################

使用

#创建分片,启动

#登录任意一个mongos节点

mongo --host 10.10.10.100 --port 21000

#newTable是新的数据库名字

#newTable是新的数据库名字
mongos> db.runCommand({enablesharding:'newTable'});
#返回信息
{"ok" : 1,"operationTime" : Timestamp(1562628535, 5),"$clusterTime" : {"clusterTime" : Timestamp(1562628535, 5),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}

#指定数据库里需要分片的集合和片键,用admin登录

mongo --host 10.10.10.100 --port 21000 admin

#newTable的table1表需要分片 根据 id 自动分片到 shard1 shard2 shard3 上面去
#要这样设置是因为不是所有mongodb 的数据库和表 都需要分片

db.runCommand( { shardcollection : "newTable.table1",key : {id: 1} } )

#以后登录可以直接登录 mongos 而不用登录各个分片的节点

开机启动脚本

mongod -f /usr/mongodb/conf/shard1.conf &
mongod -f /usr/mongodb/conf/shard2.conf &
mongod -f /usr/mongodb/conf/shard3.conf &
mongod -f /usr/mongodb/conf/config.conf &
mongos -f /usr/mongodb/conf/mongos.conf &

#登录mongos命令 21000是mongo的端口

mongo --host 10.10.10.100 --port 21000 

#然后选择不同的数据库

use newTable

完毕

文件路径如图

MongoDB 分片集群搭建相关推荐

  1. 实战:MongoDB 分片集群Shard Cluster 搭建(1台路由节点,3台配置节点,9台分片节点)

    MongoDB 分片集群搭建(分片集群安全认证+SpringBoot访问) 一.搭建要求 一台路由节点 IP地址:192.168.80.110 端口:11111 三套复制集(每个分片节点中的复制集 都 ...

  2. MongoDB——MongoDB分片集群(Sharded Cluster)两种搭建方式

    MongoDB分片集群(Sharded Cluster)两种搭建方式 MongoDB分片的概念 分片集群包含的组件 分片集群架构目标 MongoDB分片集群搭建 第一套副本集 第二套副本集 配置节点副 ...

  3. linux系统中MongoDB数据库安装及分片集群搭建

    史上最全的mongodb分片集群搭建,从介绍安装到集群搭建授权,你再也找不到比他更加详细的资料了,未经允许禁止转载!! 一.简介 MongoDB是一个便于开发和扩展设计的文档数据库,属于NoSQL数据 ...

  4. 搭建MongoDB分片集群

    在部门服务器搭建MongoDB分片集群,记录整个操作过程,朋友们也可以参考. 计划如下: 用5台机器搭建,IP分别为:192.168.58.5.192.168.58.6.192.168.58.8.19 ...

  5. DOCKER-COMPOSE搭建MONGODB分片集群(单机版)

    docker-compose搭建mongodb分片集群(单机版) - 丰哥坑到 - 博客园

  6. MongoDb分片集群认证

    本文主要基于已经搭建好的未认证集群,结合上篇Mongodb副本集分片集群模式环境部署(https://www.cnblogs.com/woxingwoxue/p/9875878.html), Mong ...

  7. mongodb 分片集群安装 -- 二进制文件安装

    一.安装前准备 机器分配: 三台机器 A :192.168.19.101 B: 192.168.19.102 C: 192.168.19.103 A机器:mongos .config server.s ...

  8. mongodb 启动_精心总结--mongodb分片集群启动与关闭

    概述 网上教程有点坑啊,很多bug,今天刚好涉及到所以抽空整理了下mongodb分片集群启动与关闭方面的教程.希望对大家有点帮助. 基础环境配置 192.168.240.145 测试Nosql服务器1 ...

  9. 2021-05-12 MongoDB面试题 什么是MongoDB分片集群

    什么是MongoDB分片集群? Sharding cluster是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建.sharding分片很好的解决了单台服务器磁盘空 ...

  10. TiDB和MongoDB分片集群架构比较

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近阅读了TiDB源码的说明文档,跟MongoDB的分片集群做了下简单对比. 首先展示TiDB的整体架构 M ...

最新文章

  1. SQL Server Integration Services 包的开发与部署初探
  2. 软件工程专业如何就业!
  3. http://www.myexception.cn/web/426486.html
  4. 数据结构(六)二叉树的遍历(递归非递归方法)
  5. ADO RecondsetPtr 以及如何实现对表的增加删除数据
  6. [Servletamp;JSP] 从JSP到Servlet
  7. Android TV 开发 (1)
  8. Vector和Arraylist的区别
  9. [文档]CSS中文字体对照表
  10. navicat 12 for mac 中文破解版使用说明
  11. 怎样将图片上文字转换成word文字
  12. 中国移动光猫外接一个路由器的方法
  13. 在Delphi中很精确地控制生成的WORD文档的格式
  14. 剑指Offer面试题:31.两个链表的第一个公共节点
  15. Unity3D 游戏引擎之FBX模型的载入与人物行走动画的播放(十二)
  16. Android开发3年,4个月面试,终于拿到小米、京东、字节大厂offer
  17. 小学英语词汇量测试软件好,Test Your Vocabulary:号称是最准的英语词汇量测试网站...
  18. NCHW vs. NHWC
  19. 如何用cmd命令进入mysql
  20. Cadence Orcad Capture创建多部分Symbol及Homogeneous及Heterogeneous图文教程及视频演示

热门文章

  1. PTA---L2-016 愿天下有情人都是失散多年的兄妹(深度优先)
  2. hse是什么意思的缩写_什么是HSE
  3. 北京折叠——一部刚要开始就已经结束的科幻小说
  4. adf输稿器是什么_送稿器是什么
  5. MMS的基本特征与特点(转)
  6. 11-linux文件操作
  7. KubeEdge入门到精通-KubeEdge v1.3部署指南!
  8. Django创建超级管理员用户步骤
  9. Xilinx FPGA等效门数计算
  10. 【报告分享】抖店百宝书-抖音电商(附下载)