千家信息网

Docker搭建MongoDB 4.0副本集

发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,环境:系统版本:CentOS 7.5 内核:4.18.7-1.el7.elrepo.x86_64Docker-ce: 18.06MongoDB: 4.0.3宿主机IP:192.168.1.1Mongo
千家信息网最后更新 2025年01月21日Docker搭建MongoDB 4.0副本集

环境:

系统版本:CentOS 7.5 内核:4.18.7-1.el7.elrepo.x86_64Docker-ce: 18.06MongoDB: 4.0.3宿主机IP:192.168.1.1MongoDB1端口:30001MongoDB2端口:30002MongoDB3端口:30003

一、安装docker、docker-compose

wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoyum install -y docker-ce python-pipmkdir /etc/docker/cat << EOF > /etc/docker/daemon.json{   "registry-mirrors": ["https://registry.docker-cn.com"],    "live-restore": true,    "default-shm-size": "128M",    "max-concurrent-downloads": 10,    "oom-score-adjust": -1000,    "debug": false}   EOFpip install docker-composesystemctl enable dockersystemctl start docker

二、创建副本集所需的key

#创建一个volume,将创建的key文件复制至volume中#400权限是要保证安全性,否则mongod启动会报错#999用户是容器中的mongod用户cd && mkdir mongod && cd mongodopenssl rand -base64 756 > mongo.keydocker volume create mongod_mongo_keyMONGO_KEY_DIR=`docker volume inspect mongod_mongo_key | grep Mount | sed -r 's#.*"(.*)",$#\1#'`cp mongo.key $MONGO_KEY_DIRchmod 400 $MONGO_KEY_DIR/mongo.keychown 999.999 $MONGO_KEY_DIR/mongo.keymkdir /data/mongo/mongdb{1..3} -pv

三、创建docker-compose文件

#映射/data/mongo/mongdb{1,2,3}目录到容器里,将数据持久化到磁盘#映射出三个端口,以便外部用户连接#MONGO_INITDB_ROOT_USERNAME:管理员用户的账号#MONGO_INITDB_ROOT_PASSWORD:管理员用户的密码cat << EOF > docker-compose.yamlversion: "3.7"services:  mongodb1:    image: mongo:4.0.3    container_name: mongodb1    networks:      - mongodb    ports:      - "30001:27017"    environment:      - MONGO_INITDB_ROOT_USERNAME=root      - MONGO_INITDB_ROOT_PASSWORD=123456x    volumes:      - /data/mongo/mongdb1:/data/db      - mongo_key:/mongo    command: ["mongod","--replSet","BigBoss","--keyFile","/mongo/mongo.key","--directoryperdb"]  mongodb2:    image: mongo:4.0.3    container_name: mongodb2    networks:      - mongodb    ports:      - "30002:27017"    environment:      - MONGO_INITDB_ROOT_USERNAME=root      - MONGO_INITDB_ROOT_PASSWORD=123456    volumes:      - /data/mongo/mongdb2:/data/db      - mongo_key:/mongo    command: ["mongod","--replSet","BigBoss","--keyFile","/mongo/mongo.key","--directoryperdb"]  mongodb3:    image: mongo:4.0.3    container_name: mongodb3    networks:      - mongodb    ports:      - "30003:27017"    environment:      - MONGO_INITDB_ROOT_USERNAME=root      - MONGO_INITDB_ROOT_PASSWORD=123456    volumes:      - /data/mongo/mongdb3:/data/db      - mongo_key:/mongo    command: ["mongod","--replSet","BigBoss","--keyFile","/mongo/mongo.key","--directoryperdb"]networks:  mongodb:    driver: bridge    name: mongodbvolumes:  mongo_key:EOF

四、启动副本集

