侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MongoDB分片集群部署详解

数据库  /  管理员 发布于 5年前   276

 一、环境说明

1、我们prod环境MongoDB的集群架构是做的分片集群的部署,但是目前我们没有分片,即所有数据都在一个分片上,后期如果数量大,需要分配,集群随时可以分片,对业务方透明

2、各个角色的部署情况

角色 IP 端口 复制集名称
mongos 172.21.244.101,172.21.244.102,172.21.244.94 27000 无
config server 172.21.244.101,172.21.244.102,172.21.244.94 27100 repl_configsvr
存储节点(shard) 172.21.244.101,172.21.244.102,172.21.244.94 27101 shard1

3、MongoDB版本

mongos> db.version()4.0.4-62-g7e345a7

二、基础信息准备

0、系统优化

echo "never" >/sys/kernel/mm/transparent_hugepage/enabledecho "never" >/sys/kernel/mm/transparent_hugepage/defrag

1、下载MongoDB二进制文件

cd /chj/appwget ops.chehejia.com:9090/pkg/chj_mongodb_4.0.4.tar.gztar -zxvf chj_mongodb_4.0.4.tar.gz

2、相关目录建立

#建立base目录mkdir /chj/data/mongodb/chj_db#把MongoDB二进制文件移动到base目录下的bin文件夹mv chj_mongodb_4.0.4/bin /chj/data/mongodb/chj_db/bin#建立认证文件目录mkdir /chj/data/mongodb/chj_db/auth#建立配置文件目录mkdir /chj/data/mongodb/chj_db/conf#建立config server的data和日志目录mkdir /chj/data/mongodb/chj_db/config/data -pmkdir /chj/data/mongodb/chj_db/config/log#建立mongos的日志目录mkdir /chj/data/mongodb/chj_db/mongos/log -p#建立数据节点data和日志目录 mkdir /chj/data/mongodb/chj_db/shard1/data -pmkdir /chj/data/mongodb/chj_db/shard1/log

3、相关配置文件编写

A、mongos的配置文件编写

vim /chj/data/mongodb/chj_db/conf/mongos.confsystemLog: destination: file logAppend: true path: /chj/data/mongodb/chj_db/mongos/log/mongos.logprocessManagement: fork: true # fork and run in background pidFilePath: /chj/data/mongodb/chj_db/mongos/log/mongos.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfonet: port: 27000 bindIpAll: true maxIncomingConnections: 1000 unixDomainSocket:  enabled: true  pathPrefix: /chj/data/mongodb/chj_db/mongos/log  filePermissions: 0700security: keyFile: /chj/data/mongodb/chj_db/auth/keyfile.key# authorization: enabled#replication:sharding: configDB: repl_configsvr/172.21.244.101:27100,172.21.244.102:27100,172.21.244.94:27100

B、config server的配置文件编写

vim /chj/data/mongodb/chj_db/conf/config.confsystemLog: destination: file logAppend: true path: /chj/data/mongodb/chj_db/config/log/congigsrv.logstorage: dbPath: /chj/data/mongodb/chj_db/config/data journal:  enabled: true wiredTiger:  engineConfig:   directoryForIndexes: trueprocessManagement: fork: true # fork and run in background pidFilePath: /chj/data/mongodb/chj_db/config/log/configsrv.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfonet: port: 27100 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. #bindIpAll: true maxIncomingConnections: 1000 unixDomainSocket:  enabled: true  pathPrefix: /chj/data/mongodb/chj_db/config/data  filePermissions: 0700security: keyFile: /chj/data/mongodb/chj_db/auth/keyfile.key authorization: enabledreplication: replSetName: repl_configsvrsharding: clusterRole: configsvr

C、存储节点的配置文件编写

vim /chj/data/mongodb/chj_db/conf/shard1.confsystemLog: destination: file logAppend: true path: /chj/data/mongodb/chj_db/shard1/log/shard1.logstorage: dbPath: /chj/data/mongodb/chj_db/shard1/data journal:  enabled: true wiredTiger:  engineConfig:   directoryForIndexes: trueprocessManagement: fork: true # fork and run in background pidFilePath: /chj/data/mongodb/chj_db/shard1/log/shard1.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfonet: port: 27101 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. #bindIpAll: true maxIncomingConnections: 1000 unixDomainSocket:  enabled: true  pathPrefix: /chj/data/mongodb/chj_db/shard1/data  filePermissions: 0700security: keyFile: /chj/data/mongodb/chj_db/auth/keyfile.key authorization: enabledreplication: replSetName: shard1sharding: clusterRole: shardsvr

4、生产key认证文件

echo "chj123456" >/chj/data/mongodb/chj_db/auth/keyfile.key#设置文件的权限为400,不然服务无法启动chmod 400 /chj/data/mongodb/chj_db/auth/keyfile.key

三、集群初始化

1、启动 config server 服务

/chj/data/mongodb/chj_db/bin/mongod -f /chj/data/mongodb/chj_db/conf/config.conf

2、初始化config server集群

