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 = true
bind_ip=0.0.0.0
port = 15555
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
4.在配置文件目录config创建日志目录和数据库文件存放目录:

5.启动配置集中的第一台mongodb:

6.同样的操作,设置配置节点集群中的另外两台机器:拷贝配置文件

修改端口:

# 数据库文件位置
dbpath=config/config2
#日志文件位置
logpath=config/logs/config2.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 15556
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr

# 数据库文件位置
dbpath=config/config3
#日志文件位置
logpath=config/logs/config3.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 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-15555
bind_ip=0.0.0.0
port=15555
fork=true
logpath=shard1/shard1-15555.log
replSet=shard1
shardsvr=true
dbpath=shard1/shard1-15556
bind_ip=0.0.0.0
port=15556
fork=true
logpath=shard1/shard1-15556.log
replSet=shard1
shardsvr=true
dbpath=shard1/shard1-15557
bind_ip=0.0.0.0
port=15557
fork=true
logpath=shard1/shard1-15557.log
replSet=shard1
shardsvr=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/15557
bind_ip=0.0.0.0
port=15555
fork=true
logpath=shard2/shard2-15555.log
replSet=shard2
shardsvr=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-15555
bind_ip=0.0.0.0
port=15555
fork=true
logpath=shard3/shard3-15555.log
replSet=shard3
shardsvr=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.file chmod 600 testKeyFile.file

4.4 配置节点集群和分片节点集群开启安全认证和指定密钥文件
配置节点和分片节点都需要添加配置:一共12个配置文件
auth=true
keyFile=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;
}
@Override
public 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分片集群,包括配置并启动config节点集群、shard集群及路由节点,同时涵盖安全认证配置及SpringBoot项目的实战操作。

1780