#副本集群成员的IP地址必须要让客户的也能解析这个IP,不然使用集群方式连接数据库时,无法连接!!!#若是在阿里云ECS上搭建副本集,想要让公网的客户端能够连接,必须要填ECS的公网IP!!!#启动三个容器,并使他们后台运行docker pull mongo:4.0.3docker-compose up -d#连接30001端口上的容器,开始配置集群mongo -port 30001 -uroot -p123456 --authenticationDatabase admin#三节点,其中一个为投票节点,并隐藏#注意host的IP地址,一定要让客户端也能连接所有的地址#_id的值为--replSet参数后的字符串rs.initiate({  _id: "BigBoss",  version: 1,  protocolVersion: 1,  writeConcernMajorityJournalDefault: true,  members: [    {      _id: 0,      host: "192.168.1.1:30001",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 66,      tags: {        BigBoss: "YES"      },      slaveDelay: 0,      votes: 1    },    {      _id: 1,      host: "192.168.1.1:30002",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 55,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    },    {      _id: 2,      host: "192.168.1.1:30003",      arbiterOnly: true,      buildIndexes: true,      hidden: true,      priority: 0,      tags: {      },      slaveDelay: 0,      votes: 1    }  ],  settings: {    chainingAllowed : true,  }})####################################################[root@master mongod]#mongo -port 30001 -uroot -p123456 --authenticationDatabase adminMongoDB shell version v4.0.3connecting to: mongodb://127.0.0.1:30001/Implicit session: session { "id" : UUID("a196085a-a142-450f-9eab-a0fc5a58c43b") }MongoDB server version: 4.0.3Server has startup warnings:2018-10-09T10:15:20.474+0000 I CONTROL  [initandlisten]2018-10-09T10:15:20.474+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2018-10-09T10:15:20.474+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2018-10-09T10:15:20.474+0000 I CONTROL  [initandlisten]---Enable MongoDB's free cloud-based monitoring service, which will then receive and displaymetrics about your deployment (disk utilization, CPU, operation statistics, etc).The monitoring data will be available on a MongoDB website with a unique URL accessible to youand anyone you share the URL with. MongoDB may use this information to make productimprovements and to suggest MongoDB products and deployment options to you.To enable free monitoring, run the following command: db.enableFreeMonitoring()To permanently disable this reminder, run the following command: db.disableFreeMonitoring()--->>> rs.initiate(... {...   _id: "BigBoss",...   version: 1,...   protocolVersion: 1,...   writeConcernMajorityJournalDefault: true,...   members: [...     {...       _id: 0,...       host: "192.168.1.1:30001",...       arbiterOnly: false,...       buildIndexes: true,...       hidden: false,...       priority: 66,...       tags: {...     BigBoss: "YES"...       },...       slaveDelay: 0,...       votes: 1...     },...     {...       _id: 1,...       host: "192.168.1.1:30002",...       arbiterOnly: false,...       buildIndexes: true,...       hidden: false,...       priority: 55,...       tags: {...     BigBoss: "NO"...       },...       slaveDelay: 0,...       votes: 1...     },...     {...       _id: 2,...       host: "192.168.1.1:30003",...       arbiterOnly: true,...       buildIndexes: true,...       hidden: true,...       priority: 0,...       tags: {...       },...       slaveDelay: 0,...       votes: 1...     }...   ],...   settings: {...     chainingAllowed : true,...   }... }... ){ "ok" : 1 }BigBoss:SECONDARY>BigBoss:SECONDARY>BigBoss:SECONDARY>BigBoss:SECONDARY>BigBoss:PRIMARY>BigBoss:PRIMARY>#现在30001端口的容器已经是PRIMARY了

五、测试

