MongoDB的分片集群搭建

发布时间:2022-08-19 11:32

架构图

MongoDB的分片集群搭建_第1张图片

环境准备

  1. 下载:mongodb-macos-x86_64-4.2.8.tgz

  2. 解压、改名:shard_cluster

tar -zxvf mongodb-macos-x86_64-4.2.8.tgz
mv mongodb-macos-x86_64-4.2.8 shard_cluster
  1. 建立config节点文件夹
mkdir -p config/{config1,config2,config3,logs}
  1. 建立shard节点文件夹
mkdir -p shard/{shard1/{shard1-37011,shard1-37013,shard1-37015,shard1-37017,logs},shard2/{shard2-47011,shard2-47013,shard2-47015,shard2-47017,logs},shard3/{shard3-57011,shard3-57013,shard3-57015,shard3-57017,logs},shard4/{shard4-58011,shard4-58013,shard4-58015,shard4-58017,logs}}
  1. 建立route节点文件夹
mkdir -p route/logs
  1. 建立密钥文件夹
mkdir -p data/mongodb

config节点部署

  1. config/config-17011.conf
# 数据库文件位置
dbpath=config/config1
#日志文件位置
logpath=config/logs/config1.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17011
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
  1. config/config-17013.conf
# 数据库文件位置
dbpath=config/config2
#日志文件位置
logpath=config/logs/config2.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17013
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
  1. config/config-17015.conf
# 数据库文件位置
dbpath=config/config3
#日志文件位置
logpath=config/logs/config3.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17015
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
  1. 启动配置节点
./bin/mongod -f config/config-17011.conf
./bin/mongod -f config/config-17013.conf
./bin/mongod -f config/config-17015.conf
  1. 进入任意config节点的mongo shell,添加config节点集群,注意:user admin
./bin/mongo --port 17011

use admin

var cfg = {"_id":"configsvr", 
  "members":[ 
    {"_id":1, "host":"localhost:17011"}, 
    {"_id":2, "host":"localhost:17013"}, 
    {"_id":3, "host":"localhost:17015"}
  ] 
};

rs.initiate(cfg)

shard1节点集群部署

  1. shard/shard1/shard1-37011.conf
dbpath=shard/shard1/shard1-37011
bind_ip=0.0.0.0
port=37011
fork=true
logpath=shard/shard1/logs/shard1-37011.log
replSet=shard1
shardsvr=true
  1. shard/shard1/shard1-37013.conf
dbpath=shard/shard1/shard1-37013
bind_ip=0.0.0.0
port=37013
fork=true
logpath=shard/shard1/logs/shard1-37013.log
replSet=shard1
shardsvr=true
  1. shard/shard1/shard1-37015.conf
dbpath=shard/shard1/shard1-37015
bind_ip=0.0.0.0
port=37015
fork=true
logpath=shard/shard1/logs/shard1-37015.log
replSet=shard1
shardsvr=true
  1. shard/shard1/shard1-37017.conf
dbpath=shard/shard1/shard1-37017
bind_ip=0.0.0.0
port=37017
fork=true
logpath=shard/shard1/logs/shard1-37017.log
replSet=shard1
shardsvr=true
  1. 启动shard1节点
./bin/mongod -f shard/shard1/shard1-37011.conf
./bin/mongod -f shard/shard1/shard1-37013.conf
./bin/mongod -f shard/shard1/shard1-37015.conf
./bin/mongod -f shard/shard1/shard1-37017.conf
  1. 进入任意shard1节点的mongo shell,添加shard1节点集群
./bin/mongo --port 37011

var cfg = {"_id":"shard1", 
  "protocolVersion" : 1, 
  "members":[ 
    {"_id":1, "host":"localhost:37011"}, 
    {"_id":2, "host":"localhost:37013"}, 
    {"_id":3, "host":"localhost:37015"}, 
    {"_id":4, "host":"localhost:37017", "arbiterOnly":true}
  ] 
};

rs.initiate(cfg) 

rs.status()

shard2节点集群部署

  1. shard/shard2/shard2-47011.conf
dbpath=shard/shard2/shard2-47011
bind_ip=0.0.0.0
port=47011
fork=true
logpath=shard/shard2/logs/shard2-47011.log
replSet=shard2
shardsvr=true
  1. shard/shard2/shard2-47013.conf
dbpath=shard/shard2/shard2-47013
bind_ip=0.0.0.0
port=47013
fork=true
logpath=shard/shard2/logs/shard2-47013.log
replSet=shard2
shardsvr=true
  1. shard/shard2/shard2-47015.conf
dbpath=shard/shard2/shard2-47015
bind_ip=0.0.0.0
port=47015
fork=true
logpath=shard/shard2/logs/shard2-47015.log
replSet=shard2
shardsvr=true
  1. shard/shard2/shard2-47017.conf
dbpath=shard/shard2/shard2-47017
bind_ip=0.0.0.0
port=47017
fork=true
logpath=shard/shard2/logs/shard2-47017.log
replSet=shard2
shardsvr=true
  1. 启动shard2节点
