w

wnwk

V1

2022/10/10阅读:39主题:默认主题

5分钟搭建mongodb分片集群

软件版本

  • vagrant 2.3.0
  • virtualBox 6.1.38
  • docker 20.10.18
  • centos7
  • mongodb 4.4.17-focal

集群环境 共三台centos7的虚拟机

虚拟机名称 ip 作用
mongo1 192.168.1.100 分片1端口 27111,分片2端口27112,配置服务端口27019,路由服务
mongo2 192.168.1.101 分片1端口 27111,分片2端口27112,配置服务端口27019
mongo3 192.168.1.102 分片1端口 27111,分片2端口27112,配置服务端口27019

通过vagrant创建三台虚拟机 Vagrantfile 如下


# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2"do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.
  #config.ssh.username = "vagrant"
  
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  #config.vm.network "public_network"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  #config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.define "mongo1" do |mongo1|

    mongo1.vm.provider "virtualbox" do |vb|
      # Display the VirtualBox GUI when booting the machine
      vb.gui = false
      vb.name= "mongo1"
      # Customize the amount of memory on the VM:
      vb.memory = 2048
      vb.cpus = 1
    end
    mongo1.vm.network "public_network", ip: "192.168.1.100"

  end

  config.vm.define "mongo2" do |mongo2|

    mongo2.vm.provider "virtualbox" do |vb|
      # Display the VirtualBox GUI when booting the machine
      vb.gui = false
      vb.name= "mongo2"
      # Customize the amount of memory on the VM:
      vb.memory = 2048
      vb.cpus = 1
    end
    mongo2.vm.network "public_network", ip: "192.168.1.101"

  end

  config.vm.define "mongo3" do |mongo3|
    mongo3.vm.provider "virtualbox" do |vb|
      # Display the VirtualBox GUI when booting the machine
      vb.gui = false
      vb.name= "mongo3"
      # Customize the amount of memory on the VM:
      vb.memory = 2048
      vb.cpus = 1
    end
    mongo3.vm.network "public_network", ip: "192.168.1.102"

  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "install" ,type"shell", inline: <<-SHELL
    yum -y install psmisc
  SHELL

  config.vm.provision "config"type"shell", inline: <<-SHELL    
    yum -y install net-tools
  SHELL

  config.vm.provision "repare"type"shell", inline: <<-SHELL    
    mkdir -p /etc/docker
    cd /etc/docker
    echo  "{
      \\"
registry-mirrors\\": [\\"配置docker加速器\\"],
      \\"exec-opts\\": [\\"native.cgroupdriver=systemd\\"]
    }" >>  daemon.json 
  SHELL

  config.vm.provision "
docker", type: "shell", inline: <<-SHELL    
    yum install -y yum-utils
    yum-config-manager --add-repo  https://download.docker.com/linux/centos/docker-ce.repo
    yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    
    systemctl daemon-reload
    systemctl enable docker
    systemctl start docker
    docker pull mongo:4.4.17-focal
  SHELL
end


分别执行如下命令,启动虚拟机,通过ssh登录

vagrant up 
vagrant ssh mongo1
vagrant ssh mongo2
vagrant ssh mongo3

创建分片1集群

  • 分别登录到三台虚拟机,执行如下命令
sudo docker run -d --name mongShare1 -p 27111:27111  mongo:4.4.17-focal --shardsvr --replSet s1 --bind_ip 0.0.0.0 --port 27111
 
  • 在任意一台机器上执行如下命令
sudo docker exec -it mongShare1 mongo --port 27111

进入mongodb后,执行如下命令

rs.initiate(
  {
    _id : "s1",
    members: [
      { _id: 0, host: "192.168.1.100:27111" },
      { _id: 1, host: "192.168.1.101:27111" },
      { _id: 2, host: "192.168.1.102:27111" }
    ]
  }
)

创建分片2集群

命令参考分片1集群,修改对应的端口号为 27112,副本集改为s2

创建配置服务

  • 分别登录到三台虚拟机,执行如下命令
sudo docker run -d --name mongConfig -p 27019:27019  mongo:4.4.17-focal --configsvr --replSet config --bind_ip 0.0.0.0   
  • 在任意一台机器上执行如下命令
sudo docker exec -it mongConfig mongo --port 27019

进入mongodb后,执行如下命令

rs.initiate(
  {
    _id : "config",
    configsvr: true,
    members: [
      { _id: 0, host: "192.168.1.100:27019" },
      { _id: 1, host: "192.168.1.101:27019" },
      { _id: 2, host: "192.168.1.102:27019" }
    ]
  }
)

创建路由服务

  • 登录到mongo1,执行如下命令
sudo docker run -d --name mongRoute1 -p 27017:27017  mongo:4.4.17-focal mongos --configdb config/192.168.1.100:27019,192.168.1.101:27019,192.168.1.102:27019 --bind_ip 0.0.0.0
  • 在mongo1机器上执行如下命令
sudo docker exec -it mongRoute1 mongo
  • 配置分片
sh.addShard( "s1/192.168.1.100:27111,192.168.1.101:27111,192.168.1.102:27111")
sh.addShard( "s2/192.168.1.100:27112,192.168.1.101:27112,192.168.1.102:27112")

分片测试

hash分片,按照商品名称hash分片

 #登录路由服务,创建shops数据库,
 sudo docker exec -it mongRoute1 mongo
 # 初始化分片库 shops
 sh.enableSharding("shops")
 # 商品表按照商品名称分片
 sh.shardCollection("shops.goods",{"name":"hashed"})
 # 插入数据到 goods表中
  for(var i=1;i<=1000;i++) {db.goods.insert({_id:i+"",name:"test"+i})}
 # 分别登录到分片1 和分片2 
  sudo docker exec -it mongShare1 mongo --port 27111
  sudo docker exec -it mongShare2 mongo --port 27112
 # 查看插入数据数 ,返现1000条数据被插入到了2个分片中
  db.goods.count()
  

范围分片,按照价格范围进行分片

# 用户表按照年龄分片
 sh.shardCollection("shops.users",{age:1})
# 年龄按照3 为界限来分片
db.adminCommand(
   {
     split: "shops.users",  middle : { age : 3 }
   }
)
# 创建测试数据
 for(var i=1;i<=1000;i++) {db.users.save({age:NumberInt(i%120)})}
# 登录到分片查看,可以看到一个分片中的数据都是年龄小于3的,另一个分片的数据都是年龄大于等于3 的
IT杂谈公众号
IT杂谈公众号

分类:

后端

标签:

后端

作者介绍

w
wnwk
V1