#停止PRIMARY的容器docker ps -adocker stop mongodb1#######################################################################################[root@master mongod]#docker ps -aCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES62f42e2aee19        mongo:4.0.3         "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        0.0.0.0:30003->27017/tcp   mongodb3735b8d8a2bcf        mongo:4.0.3         "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        0.0.0.0:30001->27017/tcp   mongodb127ebcebde77e        mongo:4.0.3         "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes       0.0.0.0:30002->27017/tcp   mongodb2[root@master mongod]#docker stop mongodb1mongodb1[root@master mongod]#docker ps -aCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                      NAMES62f42e2aee19        mongo:4.0.3         "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes               0.0.0.0:30003->27017/tcp   mongodb3735b8d8a2bcf        mongo:4.0.3         "docker-entrypoint.s…"   6 minutes ago       Exited (0) 2 seconds ago                              mongodb127ebcebde77e        mongo:4.0.3         "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes              0.0.0.0:30002->27017/tcp   mongodb2[root@master mongod]##连接端口为30002的容器mongo -port 30002 -uroot -p123456 --authenticationDatabase admin#查看集群状态rs.status()#######################################################################################[root@master mongod]#mongo -port 30002 -uroot -p123456 --authenticationDatabase adminMongoDB shell version v4.0.3connecting to: mongodb://127.0.0.1:30002/Implicit session: session { "id" : UUID("7a83559d-84e3-4c89-93dd-1947d40c4837") }MongoDB server version: 4.0.3Server has startup warnings:2018-10-09T10:20:50.944+0000 I CONTROL  [initandlisten]2018-10-09T10:20:50.944+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2018-10-09T10:20:50.944+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2018-10-09T10:20:50.945+0000 I CONTROL  [initandlisten]---Enable MongoDB's free cloud-based monitoring service, which will then receive and displaymetrics about your deployment (disk utilization, CPU, operation statistics, etc).The monitoring data will be available on a MongoDB website with a unique URL accessible to youand anyone you share the URL with. MongoDB may use this information to make productimprovements and to suggest MongoDB products and deployment options to you.To enable free monitoring, run the following command: db.enableFreeMonitoring()To permanently disable this reminder, run the following command: db.disableFreeMonitoring()---BigBoss:PRIMARY>BigBoss:PRIMARY>BigBoss:PRIMARY> rs.status(){    "set" : "BigBoss",    "date" : ISODate("2018-10-09T10:22:16.104Z"),    "myState" : 1,    "term" : NumberLong(2),    "syncingTo" : "",    "syncSourceHost" : "",    "syncSourceId" : -1,    "heartbeatIntervalMillis" : NumberLong(2000),    "optimes" : {        "lastCommittedOpTime" : {            "ts" : Timestamp(1539080483, 1),            "t" : NumberLong(1)        },        "readConcernMajorityOpTime" : {            "ts" : Timestamp(1539080483, 1),            "t" : NumberLong(1)        },        "appliedOpTime" : {            "ts" : Timestamp(1539080528, 1),            "t" : NumberLong(2)        },        "durableOpTime" : {            "ts" : Timestamp(1539080528, 1),            "t" : NumberLong(2)        }    },    "lastStableCheckpointTimestamp" : Timestamp(1539080483, 1),    "members" : [        {            "_id" : 0,            "name" : "192.168.1.1:30001",            "health" : 0,            "state" : 8,            "stateStr" : "(not reachable/healthy)",            "uptime" : 0,            "optime" : {                "ts" : Timestamp(0, 0),                "t" : NumberLong(-1)            },            "optimeDurable" : {                "ts" : Timestamp(0, 0),                "t" : NumberLong(-1)            },            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),            "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),            "lastHeartbeat" : ISODate("2018-10-09T10:22:15.296Z"),            "lastHeartbeatRecv" : ISODate("2018-10-09T10:21:26.296Z"),            "pingMs" : NumberLong(0),            "lastHeartbeatMessage" : "Error connecting to 192.168.1.1:30001 :: caused by :: Connection refused",            "syncingTo" : "",            "syncSourceHost" : "",            "syncSourceId" : -1,            "infoMessage" : "",            "configVersion" : -1        },        {            "_id" : 1,            "name" : "192.168.1.1:30002",            "health" : 1,            "state" : 1,            "stateStr" : "PRIMARY",            "uptime" : 86,            "optime" : {                "ts" : Timestamp(1539080528, 1),                "t" : NumberLong(2)            },            "optimeDate" : ISODate("2018-10-09T10:22:08Z"),            "syncingTo" : "",            "syncSourceHost" : "",            "syncSourceId" : -1,            "infoMessage" : "could not find member to sync from",            "electionTime" : Timestamp(1539080497, 1),            "electionDate" : ISODate("2018-10-09T10:21:37Z"),            "configVersion" : 1,            "self" : true,            "lastHeartbeatMessage" : ""        },        {            "_id" : 2,            "name" : "192.168.1.1:30003",            "health" : 1,            "state" : 7,            "stateStr" : "ARBITER",            "uptime" : 85,            "lastHeartbeat" : ISODate("2018-10-09T10:22:15.275Z"),            "lastHeartbeatRecv" : ISODate("2018-10-09T10:22:14.994Z"),            "pingMs" : NumberLong(0),            "lastHeartbeatMessage" : "",            "syncingTo" : "",            "syncSourceHost" : "",            "syncSourceId" : -1,            "infoMessage" : "",            "configVersion" : 1        }    ],    "ok" : 1,    "operationTime" : Timestamp(1539080528, 1),    "$clusterTime" : {        "clusterTime" : Timestamp(1539080528, 1),        "signature" : {            "hash" : BinData(0,"f+D+XukeDBrkwdiZD5AvUJkHg3M="),            "keyId" : NumberLong("6610298923057676289")        }    }}BigBoss:PRIMARY>
0