MongoDB副本集replica set (二)--副本集環境搭建
阿新 • • 發佈:2020-06-24
(一)主機資訊
作業系統版本:centos7 64-bit
資料庫版本 :MongoDB 4.2 社群版
ip | hostname |
192.168.10.41 | mongoserver1 |
192.168.10.42 | mongoserver2 |
192.168.10.43 | mongoserver3 |
(二)副本集搭建過程
首先需要在3臺伺服器上安裝MongoDB軟體,安裝過程見:https://www.cnblogs.com/lijiaman/p/12983589.html。安裝完成之後,即可進行後續的配置,具體操作如下:
(1)在一臺機器上建立keyfile
openssl rand -base64 756 > /mongo/mongo-keyfilechmod 400 /mongo/mongo-keyfile
(2)拷貝feyfile到所有節點
scp /mongo/mongo-keyfile [email protected]:/mongo/
scp /mongo/mongo-keyfile [email protected]:/mongo/
(3)以啟用身份驗證的方式開啟所有節點
這裡將所有引數設定到配置檔案裡面,方便管理,配置檔案如下:
[[email protected] mongo]# cat /etc/mongod.conf
# mongod.conf # for documentation of all options, see:# http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data.
systemLog:
destination: file
logAppend: true
path: /mongo/mongod.log # Where and how to store data.
storage:
dbPath: /mongo/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger: # how the process runs
processManagement:fork: true # fork and run in background
pidFilePath: /mongo/mongod.pid # location of pidfile # network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. security:
authorization: enabled # 啟用身份驗證
keyFile: /mongo/mongo-keyfile # 配置keyfile檔案 replication:
replSetName: rstest # 設定副本集名稱
然後啟動所有節點,以節點1為例:
[[email protected] mongo]# mongod -f /etc/mongod.conf
(4)初始化副本集
在其中一個節點執行以下指令碼初始化副本集,只需在一個節點上執行即可。
rs.initiate(
{
_id : "rstest",
members: [
{ _id : 0, host : "192.168.10.41:27017" },
{ _id : 1, host : "192.168.10.42:27017" },
{ _id : 2, host : "192.168.10.43:27017" }
]
}
)
引數含義:
_id :副本集的名稱
members :副本集的成員資訊
在初始化時,會觸發投票選舉一個主節點,可以使用rs.status()來確定主節點成員
rstest:SECONDARY> rs.status()
...
"members" : [
{
"_id" : 0,
"name" : "192.168.10.41:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 280,
"optime" : {
"ts" : Timestamp(1592897767, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-06-23T07:36:07Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1592897607, 1),
"electionDate" : ISODate("2020-06-23T07:33:27Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
...
(5)建立管理員使用者
第一個使用者必須要有建立其它使用者的許可權,例如需要有userAdminAnyDatabase許可權,並且需要建立在admin資料庫中。
因為是在副本集上建立使用者,故要在主節點上執行。如建立root使用者
use admin; db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"userAdminAnyDatabase",db:"admin"}]
}
)
(6)以管理員身份登入資料庫
通過以下方式以管理員身份登入到資料庫
mongo -u root -p 123456 --authenticationDatabase admin
(7)建立一個叢集管理員賬戶
clusterAdmin角色被授予副本集操作的許可權,如配置副本集。在admin資料庫中建立一個叢集管理員並授予clusterAdmin角色。
use admin db.createUser(
{
"user" : "replica",
"pwd" : "replica",
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)
(8)要啟用身份驗證,需要重啟資料庫
重啟完成後,就需要以使用者密碼方式登入資料庫了,假如不使用使用者名稱密碼,可以登入資料庫,但是無法訪問資料
[[email protected] mongo]# mongo
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d49b410b-a7af-4550-a455-faa82885517b") }
MongoDB server version: 4.2.7
rstest:PRIMARY> show dbs
rstest:PRIMARY>
rstest:PRIMARY> db
test
只有使用了使用者名稱密碼,才能查到資料:
[[email protected] mongo]# mongo -u root -p 123456 --authenticationDatabase admin
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a1f0da48-1266-4766-a9e4-32b97a46c3ec") }
MongoDB server version: 4.2.7
rstest:PRIMARY>
rstest:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
【完】