./bin/mongod -f shard/shard2/shard2-47011.conf
./bin/mongod -f shard/shard2/shard2-47013.conf
./bin/mongod -f shard/shard2/shard2-47015.conf
./bin/mongod -f shard/shard2/shard2-47017.conf
  1. 进入任意shard2节点的mongo shell,添加shard2节点集群
./bin/mongo --port 47011

var cfg = {"_id":"shard2", 
  "protocolVersion" : 1, 
  "members":[ 
    {"_id":1, "host":"localhost:47011"}, 
    {"_id":2, "host":"localhost:47013"}, 
    {"_id":3, "host":"localhost:47015"}, 
    {"_id":4, "host":"localhost:47017", "arbiterOnly":true}
  ] 
};

rs.initiate(cfg) 

rs.status()

shard3节点集群部署

  1. shard/shard3/shard3-57011.conf
dbpath=shard/shard3/shard3-57011
bind_ip=0.0.0.0
port=57011
fork=true
logpath=shard/shard3/logs/shard3-57011.log
replSet=shard3
shardsvr=true
  1. ashard/shard3/shard3-57013.conf
dbpath=shard/shard3/shard3-57013
bind_ip=0.0.0.0
port=57013
fork=true
logpath=shard/shard3/logs/shard3-57013.log
replSet=shard3
shardsvr=true
  1. shard/shard3/shard3-57015.conf
dbpath=shard/shard3/shard3-57015
bind_ip=0.0.0.0
port=57015
fork=true
logpath=shard/shard3/logs/shard3-57015.log
replSet=shard3
shardsvr=true
  1. shard/shard3/shard3-57017.conf
dbpath=shard/shard3/shard3-57017
bind_ip=0.0.0.0
port=57017
fork=true
logpath=shard/shard3/logs/shard3-57017.log
replSet=shard3
shardsvr=true
  1. 启动shard3节点
./bin/mongod -f shard/shard3/shard3-57011.conf
./bin/mongod -f shard/shard3/shard3-57013.conf
./bin/mongod -f shard/shard3/shard3-57015.conf
./bin/mongod -f shard/shard3/shard3-57017.conf
  1. 进入任意shard3节点的mongo shell,添加shard3节点集群
./bin/mongo --port 57011

var cfg = {"_id":"shard3", 
  "protocolVersion" : 1, 
  "members":[ 
    {"_id":1, "host":"localhost:57011"}, 
    {"_id":2, "host":"localhost:57013"}, 
    {"_id":3, "host":"localhost:57015"}, 
    {"_id":4, "host":"localhost:57017", "arbiterOnly":true}
  ] 
};

rs.initiate(cfg) 

rs.status()

shard4节点集群部署

  1. shard/shard4/shard4-58011.conf
dbpath=shard/shard4/shard4-58011
bind_ip=0.0.0.0
port=58011
fork=true
logpath=shard/shard4/logs/shard4-58011.log
replSet=shard4
shardsvr=true
  1. shard/shard4/shard4-58013.conf
dbpath=shard/shard4/shard4-58013
bind_ip=0.0.0.0
port=58013
fork=true
logpath=shard/shard4/logs/shard4-58013.log
replSet=shard4
shardsvr=true
  1. shard/shard4/shard4-58015.conf
dbpath=shard/shard4/shard4-58015
bind_ip=0.0.0.0
port=58015
fork=true
logpath=shard/shard4/logs/shard4-58015.log
replSet=shard4
shardsvr=true
  1. shard/shard4/shard4-58017.conf
dbpath=shard/shard4/shard4-58017
bind_ip=0.0.0.0
port=58017
fork=true
logpath=shard/shard4/logs/shard4-58017.log
replSet=shard4
shardsvr=true
  1. 启动shard4节点
./bin/mongod -f shard/shard4/shard4-58011.conf
./bin/mongod -f shard/shard4/shard4-58013.conf
./bin/mongod -f shard/shard4/shard4-58015.conf
./bin/mongod -f shard/shard4/shard4-58017.conf
  1. 进入任意shard4节点的mongo shell,添加shard4节点集群
./bin/mongo --port 58011

var cfg = {"_id":"shard4", 
  "protocolVersion" : 1, 
  "members":[ 
    {"_id":1, "host":"localhost:58011"}, 
    {"_id":2, "host":"localhost:58013"}, 
    {"_id":3, "host":"localhost:58015"}, 
    {"_id":4, "host":"localhost:58017", "arbiterOnly":true}
  ] 
};

rs.initiate(cfg) 

rs.status()

route节点部署

  1. route/route-27017.conf
port=27017
bind_ip=0.0.0.0
fork=true
logpath=route/logs/route.log
configdb=configsvr/localhost:17011,localhost:17013,localhost:17015
  1. 启动route节点
./bin/mongos -f route/route-27017.conf
  1. mongos中添加分片节点
./bin/mongo --port 27017

sh.status()

sh.addShard("shard1/localhost:37011,localhost:37013,localhost:37015,localhost:37017");

sh.addShard("shard2/localhost:47011,localhost:47013,localhost:47015,localhost:47017");

