MongoDB分片集群搭建
环境:
Centos 7.5 1804
MongoDB 4.0.1shard分片主机:shard1: IP:192.168.1.1shard2: IP:192.168.1.2shard2: IP:192.168.1.3#三台主机分别启动三个mongod实例:mongod1: 端口: 27017mongod2: 端口: 27018mongod2: 端口: 27019configsrv主机:IP:192.168.1.4mongod1: 端口: 27019mongod2: 端口: 37018mongod2: 端口: 47019Route主机:192.168.1.5mongods: 端口: 27017
一、准备工作
- 在所有节点安装mongodb-4 并创建相关文件夹
cat << EOF > /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.0]
name=MongoDB 4.0 Repository
baseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/\$releasever/mongodb-org/4.0/\$basearch/
gpgcheck=0
enabled=1
EOFyum install -y mongodb-orgmkdir -p /var/run/mongodb
mkdir -p /data/mongod{1..3}
mkdir -p /etc/mongo
mkdir -p /tmp/mongod{1..3}chown -R mongod.mongod /data
chown -R mongod.mongod /var/run/mongodb
chown -R mongod.mongod /tmp/mongod{1..3}
- 生成key并复制至所有主机
#在192.168.1.1主机执行openssl rand -base64 756 > /etc/mongo/mongo.key
chown -R mongod.mongod /etc/mongo
chmod -R 600 /etc/mongoscp -r /etc/mongo 192.168.1.2:/etc/
scp -r /etc/mongo 192.168.1.3:/etc/
scp -r /etc/mongo 192.168.1.4:/etc/
scp -r /etc/mongo 192.168.1.5:/etc/
二、配置configsvr
- 在configsvr主机(IP:192.168.1.4)操作
- 生成三个configsvr的配置文件:
#configsvr1的配置文件cat << EOF > /etc/mongo/configsvc1.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod1.logstorage:dbPath: /data/mongod1journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: trueprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod1.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27019#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod1filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: BigBoss
sharding:clusterRole: configsvrEOF#configsvr2的配置文件cat << EOF > /etc/mongo/configsvc2.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod2.logstorage:dbPath: /data/mongod2journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true
processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod2.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfo
net:port: 37019#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod2filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: BigBoss
sharding:clusterRole: configsvrEOF#configsvr3的配置文件cat << EOF > /etc/mongo/configsvc3.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod3.logstorage:dbPath: /data/mongod3journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: trueprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod3.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 47019#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod3filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: BigBoss
sharding:clusterRole: configsvr
EOF
- 启动mongod:
mongod -f /etc/mongo/configsvc1.conf
mongod -f /etc/mongo/configsvc2.conf
mongod -f /etc/mongo/configsvc3.conf
- 初始化configsrv副本集群:
mongo --port 27019rs.initiate(
{_id: "BigBoss",version: 1,protocolVersion: 1,writeConcernMajorityJournalDefault: true,configsvr: true,members: [{_id: 0,host: "192.168.1.4:27019",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 66,tags: {BigBoss: "YES"},slaveDelay: 0,votes: 1},{_id: 1,host: "192.168.1.4:37019",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 55,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1},{_id: 2,host: "192.168.1.4:47019",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 33,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1}],settings: {chainingAllowed : true,}
}
)#查看副本集状态
rs.status()
三、配置shard1副本集:
- 在shard1主机(IP:192.168.1.1)操作
- 生成三个mongod的配置文件:
#mongod1.conf配置文件:cat << EOF > /etc/mongo/mongod1.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod1.logstorage:dbPath: /data/mongod1journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod1.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27017#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod1filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard1
sharding:clusterRole: shardsvrEOF#mongod2.conf配置文件:cat << EOF > /etc/mongo/mongod2.confsystemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod2.logstorage:dbPath: /data/mongod2journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod2.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27018#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod2filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard1sharding:clusterRole: shardsvr
EOF#mongod3.conf配置文件:cat << EOF > /etc/mongo/mongod3.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod3.logstorage:dbPath: /data/mongod3journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod3.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27019#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod3filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard1sharding:clusterRole: shardsvrEOF
- 启动mongod:
mongod -f /etc/mongo/mongod1.conf
mongod -f /etc/mongo/mongod2.conf
mongod -f /etc/mongo/mongod3.conf
- 初始化shard1副本集
mongors.initiate(
{_id: "shard1",version: 1,protocolVersion: 1,writeConcernMajorityJournalDefault: true,members: [{_id: 0,host: "192.168.1.1:27017",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 66,tags: {BigBoss: "YES"},slaveDelay: 0,votes: 1},{_id: 1,host: "192.168.1.1:27018",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 55,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1},{_id: 2,host: "192.168.1.1:27019",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 33,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1}],settings: {chainingAllowed : true,}
}
)#查看副本集状态
rs.status()
四、配置shard2副本集:
- 在shard2主机(IP:192.168.1.2)操作
- 生成三个mongod的配置文件:
#mongod1.conf配置文件:
cat << EOF > /etc/mongo/mongod1.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod1.logstorage:dbPath: /data/mongod1journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod1.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27017#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod1filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard2
sharding:clusterRole: shardsvrEOF#mongod2.conf配置文件:cat << EOF > /etc/mongo/mongod2.confsystemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod2.logstorage:dbPath: /data/mongod2journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod2.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27018#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod2filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard2sharding:clusterRole: shardsvr
EOF#mongod3.conf配置文件:cat << EOF > /etc/mongo/mongod3.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod3.logstorage:dbPath: /data/mongod3journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod3.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27019#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod3filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard2sharding:clusterRole: shardsvrEOF
- 启动mongod:
mongod -f /etc/mongo/mongod1.conf
mongod -f /etc/mongo/mongod2.conf
mongod -f /etc/mongo/mongod3.conf
- 初始化shard2副本集
mongors.initiate(
{_id: "shard2",version: 1,protocolVersion: 1,writeConcernMajorityJournalDefault: true,members: [{_id: 0,host: "192.168.1.2:27017",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 66,tags: {BigBoss: "YES"},slaveDelay: 0,votes: 1},{_id: 1,host: "192.168.1.2:27018",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 55,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1},{_id: 2,host: "192.168.1.2:27019",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 33,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1}],settings: {chainingAllowed : true,}
}
)#查看shard2副本集状态rs.status()
五、配置shard1副本集
- 在shard1主机(IP:192.168.1.1)操作
- 生成三个mongod的配置文件:
#mongod1.conf配置文件:
cat << EOF > /etc/mongo/mongod1.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod1.logstorage:dbPath: /data/mongod1journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod1.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27017#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod1filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard3
sharding:clusterRole: shardsvrEOF#mongod2.conf配置文件:cat << EOF > /etc/mongo/mongod2.confsystemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod2.logstorage:dbPath: /data/mongod2journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod2.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27018#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod2filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard3sharding:clusterRole: shardsvr
EOF#mongod3.conf配置文件:cat << EOF > /etc/mongo/mongod3.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod3.logstorage:dbPath: /data/mongod3journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: true processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod3.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27019#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: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod3filePermissions: 0700security:keyFile: /etc/mongo/mongo.keyauthorization: enabledreplication:replSetName: shard3sharding:clusterRole: shardsvrEOF
- 启动mongod:
mongod -f /etc/mongo/mongod1.conf
mongod -f /etc/mongo/mongod2.conf
mongod -f /etc/mongo/mongod3.conf
- 初始化shard3副本集
mongors.initiate(
{_id: "shard3",version: 1,protocolVersion: 1,writeConcernMajorityJournalDefault: true,members: [{_id: 0,host: "192.168.1.3:27017",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 66,tags: {BigBoss: "YES"},slaveDelay: 0,votes: 1},{_id: 1,host: "192.168.1.3:27018",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 55,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1},{_id: 2,host: "192.168.1.3:27019",arbiterOnly: false,buildIndexes: true,hidden: false,priority: 33,tags: {BigBoss: "NO"},slaveDelay: 0,votes: 1}],settings: {chainingAllowed : true,}
}
)#查看shard3副本集状态rs.status()
六、配置Route
- 创建mongos配置文件:
#route是无状态的,在任何一台主机启动都行,只要能够连接至configsrv即可cat << EOF > /etc/mongo/route.conf
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.logprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:bindIpAll: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmpfilePermissions: 0700security:keyFile: /etc/mongo/mongo.key
# authorization: enabled#replication:sharding:configDB: BigBoss/192.168.1.4:27019,192.168.1.4:37019,192.168.1.4:47019
EOF
- 启动mongos并设置一个连接的账号密码
#启动
mongos -f /etc/mongo/route.conf#连接
mongo#设置管理员账号密码
use admindb.createUser(
{user: "root",pwd: "123456",roles: [ { role: "__system", db: "admin" } ]}
)exit
- 重连至mongodb
mongo -uroot -p123456 --authenticationDatabase admin#添加分片主机至集群中
sh.addShard("shard1/192.168.1.1:27017,192.168.1.1:27018,192.168.1.1:27019")
sh.addShard("shard2/192.168.1.2:27017,192.168.1.2:27018,192.168.1.2:27019")
sh.addShard("shard3/192.168.1.3:27017,192.168.1.3:27018,192.168.1.3:27019")#查看状态
sh.status()####为了展示出效果,修改一下默认的chunksize大小,这里修改为1M
#默认的chunksize大小为64M,示例修改命令如下:
#use config
#db.settings.save( { _id:"chunksize", value: <sizeInMB> } )use config
db.settings.save( { _id:"chunksize", value: 1 } )#为test数据库开启分片
#选择一个片键age并指定一个集合mycoll对其进行分片sh.enableSharding("test")
sh.shardCollection("test.mycoll", {"age": 1})#测试分片,写入数据到数据库中use test
for (i = 1; i <= 10000; i++) db.mycoll.insert({age:(i%100), name:"bigboss_user"+i, address:i+", Some Road, Zhengzhou, Henan", country:"China", course:"cousre"+"(i%12)"})#写入完成之后就可以查看分片信息了sh.status()
转载于:https://blog.51cto.com/bigboss/2160311
MongoDB分片集群搭建相关推荐
- 实战:MongoDB 分片集群Shard Cluster 搭建(1台路由节点,3台配置节点,9台分片节点)
MongoDB 分片集群搭建(分片集群安全认证+SpringBoot访问) 一.搭建要求 一台路由节点 IP地址:192.168.80.110 端口:11111 三套复制集(每个分片节点中的复制集 都 ...
- MongoDB——MongoDB分片集群(Sharded Cluster)两种搭建方式
MongoDB分片集群(Sharded Cluster)两种搭建方式 MongoDB分片的概念 分片集群包含的组件 分片集群架构目标 MongoDB分片集群搭建 第一套副本集 第二套副本集 配置节点副 ...
- linux系统中MongoDB数据库安装及分片集群搭建
史上最全的mongodb分片集群搭建,从介绍安装到集群搭建授权,你再也找不到比他更加详细的资料了,未经允许禁止转载!! 一.简介 MongoDB是一个便于开发和扩展设计的文档数据库,属于NoSQL数据 ...
- 搭建MongoDB分片集群
在部门服务器搭建MongoDB分片集群,记录整个操作过程,朋友们也可以参考. 计划如下: 用5台机器搭建,IP分别为:192.168.58.5.192.168.58.6.192.168.58.8.19 ...
- DOCKER-COMPOSE搭建MONGODB分片集群(单机版)
docker-compose搭建mongodb分片集群(单机版) - 丰哥坑到 - 博客园
- MongoDb分片集群认证
本文主要基于已经搭建好的未认证集群,结合上篇Mongodb副本集分片集群模式环境部署(https://www.cnblogs.com/woxingwoxue/p/9875878.html), Mong ...
- mongodb 分片集群安装 -- 二进制文件安装
一.安装前准备 机器分配: 三台机器 A :192.168.19.101 B: 192.168.19.102 C: 192.168.19.103 A机器:mongos .config server.s ...
- mongodb 启动_精心总结--mongodb分片集群启动与关闭
概述 网上教程有点坑啊,很多bug,今天刚好涉及到所以抽空整理了下mongodb分片集群启动与关闭方面的教程.希望对大家有点帮助. 基础环境配置 192.168.240.145 测试Nosql服务器1 ...
- 2021-05-12 MongoDB面试题 什么是MongoDB分片集群
什么是MongoDB分片集群? Sharding cluster是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建.sharding分片很好的解决了单台服务器磁盘空 ...
- TiDB和MongoDB分片集群架构比较
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近阅读了TiDB源码的说明文档,跟MongoDB的分片集群做了下简单对比. 首先展示TiDB的整体架构 M ...
最新文章
- 关于安卓版的eclipse连接数据库并誓言增删改查
- windows不能查询组策略对象列表 解决方案
- 最快的ASP无组件上传类(4M只需10秒)0.96版
- 细品java封装继承多态
- 两张趣图助你理解状态码的含义~
- guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...
- mysql解决select * from 表名 (where + 约束条件为空)
- 使 JavaScript 更加简洁的小技巧
- C语言实现左旋字符串
- copyWebpackPlugin的使用及常见问题(glob及Path ............... is not in cwd)
- STM32F107 资料
- Google预训练语言模型T5
- linux中文输入配置sougou输入法
- android room 简书,Android Room 的坑
- ChatGPT 可以联网了!浏览器插件下载
- unity粗体字+android,[Unity] UGUI加粗字体小记
- Python:统计文件中所有英文单词的数量
- python数据项目分析实战技法_《Python数据分析与机器学习实战-唐宇迪》读书笔记第9章--随机森林项目实战——气温预测(1/2)...
- C++day06 继承的关系
- sql 语句left join关联超过2张表时,随着left join一路向左,永远只能是最左边的关联表与新表关联吗?还是母表可以一直on下去?
热门文章
- 【AI视野·今日CV 计算机视觉论文速览 第228期】Tue, 29 Jun 2021
- Java多线程系列--“JUC线程池”03之 线程池原理(二)
- round()函数的使用方法
- 【期末复习】现代管理科学基础
- python日期判断星座_Python学习笔记 - 根据出生日期判断你的星座
- 4线电阻屏linux驱动,TQ2440四线电阻式触摸屏驱动程序的分析
- HSB”、lab、CMYK、RGB有什么区别
- python 爬取12306验证码
- 如何设置代理服务器?
- asp dsn mysql 连接失败_Asp透过系统DSN链接mysql数据库