实战:MongoDB 分片集群Shard Cluster 搭建(1台路由节点,3台配置节点,9台分片节点)
MongoDB 分片集群搭建(分片集群安全认证+SpringBoot访问)
一、搭建要求
一台路由节点
IP地址:192.168.80.110
端口:11111
三套复制集(每个分片节点中的复制集 都需要有仲裁节点)
IP地址: 192.168.80.55 , 端口:15555/15556/15557
IP地址: 192.168.80.56 , 端口:15555/15556/15557
IP地址: 192.168.80.57 , 端口:15555/15556/15557
集群都是在一台机器上搭建此集群,使用不同端口进行区分
一套配置集群
IP地址: 192.168.80.128 , 端口:15555/15556/15557(集群都是在一台机器上搭建此集群,使用不同端口进行区分)
二、准备工作
名称 | 地址 |
---|---|
mongodb-linux-x86_64-4.1.3.tgz | 下载地址,提取码:5555 |
2.1 准备5台机器
- ip地址分别为:
- 192.168.80.110 作为路由节点
- 192.168.80.128 作为配置节点(搭建三台节点的复制集集群,一主两从)
- 192.168.80.55 作为分片节点(搭建三台节点的复制集集群,一主两从)
- 192.168.80.56 作为分片节点(搭建三台节点的复制集集群,一主两从)
- 192.168.80.57 作为分片节点(搭建三台节点的复制集集群,一主两从)
注意:集群都是在一台机器上搭建此集群,使用不同端口进行区分
三、搭建分片集群
3.1 配置 并启动config 节点集群
节点名称 | 配置文件 |
---|---|
节点1 :192.168.80.128:155555 | 配置文件:config-15555.conf |
节点2 :192.168.80.128:155556 | 配置文件:config-15556.conf |
节点3 :192.168.80.128:155557 | 配置文件:config-15557.conf |
实现第一台配置节点的搭建:
1.拷贝安装文件,并解压缩:
2.重命名mongodb目录并在根目录下创建配置目录:
3.创建配置文件:config-15555.conf
# 数据库文件位置dbpath=config/config1#日志文件位置logpath=config/logs/config1.log# 以追加方式写入日志logappend=true# 是否以守护进程方式运行fork = truebind_ip=0.0.0.0port = 15555# 表示是一个配置服务器configsvr=true#配置服务器副本集名称replSet=configsvr
4.在配置文件目录config创建日志目录和数据库文件存放目录:
5.启动配置集中的第一台mongodb:
6.同样的操作,设置配置节点集群中的另外两台机器:拷贝配置文件
修改端口:
# 数据库文件位置dbpath=config/config2#日志文件位置logpath=config/logs/config2.log# 以追加方式写入日志logappend=true# 是否以守护进程方式运行fork = truebind_ip=0.0.0.0port = 15556# 表示是一个配置服务器configsvr=true#配置服务器副本集名称replSet=configsvr
# 数据库文件位置dbpath=config/config3#日志文件位置logpath=config/logs/config3.log# 以追加方式写入日志logappend=true# 是否以守护进程方式运行fork = truebind_ip=0.0.0.0port = 15557# 表示是一个配置服务器configsvr=true#配置服务器副本集名称replSet=configsvr
创建数据库文件存放目录:
7.同样的操作,启动配置节点集群中的另外两台机器
进入任意节点的mongo shell 并添加 配置节点集群:
注意:use admin
var cfg ={"_id":"configsvr","members":[{"_id":1,"host":"192.168.80.128:15555"},{"_id":2,"host":"192.168.80.128:15556"},{"_id":3,"host":"192.168.80.128:15557"}]};rs.initiate(cfg)
查看集群状态:15555节点变为primary,其他俩个节点变为secondary
rs.status()
3.2 配置shard集群
shard1集群搭建
节点名称 | 配置文件 |
---|---|
节点1 :192.168.80.55:155555 | 配置文件:config-15555.conf |
节点2 :192.168.80.55:155556 | 配置文件:config-15556.conf |
节点3 :192.168.80.55:155557 | 配置文件:config-15557.conf |
shard2集群搭建
节点名称 | 配置文件 |
---|---|
节点1 :192.168.80.56:155555 | 配置文件:config-15555.conf |
节点2 :192.168.80.56:155556 | 配置文件:config-15556.conf |
节点3 :192.168.80.56:155557 | 配置文件:config-15557.conf |
shard3集群搭建
节点名称 | 配置文件 |
---|---|
节点1 :192.168.80.57:155555 | 配置文件:config-15555.conf |
节点2 :192.168.80.57:155556 | 配置文件:config-15556.conf |
节点3 :192.168.80.57:155557 | 配置文件:config-15557.conf |
配置第一个shard1集群
- 在服务器192.168.80.55上,安装并配置mongodb
每一台的配置详情:
dbpath=shard1/shard1-15555bind_ip=0.0.0.0port=15555fork=truelogpath=shard1/shard1-15555.logreplSet=shard1shardsvr=truedbpath=shard1/shard1-15556bind_ip=0.0.0.0port=15556fork=truelogpath=shard1/shard1-15556.logreplSet=shard1shardsvr=true dbpath=shard1/shard1-15557bind_ip=0.0.0.0port=15557fork=truelogpath=shard1/shard1-15557.logreplSet=shard1shardsvr=true
启动每个mongod
进入其中一个mongodb进行集群配置
var cfg ={"_id":"shard1","protocolVersion" : 1,"members":[{"_id":1,"host":"192.168.80.55:15555"},{"_id":2,"host":"192.168.80.55:15556"},{"_id":3,"host":"192.168.80.55:15557"}]};rs.initiate(cfg)
查看集群状态:
rs.status()
配置shard2和shard3集群
配置方式和配置shard1集群一致
shard2集群搭建15555到15557
dbpath=shard2/shard2-15555/15556/15557bind_ip=0.0.0.0port=15555fork=truelogpath=shard2/shard2-15555.logreplSet=shard2shardsvr=true
shard2集群操作1:创建配置文件,并启动mongodb
var cfg ={"_id":"shard2","protocolVersion" : 1,"members":[{"_id":1,"host":"192.168.80.56:15555"},{"_id":2,"host":"192.168.80.56:15556"},{"_id":3,"host":"192.168.80.56:15557"}]};rs.initiate(cfg)rs.status()
shard2集群操作2:进入其中一个mongodb进行集群配置
shard3集群搭建15555到15557
dbpath=shard3/shard3-15555bind_ip=0.0.0.0port=15555fork=truelogpath=shard3/shard3-15555.logreplSet=shard3shardsvr=true
shard3集群操作1:创建配置文件,并启动mongodb
var cfg ={"_id":"shard3","protocolVersion" : 1,"members":[{"_id":1,"host":"192.168.80.57:15555"},{"_id":2,"host":"192.168.80.57:15556"},{"_id":3,"host":"192.168.80.57:15557"}]};rs.initiate(cfg)rs.status()
shard3集群操作2:进入其中一个mongodb进行集群配置
3.3 配置和启动 路由节点
route路由节点
节点名称 | 配置文件 |
---|---|
节点1 :192.168.80.110:155555 | 配置文件:config-15555.conf |
登陆到路由节点,解压mongodb,并重命名
编辑路由节点配置文件:route-15555.conf
port=15555
bind_ip=0.0.0.0
fork=true
logpath=route/logs/route.log
configdb=configsvr/192.168.80.128:15555,192.168.80.128:15556,192.168.80.128:15557
启动路由节点使用 mongos (注意不是mongod)
./bin/mongos -f route/route-15555.conf
进入 mongo 路由
./bin/mongo --port 15555
查看 mongo 路由状态
3.4 mongos(路由)中添加分片节点shard1,shard2,shard3
添加shard1分片节点
添加shard2和shard3分片节点
查看状态
sh.addShard("shard1/192.168.80.55:15555,192.168.80.55:15556,192.168.80.55:15557");sh.addShard("shard2/192.168.80.56:15555,192.168.80.56:15556,192.168.80.56:15557");
sh.status()sh.addShard("shard3/192.168.80.57:15555,192.168.80.57:15556,192.168.80.57:15557");
sh.status()
3.5 开启数据库和集合分片(指定片键)
继续使用mongos完成分片开启和分片大小设置
为数据库开启分片功能:sh.enableSharding(“dabing_employee”)
为指定集合开启分片功能:sh.shardCollection(“dabing_employee.dabing_employee_datas”,{“片键字段名如 name”:索引说明})
3.6 向集合中插入数据测试
查看各个分片是否同步了集合:
通过路由循环向集合中添加数据500条:
use dabing_employee;
for(var i=1;i<= 500;i++){db.dabing_employee_datas.insert({"name":"test"+i,salary:(Math.random()*20000).toFixed(2)});
}
3.7 验证分片效果
分别进入 shard1 和 shard2 中的数据库 进行验证:名称会按照hash的方式进行散列分布到不同的节点
四、分片集群安全认证
4.1 进入路由创建管理员和普通用户
创建管理员:
创建普通用户:
4.2 关闭所有的配置节点 分片节点 和 路由节点(不建议使用kill -9方式进行结束进程)
注意:
不建议直接使用kill -9 直接结束mongodb推荐使用命令:从客户端进去,使用shutdown命令> use admin;switched to db admin> db.shutdownServer();server should be down...
注意:
keyfile文件权限只能是600
4.3 生成密钥文件 并修改权限
创建data/mongodb目录存放生成的密钥:
生成密钥(便于 配置节点集群和分片节点集群 交互)(拷贝testKeyFile.file到配置节点和分片节点)
openssl rand -base64 756 > data/mongodb/testKeyFile.filechmod 600 testKeyFile.file
4.4 配置节点集群和分片节点集群开启安全认证和指定密钥文件
配置节点和分片节点都需要添加配置:一共12个配置文件
auth=truekeyFile=data/mongodb/testKeyFile.file
配置节点集群
分片节点shard1集群
分片节点shard2集群
分片节点shard3集群
注意:
每一台节点上的密钥文件都需要设置访问权限,否则报错:testKeyFile.file are too open
解决: 12台节点都需要设置,注意:此处得权限需要设置成600 ,过大也会造成上述问题
chmod 600 data/mongodb/testKeyFile.file
4.5 在路由配置文件中 设置密钥文件
keyFile=data/mongodb/testKeyFile.file
4.6 启动所有的配置节点 分片节点 和 路由节点 使用路由进行权限验证
./bin/mongod -f config/config-15555.conf./bin/mongod -f config/config-15556.conf./bin/mongod -f config/config-15557.conf
./bin/mongod -f shard1/shard1-15555.conf./bin/mongod -f shard1/shard1-15556.conf./bin/mongod -f shard1/shard1-15557.conf
./bin/mongod -f shard2/shard2-15555.conf./bin/mongod -f shard2/shard2-15556.conf./bin/mongod -f shard2/shard2-15557.conf
./bin/mongod -f shard/shard2/shard2-47017.conf./bin/mongod -f shard/shard2/shard2-47018.conf./bin/mongod -f shard/shard2/shard2-47019.conf
./bin/mongos -f route/route-27017.conf
4.7 访问路由进行权限验证
使用管理员账户:admin/admin
使用普通用户:hanmeimei/123456
注意:
直接查询分片集群,查询不到数据,因为此时数据不可见,需要对每个分片节点创建用户权限
五、SpringBoot项目实操
5.1 创建springboot工程
5.2 核心类
1. Maven依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.dabing</groupId><artifactId>mongodb_springboot_shards</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><version>2.2.2.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>11</source><target>11</target></configuration></plugin></plugins></build></project>
2. 配置文件 application.properties
spring.data.mongodb.host=192.168.80.110
spring.data.mongodb.port=15555
spring.data.mongodb.database=dabing_employee
spring.data.mongodb.username=hanmeimei
spring.data.mongodb.password=123456
3. 实体类 Employee
package cn.dabing.entity;import org.springframework.data.mongodb.core.mapping.Document;import java.util.Date;@Document(value = "dabing_employee")
public class Employee {private String id;private String name;private String city;private Date birthday;private Double expectSalary;public Employee() {}public Employee(String id, String name, String city, Date birthday, Double expectSalary) {this.id = id;this.name = name;this.city = city;this.birthday = birthday;this.expectSalary = expectSalary;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Double getExpectSalary() {return expectSalary;}public void setExpectSalary(Double expectSalary) {this.expectSalary = expectSalary;}@Overridepublic String toString() {return "Resume{" +"id='" + id + '\'' +", name='" + name + '\'' +", city='" + city + '\'' +", birthday=" + birthday +", expectSalary=" + expectSalary +'}';}}
4. 持久层接口 EmployeeRespository
package cn.dabing.repository;import cn.dabing.entity.Employee;
import org.springframework.data.mongodb.repository.MongoRepository;public interface EmployeeRespository extends MongoRepository<Employee,String>{}
5. 测试类 MongoDBRespositoryMain
package cn.dabing;import cn.dabing.entity.Employee;
import cn.dabing.repository.EmployeeRespository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;import java.util.Date;
import java.util.List;
import java.util.Random;@SpringBootApplication
public class MongoDBRespositoryMain {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(MongoDBRespositoryMain.class, args);EmployeeRespository resumeRepository = context.getBean(EmployeeRespository.class);Random random = new Random();for (int i = 0; i < 1000; i++) {Employee resume = new Employee();resume.setName("test" + (i + 1));resume.setBirthday(new Date());resume.setExpectSalary((i + 1) * random.nextDouble());resume.setCity("beijing");resumeRepository.insert(resume);System.out.println("insert success");}System.out.println("query start");List<Employee> employees = resumeRepository.findAll();for (Employee employee : employees) {System.out.println(employee);}}
}
6. 程序执行结果
控制台执行日志:
7. mongo分片集群数据验证
项目下载地址
云盘地址:springboot 访问mongodb 测试案例 提取码:z5ow
实战:MongoDB 分片集群Shard Cluster 搭建(1台路由节点,3台配置节点,9台分片节点)相关推荐
- MongoDB集群之分片集群 Shard Cluster
1.什么是分片 分片(sharding)是MongoDB用来将大型集合水平分割到不同服务器(或者复制集)上所采用的方法.不需要功能强大的大型计算机就可以存储更多的数据,处理更大的负载. 2.为什么要分 ...
- Redis高可用集群Redis Cluster搭建
前言: Redis3.0版本之前,可以通过Redis Sentinel(哨兵)来实现高可用 ( HA ),从3.0版本之后,官方推出了Redis Cluster,它的主要用途是实现数据分片(Data ...
- 两台服务器安装redis集群_Redis Cluster搭建高可用Redis服务器集群
一.Redis Cluster集群简介 Redis Cluster是Redis官方提供的分布式解决方案,在3.0版本后推出的,有效地解决了Redis分布式的需求,当一个节点挂了可以快速的切换到另一个节 ...
- Docker搭建MongoRocks副本分片集群(Docker Mongodb Rocksdb Replication Sharding)
Docker搭建MongoRocks副本分片集群 准备 依赖 安装 下载镜像 基本单实例 带配置的单实例 权限配置 docker参数解释 启动命令 rocksdb配置解释 查看启动日志 连接测试 ov ...
- 2021-05-12 MongoDB面试题 什么是MongoDB分片集群
什么是MongoDB分片集群? Sharding cluster是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建.sharding分片很好的解决了单台服务器磁盘空 ...
- Docker中搭建redis分片集群,搭建redis哨兵结构,实现springboot中对redis分片集群、哨兵结构的访问,Redis缓存雪崩、缓存击穿处理(非关系型数据库技术课程 第十二周)
文章目录 一.要求: 二.知识总结 缓存雪崩 解决方案 docker中redis分片集群搭建 配置好配置文件 redis-6380.conf redis-6381.conf redis-6382.co ...
- 2.6_11 Redis主从复制、哨兵模式、分片集群
相关链接 Excel目录 redis官网 redis中文网 Redis集群 1) 主从复制replication:一个master,多个slave,要几个slave取决于要求的读吞吐量[10万QPS/ ...
- 阿里面试官:Redis分片集群都不懂?还来面试?
面试官:聊下Redis的分片集群,先聊 Redis Cluster好咯? 面试官:Redis Cluser是Redis 3.x才有的官方集群方案,这块你了解多少? 候选者:嗯,要不还是从基础讲起呗? ...
- MongoDB——MongoDB分片集群(Sharded Cluster)两种搭建方式
MongoDB分片集群(Sharded Cluster)两种搭建方式 MongoDB分片的概念 分片集群包含的组件 分片集群架构目标 MongoDB分片集群搭建 第一套副本集 第二套副本集 配置节点副 ...
最新文章
- 【干货书】图、网络与算法
- JSP中response,session,cookie,application的作用及区别
- vue定义一个全局价格处理函数
- python中dic_python之dic {字典}(重要指数*****)
- 蓝桥杯 ADV-205算法提高 拿糖果(动态规划)
- mysql数据库汉字乱码解决方案
- matlab车流量检测,求解释代码,一段交通车流量检测的代码
- java框架_Java 中几种常用的 RPC 框架介绍
- 高可用Redis(四):列表,集合与有序集合
- 备战数学建模14-熵权法确定指标权重系数
- 微信号复制统计工具源码
- 骨传导式耳机推荐,2021骨传导耳机排行榜
- 番茄助手功能及快捷键
- 中文期刊分类(blog版)
- 互金累计融资近千例 借贷行业融资数量居首位
- 红米note3android5.0,小米红米note3MOSBeta5.0安卓8.1.0来去电归属农历等本地化增强适配...
- TwinCAT3中GetSystemTime()功能块使用
- 我是怎样用这个神器搞定我的4T电影的
- HL7 标准及实现指南 必看的网址
- 【AI 学习】2.机器学习和深度学习综述