#登录其中一个config server节点/chj/data/mongodb/chj_db/bin/mongo --port 27100#配置集群config = { _id:"repl_configsvr",members:[ {_id:0,host:"172.21.244.101:27100"}, {_id:1,host:"172.21.244.102:27100"}, {_id:2,host:"172.21.244.94:27100"}] }#初始化集群rs.initiate(config)PS:结果输出如下,说明集群初始化成功,可以通过rs.status()命令查看集群状态{    "ok" : 1,    "$gleStats" : {        "lastOpTime" : Timestamp(1557538260, 1),        "electionId" : ObjectId("000000000000000000000000")    },    "lastCommittedOpTime" : Timestamp(0, 0)}

3、启动存储节点服务

/chj/data/mongodb/chj_db/bin/mongod -f /chj/data/mongodb/chj_db/conf/shard1.conf

4、初始化存储集群

#登录你希望是主节点的服务器/chj/data/mongodb/chj_db/bin/mongo --port 27101#配置集群config = { _id:"shard1",members:[ {_id:0,host:"172.21.244.101:27101"}, {_id:1,host:"172.21.244.102:27101"},{_id:2,host:"172.21.244.94:27101",arbiterOnly:true}] }#初始化集群rs.initiate(config)PS:结果输出如下,说明集群初始化成功,可以通过rs.status()命令查看集群状态{ "ok" : 1 }

5、添加存储集群的管理账号

登录主节点

/chj/data/mongodb/chj_db/bin/mongo --port 27101use admindb.createUser({user: "root",pwd: "123456",roles: [ { role: "root", db: "admin" } ]})

6、启动mongos 服务

/chj/data/mongodb/chj_db/bin/mongos -f /chj/data/mongodb/chj_db/conf/mongos.conf

7、添加config server的管理账号

登录任意一个mongos节点

/chj/data/mongodb/chj_db/bin/mongo --port 27000use admindb.createUser({user: "root",pwd: "123456",roles: [ { role: "root", db: "admin" } ]})

8、把存储节点添加到mongos

登录任意一个mongos节点(如果是在上一步的窗口,需要退出重新登录)

/chj/data/mongodb/chj_db/bin/mongo --port 27000use admindb.auth('root','123456')#添加分片sh.addShard('shard1/172.21.244.101:27101,172.21.244.102:27101,172.21.244.94:27101')#查看分片状态sh.status()

四、交付业务方

1、建立应用账号

登录任意一个mongos节点/chj/data/mongodb/chj_db/bin/mongo --port 27000use admindb.auth('root','123456')#切到业务数据库use chj_db#建立读写账号db.createUser({  user: "chj_db_rw",  pwd: "123456",  roles: [   { role: "readWrite", db: "chj_db" },   { role: "dbOwner", db: "chj_db" }  ]})#建立只读账号(根据业务需求确认是否需要)db.createUser({user: "chj_db_r",pwd: "123456",roles: [ { role: "read", db: "chj_db" } ]})

2、交付开发人员信息

连接地址:172.21.244.101:27000,172.21.244.102:27000,172.21.244.94:27000库名:chj_db账号:chj_db_rw密码:123456

五、数据库启用分片

如果后期业务量大,需要开启分片,配置如下

#指定需要分片的数据库mongos> sh.enableSharding("chj_db") {    "ok" : 1,    "operationTime" : Timestamp(1557546835, 3),    "$clusterTime" : {        "clusterTime" : Timestamp(1557546835, 3),        "signature" : {"hash" : BinData(0,"bkrrr8Kxrr9j9udrDc/hURHld38="),"keyId" : NumberLong("6689575940508352541")        }    }}#在chj_db数据库和users集合中创建了name和age为升序的片键mongos> sh.shardCollection("chj_db.users",{name:1,age:1}) {    "collectionsharded" : "chj_db.users",    "collectionUUID" : UUID("59c0b99f-efff-4132-b489-f6c7e3d98f42"),    "ok" : 1,    "operationTime" : Timestamp(1557546861, 12),    "$clusterTime" : {        "clusterTime" : Timestamp(1557546861, 12),        "signature" : {"hash" : BinData(0,"UBB1A/YODnmXwG5eAhgNLcKVzug="),"keyId" : NumberLong("6689575940508352541")        }    }}#查看分片情况mongos> sh.status() --- Sharding Status --- sharding version: {    "_id" : 1,    "minCompatibleVersion" : 5,    "currentVersion" : 6,    "clusterId" : ObjectId("5cd625e0da695346d740f749") } shards:    { "_id" : "shard1", "host" : "shard1/172.21.244.101:27101,172.21.244.102:27101", "state" : 1 } active mongoses:    "4.0.4-62-g7e345a7" : 3 autosplit:    Currently enabled: yes balancer:    Currently enabled: yes    Currently running: no    Failed balancer rounds in last 5 attempts: 0    Migration Results for the last 24 hours:        No recent migrations databases:    { "_id" : "chj_db", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("82088bc7-7b98-4033-843d-7058d8d959f6"), "lastMod" : 1 } }        chj_db.usersshard key: { "name" : 1, "age" : 1 }unique: falsebalancing: truechunks:    shard1 1{ "name" : { "$minKey" : 1 }, "age" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 }, "age" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)    { "_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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    大数据环境下mongoDB为何要加索引浅析
    下一条:
    MongoDB实现自动备份的全过程记录
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客