準(zhǔn)備
安裝
- 解壓:tar -zvxf mongodb-linux-x86_64-rhel70-4.0.2.tgz /opt/app/
- 重命名:為了容易區(qū)分集群拂到,我們把文件重命名 mv mongodb-linux-x86_64-rhel70-4.0.2 ./mongodb-4.0.2-0
- 多集群:假設(shè)我們是3個節(jié)點(diǎn)的集群脆侮,復(fù)制該文件夾三份滩届,如下:
[root@dmfw-testdk mongo]# pwd
/opt/app/mongo
[root@dmfw-testdk mongo]# ls
mongodb-4.0.2-0 mongodb-4.0.2-1 mongodb-4.0.2-2
- 創(chuàng)建配置文件 touch mongodb.conf 姐扮,并將以下內(nèi)容寫入(這文件也要三份,別copy太開心肥惭,記得改路徑跳纳,如果在一臺服務(wù)器,記得改端口绝骚,大兄弟)
dbpath=/opt/app/mongodb-linux-x86_64-rhel70-4.0.2-0/data
logpath=/opt/app/mongodb-linux-x86_64-rhel70-4.0.2-0/log/mongodb.log
fork=true
logappend=true
bind_ip=0.0.0.0
port=27017
replSet=aps-mongo
啟動
- 進(jìn)入到bin目錄 cd /opt/app/mongo/mongodb-4.0.2-0/bin
- 執(zhí)行 ./mongod -config ../mongodb.conf輸出以下內(nèi)容視為成功
2022-02-14T14:15:17.189+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 7198
child process started successfully, parent exiting
集群的三個節(jié)點(diǎn)都要依次啟動
關(guān)閉
- 執(zhí)行命令:./mongod --shutdown --dbpath ../data
- mongodb集群有自動切換主庫功能耐版,如果先關(guān)主庫,主庫就切換到其它上面去了压汪,這里預(yù)防主庫變更粪牲,從庫關(guān)閉后再關(guān)閉主庫
副本集
鏈接mongo ./mongo localhost:27017 (當(dāng)然也可以選擇工具來操作)
初始化
#配置節(jié)點(diǎn)信息
conf=
{
"_id" : "aps-mongo",
"members" : [
{ "_id" : 0, "host" : "172.****:27017" },
{ "_id" : 1, "host" : "172.****:27018" },
{ "_id" : 2, "host" : "172.****:27019" }
]
}
# 初始化節(jié)點(diǎn)
rs.initiate(conf)
- 查看配置
> rs.conf()
{
"_id" : "aps-mongo",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "172.****:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "172.****:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "172.****:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("620a0cc5d6479d18ad5e9e37")
}
}
aps-mongo:PRIMARY>
- 查看集群狀態(tài)
rs.status()
{
"set" : "aps-mongo",
"date" : ISODate("2022-02-14T16:21:49.863+08:00"),
"myState" : 1,
"term" : 1,
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : 2000,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"appliedOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"durableOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
}
},
"lastStableCheckpointTimestamp" : Timestamp(1644826889, 1),
"members" : [
{
"_id" : 0,
"name" : "172.*.*.*:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4916,
"optime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"optimeDate" : ISODate("2022-02-14T16:21:49.000+08:00"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1644825808, 1),
"electionDate" : ISODate("2022-02-14T16:03:28.000+08:00"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.*.*.*:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1112,
"optime" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDurable" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"optimeDurableDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"lastHeartbeat" : ISODate("2022-02-14T16:21:48.719+08:00"),
"lastHeartbeatRecv" : ISODate("2022-02-14T16:21:49.184+08:00"),
"pingMs" : 0,
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.1.207:27017",
"syncSourceHost" : "172.16.1.207:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.*.*.*:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1112,
"optime" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDurable" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"optimeDurableDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"lastHeartbeat" : ISODate("2022-02-14T16:21:48.719+08:00"),
"lastHeartbeatRecv" : ISODate("2022-02-14T16:21:49.184+08:00"),
"pingMs" : 0,
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.1.207:27017",
"syncSourceHost" : "172.16.1.207:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1644826909, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1644826909, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : 0
}
}
}
- 權(quán)限
// 生成base64內(nèi)容的文件,然后在mongo.conf里面配置這個文件路徑止剖,重啟就可以了
[root@dmfw-testdk bin]# openssl rand -base64 666 > /opt/app/mongo/keyfile
[root@dmfw-testdk bin]# cd /opt/app/mongo/
[root@dmfw-testdk mongo]# ls
keyfile mongodb-4.0.2-0 mongodb-4.0.2-1 mongodb-4.0.2-2
//給當(dāng)前用戶設(shè)置讀寫權(quán)限腺阳,這兒科普下linux知識點(diǎn):看接下去的第五行落君;rwx可以用數(shù)字421代替,600說明是當(dāng)前用戶的讀寫權(quán)限亭引;就是這樣绎速,所以別一來就瞎雞兒給 chmod 777 ;這兒還可以科普下chown和chmod的區(qū)別,前者是我把群主給你焙蚓,你就擁有權(quán)限了朝氓,后者是我把你設(shè)置為管理員,差不多就是這個意思
[root@dmfw-testdk mongo]# chmod 600 ./keyfile
[root@dmfw-testdk mongo]# ll
total 4
-rw------- 1 root root 902 Feb 15 09:28 keyfile
//看這排字母 drwxr-xr-x d 是document的縮寫主届,標(biāo)識這個是個文件夾赵哲,后面有9位,前三代表當(dāng)前用戶君丁,中三代表組枫夺,后三代表其他;
drwxr-xr-x 5 root root 187 Feb 14 14:51 mongodb-4.0.2-0
drwxr-xr-x 5 root root 187 Feb 14 15:00 mongodb-4.0.2-1
drwxr-xr-x 5 root root 187 Feb 14 15:01 mongodb-4.0.2-2
- 客戶端工具下載地址:https://nosqlbooster.com/downloads
mongodb的副本集和redis的哨兵模式類似绘闷,都是基于故障自動切換的主從模式橡庞。主提供寫讀,從提供讀印蔗。從各個節(jié)點(diǎn)的日志來看扒最,會有2秒一次的心跳檢測,發(fā)現(xiàn)宕機(jī)后华嘹,會進(jìn)行選舉吧趣,切換主節(jié)點(diǎn),后面單獨(dú)寫一篇文章分析