images\cherry_red.png1 Docker的学习-gaogaoTwo
      images\cherry_blue.png1.1 docker常见问题故障
         images\cherry_orange.png1.1.1 docker容器无法远程连接MySQL
      images\cherry_blue.png1.2 Docker基础命令
         images\cherry_orange.png1.2.1 docker logs
         images\cherry_orange.png1.2.2 镜像构建优化
         images\cherry_orange.png1.2.3 network
         images\cherry_orange.png1.2.4 内部安装应用命令
         images\cherry_orange.png1.2.5 docker ps
      images\cherry_blue.png1.3 Docker网络模式
      images\cherry_blue.png1.4 docker compose
         images\cherry_orange.png1.4.1 docker-compose安装
      images\cherry_blue.png1.5 部署Tomcat 运行环境
      images\cherry_blue.png1.6 docker(compose) LNMP环境的部署
      images\cherry_blue.png1.7 docker 搭建部署Redis集群
      images\cherry_blue.png1.8 docker 搭建部署MongoDB集群
         images\cherry_orange.png1.8.1 MongoDB(configsvr)配置服务器
         images\cherry_orange.png1.8.2 MongoDB(shared) 分片初始化操作
         images\cherry_orange.png1.8.3 配置MongoDB(Mongos)路由服务器
         images\cherry_orange.png1.8.4 MongoDB测试集群可用性
      images\cherry_blue.png1.9 docker 搭建部署MySQL主从复用
      images\cherry_blue.png1.10 docker+Keepalived+Nginx高可用
      images\cherry_blue.png1.11 docker+Ansible多节点集群管理
      images\cherry_blue.png1.12 docker(compose)+Zabbix 多节点监控服务
      images\cherry_blue.png1.13 docker 部署文件目录下载
      images\cherry_blue.png1.14 docker
         images\cherry_orange.png1.14.1 docker镜像管理
         images\cherry_orange.png1.14.2 docker容器
         images\cherry_orange.png1.14.3 docker容器高级操作

#创建配置文件、数据目录 3台运行主机都需要创建

mkdir /data/mongod/shard1/{log,db} -p
mkdir /data/mongod/shard2/{log,db} -p
mkdir /data/mongod/shard3/{log,db} -p
chmod -R 777 /data/mongod/shard1
chmod -R 777 /data/mongod/shard2
chmod -R 777 /data/mongod/shard3
mkdir -p /data/mongod/conf/{shard1,shard2,shard3}


注意配置文件需要一一对应服务
shared1 --→ rs_shardsvr1 shared2 ------->rs_shardsvr2 shared3 ------->rs_shardsvr3
也就是说在三个服务器中对应的 /data/mongod/conf/{shard1,shard2,shard3} 对应的副本集初始化需要一致性

# vim /data/mongod/conf/{shard1,shard2,shard3}/mongod.conf (这里需要在每一个节点 shard1 shard2 shard3)创建mongod.conf配置文件

shard1 分片1
172.17.140.41:9005 主节点
172.17.140.42:9005 从节点
172.17.140.43:9005 仲裁节点


修改服务器中 172.17.140.41,172.17.140.42,172.17.140.43 服务器中 添加配置文件
# vim /data/mongod/conf/shard1/mongod.conf
replication:
replSetName
: rs_shardsvr1 #分片的服务名
storage:
  dbPath: /home/mongod/db 
  journal:
    enabled: true
  directoryPerDB: true

systemLog:
  destination: file
  logAppend: true
  path: /home/mongod/log/mongod.log

net:
  bindIpAll: true

setParameter:
  enableLocalhostAuthBypass: false

replication:
  replSetName: rs_shardsvr1  
 
sharding: 
   clusterRole: shardsvr     



修改服务器中 172.17.140.41,172.17.140.42,172.17.140.43 服务器中 添加配置文件
# vim /data/mongod/conf/shard2/mongod.conf

storage:
  dbPath: /home/mongod/db 
  journal:
    enabled: true
  directoryPerDB: true

systemLog:
  destination: file
  logAppend: true
  path: /home/mongod/log/mongod.log

net:
  bindIpAll: true

setParameter:
  enableLocalhostAuthBypass: false

