您好,登錄后才能下訂單哦!
系統(tǒng)版本:CentOS 7.5
內(nèi)核: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
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
#創(chuàng)建一個volume,將創(chuàng)建的key文件復制至volume中
#400權(quán)限是要保證安全性,否則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
#映射/data/mongo/mongdb{1,2,3}目錄到容器里,將數(shù)據(jù)持久化到磁盤
#映射出三個端口,以便外部用戶連接
#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,不然使用集群方式連接數(shù)據(jù)庫時,無法連接?。?!
#若是在阿里云ECS上搭建副本集,想要讓公網(wǎng)的客戶端能夠連接,必須要填ECS的公網(wǎng)IP?。?!
#啟動三個容器,并使他們后臺運行
docker pull mongo:4.0.3
docker-compose up -d
#連接30001端口上的容器,開始配置集群
mongo -port 30001 -uroot -p123456 --authenticationDatabase admin
#三節(jié)點,其中一個為投票節(jié)點,并隱藏
#注意host的IP地址,一定要讓客戶端也能連接所有的地址
#_id的值為--replSet參數(shù)后的字符串
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 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>
#現(xiàn)在30001端口的容器已經(jīng)是PRIMARY了
#停止PRIMARY的容器
docker ps -a
docker stop mongodb1
#######################################################################################
[root@master 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
[root@master mongod]#docker stop mongodb1
mongodb1
[root@master 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
[root@master mongod]#
#連接端口為30002的容器
mongo -port 30002 -uroot -p123456 --authenticationDatabase admin
#查看集群狀態(tài)
rs.status()
#######################################################################################
[root@master 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>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。