环境:
系统版本:CentOS 7.5
内核:4.18.7-1.el7.elrepo.x86_64
Docker-ce: 18.06
MongoDB: 4.0.3
宿主机IP:192.168.1.1
MongoDB1端口:30001
MongoDB2端口:30002
MongoDB3端口:30003
一、安装docker、docker-compose
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce python-pip
mkdir /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
}
EOF
pip install docker-compose
systemctl enable docker
systemctl start docker
二、创建副本集所需的key
#创建一个volume,将创建的key文件复制至volume中
#400权限是要保证安全性,否则mongod启动会报错
#999用户是容器中的mongod用户
cd && mkdir mongod && cd mongod
openssl rand -base64 756 > mongo.key
docker volume create mongod_mongo_key
MONGO_KEY_DIR=`docker volume inspect mongod_mongo_key | grep Mount | sed -r ‘s#.*"(.*)",$#\1#‘`
cp mongo.key $MONGO_KEY_DIR
chmod 400 $MONGO_KEY_DIR/mongo.key
chown 999.999 $MONGO_KEY_DIR/mongo.key
mkdir /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.yaml
version: "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: mongodb
volumes:
mongo_key:
EOF
四、启动副本集
#副本集群成员的IP地址必须要让客户的也能解析这个IP,不然使用集群方式连接数据库时,无法连接!!!
#若是在阿里云ECS上搭建副本集,想要让公网的客户端能够连接,必须要填ECS的公网IP!!!
#启动三个容器,并使他们后台运行
docker pull mongo:4.0.3
docker-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,
}
}
)
####################################################
[[email protected] mongod]#mongo -port 30001 -uroot -p123456 --authenticationDatabase admin
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:30001/
Implicit session: session { "id" : UUID("a196085a-a142-450f-9eab-a0fc5a58c43b") }
MongoDB server version: 4.0.3
Server 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 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.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 -a
docker stop mongodb1
#######################################################################################
[[email protected] mongod]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62f42e2aee19 mongo:4.0.3 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:30003->27017/tcp mongodb3
735b8d8a2bcf mongo:4.0.3 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:30001->27017/tcp mongodb1
27ebcebde77e mongo:4.0.3 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:30002->27017/tcp mongodb2
[[email protected] mongod]#docker stop mongodb1
mongodb1
[[email protected] mongod]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62f42e2aee19 mongo:4.0.3 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:30003->27017/tcp mongodb3
735b8d8a2bcf mongo:4.0.3 "docker-entrypoint.s…" 6 minutes ago Exited (0) 2 seconds ago mongodb1
27ebcebde77e mongo:4.0.3 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:30002->27017/tcp mongodb2
[[email protected] mongod]#
#连接端口为30002的容器
mongo -port 30002 -uroot -p123456 --authenticationDatabase admin
#查看集群状态
rs.status()
#######################################################################################
[[email protected] mongod]#mongo -port 30002 -uroot -p123456 --authenticationDatabase admin
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:30002/
Implicit session: session { "id" : UUID("7a83559d-84e3-4c89-93dd-1947d40c4837") }
MongoDB server version: 4.0.3
Server 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 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()
---
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>
原文地址:http://blog.51cto.com/bigboss/2296445
时间: 2024-10-31 15:02:45