详细链接https://cloud.tencent.com/developer/article/1509149
docker pull mongo:4.0.0
为MongoDB集群创建独立的docker网桥
#创建容器间公用的网络 docker network create --subnet=10.1.1.0/24 mongodb0 #查看docker 下边的所有网络情况 docker network ls #查看某一个网络的具体情况 docker network inspect ef6 【容器id】 #创建本地的 挂载 路径 mkdir -p /adcs/mongodb/configsvr mkdir -p /adcs/mongodb/shard1 mkdir -p /adcs/mongodb/shard2 mkdir -p /adcs/mongodb/shard3 mkdir -p /adcs/mongodb/mongos docker run -d --name=cfg_1 --network=mongodb0 --ip=10.1.1.2 -v /adcs/mongodb/configsvr:/etc/mongodb mongo:4.0.0 -f /etc/mongodb/mongod.conf docker run -d --name=cfg_2 --network=mongodb0 --ip=10.1.1.3 -v /adcs/mongodb/configsvr:/etc/mongodb mongo:4.0.0 -f /etc/mongodb/mongod.conf docker run -d --name=cfg_3 --network=mongodb0 --ip=10.1.1.4 -v /adcs/mongodb/configsvr:/etc/mongodb mongo:4.0.0 -f /etc/mongodb/mongod.conf docker run -p 60301:27017 -v /adcs/mongodb/data/db:/data/db --name singal_mongodb -d mongo 在上面的命令中,几个命令参数的详细解释如下: -p 指定容器的端口映射,mongodb 默认端口为 27017 -v 为设置容器的挂载目录,这里是将即本机中的目录挂载到容器中的/data/db中,作为 mongodb 的存储目录 –name 为设置该容器的名称 -d 设置容器以守护进程方式运行 mongodb container.jpeg
version: '3.1' services: mongodb1: image: mongo restart: always container_name: mongo1 volumes: - ./data/db/mongo1:/data/db - ./mongodb.key:/data/mongodb.key ports: - 60401:27017 environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} networks: - mongoNet command: mongod --replSet mongos --keyFile /data/mongodb.key entrypoint: - bash - -c - | chmod 400 /data/mongodb.key chown 999:999 /data/mongodb.key exec docker-entrypoint.sh $$@ mongodb2: image: mongo restart: always container_name: mongo2 volumes: - ./data/db/mongo2:/data/db - ./mongodb.key:/data/mongodb.key ports: - 60402:27017 environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} networks: - mongoNet command: mongod --replSet mongos --keyFile /data/mongodb.key entrypoint: - bash - -c - | chmod 400 /data/mongodb.key chown 999:999 /data/mongodb.key exec docker-entrypoint.sh $$@ mongodb3: image: mongo restart: always container_name: mongo3 volumes: - ./data/db/mongo3:/data/db - ./mongodb.key:/data/mongodb.key ports: - 60403:27017 environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} networks: - mongoNet command: mongod --replSet mongos --keyFile /data/mongodb.key entrypoint: - bash - -c - | chmod 400 /data/mongodb.key chown 999:999 /data/mongodb.key exec docker-entrypoint.sh $$@ networks: mongoNet: driver: bridge
#切换到 docker-compose.yml 所在的目录 cd /adcs/mongo2 #启动 mongo 集群 docker-compose up -d #进入节点1 docker exec -it mongo1 /bin/bash #进入mongo 的 命令行端口 mongo 失败,因为没有输入正确的密码
root@e0ff2180d0e4:/# mongo MongoDB shell version v4.2.1 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("69ca0576-1364-48a0-8e91-fb3d871d30fc") } MongoDB server version: 4.2.1 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > rs.status() { "ok" : 0, "errmsg" : "command replSetGetStatus requires authentication", "code" : 13, "codeName" : "Unauthorized" }
mongo -u root -p admin
root@e0ff2180d0e4:/# mongo -u root -p admin MongoDB shell version v4.2.1 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("14b5b96e-852d-4a68-89e7-d7a824174040") } MongoDB server version: 4.2.1 Server has startup warnings: 2019-11-18T06:13:46.970+0000 I STORAGE [initandlisten] 2019-11-18T06:13:46.970+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2019-11-18T06:13:46.970+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-11-18T06:13:58.122+0000 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics 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 you and anyone you share the URL with. MongoDB may use this information to make product improvements 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.status() { "ok" : 0, "errmsg" : "no replset config has been received", "code" : 94, "codeName" : "NotYetInitialized" }
rs.initiate({ _id: “mongos”, members: [ { _id : 0, host : “192.168.1.111:27017” }, { _id : 1, host : “192.168.1.111:27018” }, { _id : 2, host : “192.168.1.111:27019” } ] });
配置成功
rs.initiate({ _id: "mongos", members: [ { _id : 0, host : "122.51.70.155:60401" }, { _id : 1, host : "122.51.70.155:60402" }, { _id : 2, host : "122.51.70.155:60403" } ] }); { "ok" : 1 }