docker-compose搭建MongoDB分片集群详细步骤

服务器三台

192.168.1.125
192.168.1.126
192.168.1.127

创建目录

在每台机器上操作此步骤

cd /home/seeyii
mkdir mongoCluster
cd mongoCluster
vi mongod.conf

storage:
  dbPath: /data/db
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  maxIncomingConnections: 10000
replication:
  oplogSizeMB: 10240

# security:
  # keyFile: /data/mongodb/key.file
  # authorization: enabled

vi mongos.conf

storage:
  dbPath: /data/db
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongos.log

net:
  maxIncomingConnections: 10000

# security:
  # keyFile: /data/mongodb/key.file

创建目录

vi first_mkdir.sh

mkdir -p /database/vol/conf/config
mkdir -p /database/vol/conf/db
mkdir -p /database/vol/shard1/config
mkdir -p /database/vol/shard1/db
mkdir -p /database/vol/shard1/backup
mkdir -p /database/vol/shard2/config
mkdir -p /database/vol/shard2/db
mkdir -p /database/vol/shard2/backup
mkdir -p /database/vol/mongos/config
mkdir -p /database/vol/mongos/db

生成认证文件 root 用户 三台共用一个

openssl rand -base64 741 > key.file
chmod 600 key.file
chown 999 key.file
mv key.file /database/vol

将配置文件放到指定位置

vi second_mv.sh

#!/bin/bash
for item in /database/vol/
 do
   for item2 in `ls $item`
    do
     if [ $item2 = 'mongos' ]
     then
        echo $item2
        cp mongos.conf $item$item2/config/mongos.conf
     else
        echo "no"
        cp mongod.conf $item$item2/config/mongod.conf
    fi
    done
done

docker-compose.yaml 容器编排

将文件放在 mongoCluster目录中

启动 docker-compose up -d

添加副本集

配置服务器

连接任意一个节点 mongo --host 192.168.1.125 --port 10021

rs.initiate({
		    _id: "rs-config-server",
		    configsvr: true,
		    members: [
		    	{ _id : 0, host : "192.168.1.125:10021" },
		        { _id : 1, host : "192.168.1.126:10021" },
		        { _id : 2, host : "192.168.1.127:10021" },
		    ]
		});

分片1

连接任意一个节点 mongo --host 192.168.1.125 --port 10031

rs.initiate({
		    _id: "rs-shard1-server",
		    members: [
		    	{ _id : 0, host : "192.168.1.125:10031" },
		        { _id : 1, host : "192.168.1.126:10031" },
		        { _id : 2, host : "192.168.1.127:10031" },
		    ]
		});

分片2

连接任意一个节点 mongo --host 192.168.1.125 --port 10041

rs.initiate({
		    _id: "rs-shard2-server",
		    members: [
		    	{ _id : 0, host : "192.168.1.125:10041" },
		        { _id : 1, host : "192.168.1.126:10041" },
		        { _id : 2, host : "192.168.1.127:10041" },
		    ]
		});

配置mongos

确保mongos服务起来之后,连接到192.168.1.125:10011执行以下命令添加分片服务器信息

sh.addShard("rs-shard1-server/192.168.1.125:10031,192.168.1.126:10031,192.168.1.127:10031")
sh.addShard("rs-shard2-server/192.168.1.125:10041,192.168.1.126:10041,192.168.1.127:10041")

添加用户认证

  • 连接任意的mongos
use admin

db.createUser(
		    {
		        user:"root",
		        pwd:"shiye1805A",
		        roles:[{role:"root",db:"admin"}]
		    }
		)
  • 验证是否创建成功
db.auth('root','shiye1805A')

返回值 1 成功

  • 将配置文件的用户认证全部打开 security:

  • 重启容器 注意: 保证key.file 权限为 999

容器 操作

删除
docker rm -f rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2

停止
docker stop rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2

重启
docker restart rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2

启动
docker start rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2

docker-compose 文件

docker-compose.yaml

version: '2'
services:
  rs_config_server:
    image: mongo:3.4
    command: mongod -f /etc/mongod/mongod.conf --configsvr --replSet "rs-config-server"
    volumes:
      - /database/vol/key.file:/data/mongodb/key.file
      - /database/vol/conf/config:/etc/mongod
      - /database/vol/conf/db:/data/db
    ports:
      - "10021:27019"
    restart:
      always
    container_name:
      rs_config_server
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

  rs_shard_server1:
    image: mongo:3.4
    command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "rs-shard1-server"
    volumes:
      - /database/vol/key.file:/data/mongodb/key.file
      - /database/vol/shard1/config:/etc/mongod
      - /database/vol/shard1/db:/data/db
    ports:
      - "10031:27018"
    restart:
      always
    container_name:
      rs_shard_server1
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

  rs_shard_server2:
    image: mongo:3.4
    command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "rs-shard2-server"
    volumes:
      - /database/vol/key.file:/data/mongodb/key.file
      - /database/vol/shard2/config:/etc/mongod
      - /database/vol/shard2/db:/data/db
    ports:
      - "10041:27018"
    restart:
      always
    container_name:
      rs_shard_server2
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

  rs_mongos_server:
    image: mongo:3.4
    command: mongos -f /etc/mongod/mongos.conf --configdb rs-config-server/192.168.1.125:10021,192.168.1.126:10021,192.168.1.127:10021
    ports:
      - "10011:27017"
    volumes:
      - /database/vol/key.file:/data/mongodb/key.file
      - /database/vol/mongos/config:/etc/mongod
      - /database/vol/mongos/db:/data/db
    restart:
      always
    container_name:
      rs_mongos_server
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

你可能感兴趣的:(MongoDB,docker)