域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
這篇文章主要給大家介紹了關(guān)于mongodb 4.0副本集搭建的全過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mongodb 4.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
前言
近期有同學(xué)問(wèn)mongodb副本集難不難部署,我的回答是不難,很快,幾分鐘搞定,比mysql MHA簡(jiǎn)單的不止一點(diǎn)半點(diǎn)。 那么到底如何部署呢?請(qǐng)看下文。
1. 準(zhǔn)備工作
1.1 下載軟件
選擇版本并下載mongodb的軟件,注意操作系統(tǒng)版本等。本次我選用的是percona分支的mongodb 4.2.8版本搭建,操作系統(tǒng)為centos6
cd /usr/local/
wget https://www.percona.com/downloads/percona-server-mongodb-LATEST/percona-server-mongodb-4.2.8-8/binary/tarball/percona-server-mongodb-4.2.8-8-centos6-x86_64.tar.gz
tar -zxvf percona-server-mongodb-4.2.8-8-centos6-x86_64.tar.gz
ln -s percona-server-mongodb-4.2.8-8 mongodb
1.2 規(guī)劃各節(jié)點(diǎn)角色
各節(jié)點(diǎn)角色如下
IP port role
192.168.128.20827017PRIMARY
192.168.128.20927017SECONDARY
192.168.128.20928017ARBITER
2 部署
2.1 創(chuàng)建各節(jié)點(diǎn)相關(guān)目錄
創(chuàng)建數(shù)據(jù)目錄、日志目錄等相關(guān)目錄
[root@m1 ~]# mkdir -p /data/mongodb/27017/{data,logs,tmp,etc,keyfile}
[root@m1 ~]# cd /data/mongodb/27017/
[root@m1 27017]# ll
總用量 20
drwxr-xr-x 2 root root 4096 8月 7 16:59 data
drwxr-xr-x 2 root root 4096 8月 7 16:59 etc
drwxr-xr-x 2 root root 4096 8月 7 16:59 keyfile
drwxr-xr-x 2 root root 4096 8月 7 16:59 logs
drwxr-xr-x 2 root root 4096 8月 7 16:59 tmp
2.2 配置文件
創(chuàng)建配置文件,并添加對(duì)應(yīng)的配置信息,注意仲裁節(jié)點(diǎn)的端口
[root@m1 27017]# cd /data/mongodb/27017/etc/
[root@m1 etc]# vim mongod.conf
添加如下內(nèi)容
storage:
dbPath: /data/mongodb/27017/data
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /data/mongodb/27017/logs/mongod.log
processManagement:
fork: true
pidFilePath: /data/mongodb/27017/tmp/mongod.pid
net:
port: 27017
bindIp: 0.0.0.0replication: replSetName: "test1" # 副本集名稱
2.3 啟動(dòng)各節(jié)點(diǎn)
以其中一個(gè)節(jié)點(diǎn)為例,其他節(jié)點(diǎn)修改對(duì)應(yīng)配置文件即可
[root@m1 local]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 30019
child process started successfully, parent exiting
2.4 初始化副本集
初始化副本集,指定各節(jié)點(diǎn)id及角色,注意副本集的名稱和配置文件里一致。
在任意節(jié)點(diǎn)執(zhí)行如下語(yǔ)句
> use admin
switched to db admin
> rs.initiate( {
... _id: "test1",
... members: [
... { _id: 0, host: "192.168.128.208:27017" },
... { _id: 1, host: "192.168.128.209:27017" },
... { _id: 2, host: "192.168.128.209:28017",arbiterOnly:true }
... ] })
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1596792682, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1596792682, 1)
}
完成后可以查看各集群狀態(tài),如下,可見(jiàn)當(dāng)前192.168.128.208:27017 為PRIMARY節(jié)點(diǎn)
test1:PRIMARY> rs.status()
{
"set" : "test1",
"date" : ISODate("2020-08-07T11:09:24.454Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-08-07T11:09:23.663Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-08-07T11:09:23.663Z"),
"appliedOpTime" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-08-07T11:09:23.663Z"),
"lastDurableWallTime" : ISODate("2020-08-07T11:09:23.663Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1596798513, 1),
"lastStableCheckpointTimestamp" : Timestamp(1596798513, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-08-07T09:31:33.409Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1596792682, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-08-07T09:31:33.444Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-08-07T09:31:34.050Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.128.208:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 5950,
"optime" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-08-07T11:09:23Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1596792693, 1),
"electionDate" : ISODate("2020-08-07T09:31:33Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.128.209:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5882,
"optime" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1596798563, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-08-07T11:09:23Z"),
"optimeDurableDate" : ISODate("2020-08-07T11:09:23Z"),
"lastHeartbeat" : ISODate("2020-08-07T11:09:23.672Z"),
"lastHeartbeatRecv" : ISODate("2020-08-07T11:09:22.804Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.128.208:27017",
"syncSourceHost" : "192.168.128.208:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.128.209:28017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 5882,
"lastHeartbeat" : ISODate("2020-08-07T11:09:23.672Z"),
"lastHeartbeatRecv" : ISODate("2020-08-07T11:09:22.952Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1596798563, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1596798563, 1)
}
test1:PRIMARY>
3 修改為認(rèn)證模式
為考慮安全性等,需要開(kāi)啟認(rèn)證模式(需要通過(guò)用戶名/密碼方式登錄)
3.1 創(chuàng)建用戶
創(chuàng)建超級(jí)管理員賬號(hào)
test1:PRIMARY> db.createUser({user: "root", pwd: "Root#123", roles: [{role: "root", db: "admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
3.2 創(chuàng)建key文件
在主庫(kù)上執(zhí)行腳本生成key文件,然后將結(jié)拷貝到另外2個(gè)節(jié)點(diǎn)
[root@m1 mongodb]# cd /data/mongodb/27017/keyfile/
[root@m1 keyfile]# openssl rand -base64 756 > mongo.key
[root@m1 keyfile]# chmod 600 mongo.key # 必須修改為600權(quán)限,否則無(wú)法啟動(dòng)
以上完成后,將文件復(fù)制到另外2個(gè)節(jié)點(diǎn)
3.3 修改配置文件
配置文件中 添加如下內(nèi)容,注意不同節(jié)點(diǎn)的文件路徑
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /data/mongodb/27017/keyfile/mongo.key
3.4 重啟后并用認(rèn)證模式登陸
# 關(guān)閉mongodb
[root@m1 keyfile]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/etc/mongod.conf -shutdown
killing process with pid: 30675
# 啟動(dòng)mongodb
[root@m1 keyfile]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/etc/mongod.conf about to fork child process, waiting until server is ready for connections.
forked process: 2430
child process started successfully, parent exiting
# 認(rèn)證模式登陸
[root@m1 keyfile]#
[root@m1 keyfile]# /usr/local/mongodb/bin/mongo -u root -p 'Root#123' --authenticationDatabase admin
至此,mongodb 副本集搭建完畢,你會(huì)了嗎?
本文來(lái)自腳本之家,原文鏈接:https://www.jb51.net/article/193090.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!