sh.addShard("shard3/localhost:57011,localhost:57013,localhost:57015,localhost:57017");

sh.addShard("shard4/localhost:58011,localhost:58013,localhost:58015,localhost:58017");

sh.status()
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5f1ef531f4a85c5270098b90")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/localhost:37011,localhost:37013,localhost:37015",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/localhost:47011,localhost:47013,localhost:47015",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/localhost:57011,localhost:57013,localhost:57015",  "state" : 1 }
        {  "_id" : "shard4",  "host" : "shard4/localhost:58011,localhost:58013,localhost:58015",  "state" : 1 }
  active mongoses:
        "4.2.8" : 1
  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" : "config",  "primary" : "config",  "partitioned" : true }
  1. 开启数据库和集合分片,指定片键(验证分片效果使用)
sh.enableSharding("lx_resume")

sh.shardCollection("lx_resume.lx_resume_datas",{"name":"hashed"})

增加权限控制

  1. 进入route节点
./bin/mongo --port 27017

use lx_resume

db.createUser( 
  { 
    user: "lx", 
    pwd: "abc321", 
    roles: [ 
    	{ role: "readWrite", db: "lx_resume" } 
    ] 
  } 
)

Successfully added user: {
        "user" : "lx",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "lx_resume"
                }
        ]
}
  1. 关闭所有的config节点、分片节点、route节点
killall mongod

killall mongos

killall mongo
  1. 生成密钥文件,并修改权限(也可以用509证书)
openssl rand -base64 756 > data/mongodb/testKeyFile.file

chmod 600 data/mongodb/testKeyFile.file
  1. config节点集群和shard节点集群开启安全认证和指定密钥文件
auth=true
keyFile=data/mongodb/testKeyFile.file
  1. 在route配置文件中,设置密钥文件
keyFile=data/mongodb/testKeyFile.file
  1. 编写shell启动config节点、shard节点、route节点

    start.sh

./bin/mongod -f config/config-17011.conf
./bin/mongod -f config/config-17013.conf
./bin/mongod -f config/config-17015.conf
./bin/mongod -f shard/shard1/shard1-37011.conf
./bin/mongod -f shard/shard1/shard1-37013.conf
./bin/mongod -f shard/shard1/shard1-37015.conf
./bin/mongod -f shard/shard1/shard1-37017.conf
./bin/mongod -f shard/shard2/shard2-47011.conf
./bin/mongod -f shard/shard2/shard2-47013.conf
./bin/mongod -f shard/shard2/shard2-47015.conf
./bin/mongod -f shard/shard2/shard2-47017.conf
./bin/mongod -f shard/shard3/shard3-57011.conf
./bin/mongod -f shard/shard3/shard3-57013.conf
./bin/mongod -f shard/shard3/shard3-57015.conf
./bin/mongod -f shard/shard3/shard3-57017.conf
./bin/mongod -f shard/shard4/shard4-58011.conf
./bin/mongod -f shard/shard4/shard4-58013.conf
./bin/mongod -f shard/shard4/shard4-58015.conf
./bin/mongod -f shard/shard4/shard4-58017.conf
./bin/mongos -f route/route-27017.conf
  1. 使用route节点进行安全认证测试
./bin/mongo --port 27017

use lx_resume

db.auth("lx","abc321")

SpringBoot访问MongoDB

  1. pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-mongodbartifactId>
        <version>2.2.2.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <version>2.2.2.RELEASEversion>
        <scope>testscope>
    dependency>
dependencies>
  1. application.properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=lx_resume
spring.data.mongodb.username=lx
spring.data.mongodb.password=abc321
  1. 建立pojo
package com.lx.pojo;

import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;

@Document("lx_resume_datas")
public class Resume {

    private String id;
    private String name;
    private String city;
    private Date birthday;
    private Double expectSalary;

    public Resume() {
    }

    public Resume(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;
    }

    @Override
    public String toString() {
        return "Resume{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", city='" + city + '\'' +
                ", birthday=" + birthday +
                ", 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;
    }
}

  1. 建立dao
package com.lx.dao;

import com.lx.pojo.Resume;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface IResumeDao extends MongoRepository<Resume, String> {
}

  1. 建立启动类
package com.lx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MongoRepositoryMain {

    public static void main(String[] args) {
        SpringApplication.run(MongoRepositoryMain.class, args);
    }
}

  1. 建立测试类
package com.lx;

import com.lx.dao.IResumeDao;
import com.lx.pojo.Resume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoRepositoryMain.class)
public class TestRepository {

    @Autowired
    private IResumeDao resumeDao;

    @Test
    public void testAdd() throws ParseException {
        Resume resume = new Resume();
        resume.setName("test2");
        resume.setCity("BJ");
        resume.setExpectSalary(18000d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse("2002-08-11 11:13:14");
        resume.setBirthday(date);
        resumeDao.save(resume);
    }

    @Test
    public void testQuery() {
        List<Resume> list = resumeDao.findAll();
        list.forEach(resume -> {
            System.out.println(resume);
        });
    }
}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号