【4.13】服务器安装 Docker中安装与配置 MongoDB

简单模式

version: '2'
services:
  mongo-container:
    image: mongo:3.4
    container_name: mongodb
    environment:
        - MONGO_INITDB_ROOT_USERNAME=root
        - MONGO_INITDB_ROOT_PASSWORD=123456
    ports:
      - "27017:27017"
    volumes:
      - "./data/mongo:/data/db"
    command: mongod

单片 一主一从一仲裁

配置文件mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /data/db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:
#  keyFile: "/etc/key.file"

#operationProfiling:

replication:
  replSetName: rs0

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

version: '3.7'
services:
  shard11:
    hostname: shard11
    container_name: shard11
    image: mongo:4.0.3
    restart: always
    networks:
      - mongo_test
    command: mongod -f /data/conf/mongod.conf
    privileged: true
    ports:
      - 19001:27017
    volumes:
      - /etc/localtime:/etc/localtime
      - /home/data/db/testm/shard11:/data/db
      - /home/data/db/testm/mongos/mongod.conf:/data/conf/mongod.conf
    deploy:
      placement:
        constraints:
          - node.hostname == manager
  shard12:
    hostname: shard12
    container_name: shard12
    image: mongo:4.0.3
    restart: always
    command: mongod -f /data/conf/mongod.conf
    privileged: true
    networks:
      - mongo_test
    ports:
      - 19002:27017
    volumes:
      - /etc/localtime:/etc/localtime
      - /home/data/db/testm/shard12:/data/db
      - /home/data/db/testm/mongos/mongod.conf:/data/conf/mongod.conf
    deploy:
      placement:
        constraints:
          - node.hostname == manager
  shard13:
    hostname: shard13
    container_name: shard13
    image: mongo:4.0.3
    restart: always
    command: mongod -f /data/conf/mongod.conf
    privileged: true
    networks:
      - mongo_test
    ports:
      - 19003:27017
    volumes:
      - /etc/localtime:/etc/localtime
      - /home/data/db/testm/shard13:/data/db
      - /home/data/db/testm/mongos/mongod.conf:/data/conf/mongod.conf
    deploy:
      placement:
        constraints:
          - node.hostname == manager

          
networks:
  mongo_test:
    external: true          

启动 容器后进入一个容器内

mongo
rs.initiate( {_id : "rs0",members: [{ _id: 0, host: "shard11:27017",priority:2 },{ _id: 1, host: "shard12:27017",priority:1 },{ _id: 2, host: "shard13:27017", arbiterOnly:true }]})

rs.status()
可以看到stateStr 的值都不一样


image.png

主从模式下只有主 可以写操作
仲裁节点甚至都不会保存数据

点波关注 系统搭建(docker)

你可能感兴趣的:(【4.13】服务器安装 Docker中安装与配置 MongoDB)