replication:
  replSetName: rs_shardsvr2
 
sharding: 
   clusterRole: shardsvr


修改服务器中 172.17.140.41,172.17.140.42,172.17.140.43 服务器中 添加配置文件
# vim /data/mongod/conf/shard3/mongod.conf

storage:
  dbPath: /home/mongod/db
  journal:
    enabled: true
  directoryPerDB: true

systemLog:
  destination: file
  logAppend: true
  path: /home/mongod/log/mongod.log

net:
  bindIpAll: true

setParameter:
  enableLocalhostAuthBypass: false

replication:
  replSetName: rs_shardsvr3
 
sharding: 
   clusterRole: shardsvr




1,
# 三台主机(172.17.140.41、42、43)执行如下启动命令
初始化分片服务器shard1 (一主一从一仲裁)
# 登录进容器中,尽量在主节点执行,否则会报错
shard1 分片1
172.17.140.41:9005 主节点
172.17.140.42:9005 从节点
172.17.140.43:9005 仲裁节点

docker run --name shardsvr1 -d -p 9005:27018 \
  --entrypoint "mongod" \
  -v /data/mongod/shard1:/home/mongod \
  -v /data/mongod/conf/keyfile/:/data/keyfile/ \
  -v /data/mongod/conf/shard1/:/data/conf/ \
  mongo:4.0.21 -f /data/conf/mongod.conf


images\8-1.png

docker exec -it shardsvr1 bash


mongo --host 172.17.140.41 --port 9005

use admin

rs.initiate(
    {
        _id : "rs_shardsvr1",
        members: [
            { _id : 0, host : "172.17.140.41:9005",priority:},
            { _id : 1, host : "172.17.140.42:9005",priority:},
            { _id : 2, host : "172.17.140.43:9005",arbiterOnly:true}
        ]
    }
)


images\8-2.png


#rs.status() 查看状态

2,
初始化分片服务器shard2 (一主一从一仲裁)
# 三台主机(172.17.140.41、42、43)执行如下启动命令
# 登录进容器中,尽量在主节点执行,否则会报错
shard2 分片2
172.17.140.41:9006 仲裁节点
172.17.140.42:9006 主节点
172.17.140.43:9006 从节点

docker run --name shardsvr2 -d -p 9006:27018 \
  --entrypoint "mongod" \
  -v /data/mongod/shard2:/home/mongod \
  -v /data/mongod/conf/keyfile/:/data/keyfile/ \
  -v /data/mongod/conf/shard2/:/data/conf/ \
  mongo:4.0.21 -f /data/conf/mongod.conf


#docker exec -it shardsvr2 bash

mongo --host 172.17.140.42 --port 9006
use admin
rs.initiate(
    {
        _id : "rs_shardsvr2",
        members: [
            { _id : 0, host : "172.17.140.41:9006",arbiterOnly:true },
            { _id : 1, host : "172.17.140.42:9006",priority:},
            { _id : 2, host : "172.17.140.43:9006",priority:}
        ]
    }
)

images\8-3.png

3,
初始化分片服务器shard3 (一主一从一仲裁)
# 三台主机(172.17.140.41、42、43)执行如下启动命令
# 登录进容器中,尽量在主节点执行,否则会报错
shard3 分片3
172.17.140.41:9007 从节点
172.17.140.42:9007 仲裁节点
172.17.140.43:9007 主节点
docker run --name shardsvr3 -d -p 9007:27018 \
  --entrypoint "mongod" \
  -v /data/mongod/shard3:/home/mongod \
  -v /data/mongod/conf/keyfile/:/data/keyfile/ \
  -v /data/mongod/conf/shard3/:/data/conf/ \
  mongo:4.0.21 -f /data/conf/mongod.conf


#docker exec -it shardsvr3 bash

mongo --host 172.17.140.43 --port 9007   
use admin
rs.initiate(
    {
        _id : "rs_shardsvr3",
        members: [
            { _id : 0, host : "172.17.140.41:9007",priority:},
            { _id : 1, host : "172.17.140.42:9007",arbiterOnly:true },
            { _id : 2, host : "172.17.140.43:9007",priority:}
        ]
    }
)

images\8-4.png


#rs.status() 查看状态