一、部署準備
1.三臺節點準備
192.168.145.172
192.168.145.173
192.168.145.174
2.官網地址:社群版傳送門
3.選擇對應的版本並下載
4.建立用於存放資料、日誌檔案和配置檔案的資料夾
mkdir -p /data/mongodb
mkdir /var/log/mongodb
mkdir /etc/mongodb
5.解壓安裝包,配置環境變數
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.28.tgz -C /usr/local
#配置環境變數
vim /etc/profile.d/mongodb.sh
#加入如下內容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
#重啟系統配置
source /etc/profile.d/mongodb.sh
6.新增配置檔案 三個節點保持一致
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb/"
journal:
enabled: true
processManagement:
fork: true
pidFilePath: "/data/mongodb/mongod.pid"
net:
bindIpAll: true
port: 27017
#security:
#keyFile: "/etc/mongodb/mongodb-keyfile
#authorization: "enabled"
#setParameter:
# authenticationMechanisms: SCRAM-SHA-1
replication:
oplogSizeMB: 500
replSetName: mongodb_rs
二、mongodb服務
1.啟動三個節點
[root@mongo2 mongodb]# mongod -f /etc/mongodb/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1812
child process started successfully, parent exiting
2.任選一個節點配置叢集
進入mongodb
#進入mongodb
[root@mongo1 mongodb]# mongo 192.168.145.172:27017
MongoDB shell version v4.0.28
connecting to: mongodb://192.168.145.172:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2d98d09b-1ea6-4a3e-85bf-bea736e2b6e3") }
MongoDB server version: 4.0.28
Server has startup warnings:
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten]
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten]
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten]
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten]
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2024-03-20T13:28:08.543+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2024-03-20T13:28:08.543+0800 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()
---
執行節點配置命令
> cfg={ _id:"mongodb_rs", members:[ {_id:1,host:'192.168.145.172:27017',priority:2}, {_id:2,host:'192.168.145.173:27017',priority:1}, {_id:3,host:'192.168.145.174:27017',arbiterOnly:true}] };
{
"_id" : "mongodb_rs",
"members" : [
{
"_id" : 1,
"host" : "192.168.145.172:27017",
"priority" : 2
},
{
"_id" : 2,
"host" : "192.168.145.173:27017",
"priority" : 1
},
{
"_id" : 3,
"host" : "192.168.145.174:27017",
"arbiterOnly" : true
}
]
}
> rs.initiate(cfg)
{ "ok" : 1 }
檢視節點狀態
mongodb_rs:PRIMARY> rs.status()
{
"set" : "mongodb_rs",
"date" : ISODate("2024-03-20T07:38:28.806Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1710920291, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2024-03-20T05:43:20.422Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1710913389, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 2,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2024-03-20T05:43:20.425Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2024-03-20T05:43:21.005Z")
},
"members" : [
{
"_id" : 1,
"name" : "192.168.145.172:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 7820,
"optime" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2024-03-20T07:38:21Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1710913400, 1),
"electionDate" : ISODate("2024-03-20T05:43:20Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.145.173:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 6918,
"optime" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1710920301, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2024-03-20T07:38:21Z"),
"optimeDurableDate" : ISODate("2024-03-20T07:38:21Z"),
"lastHeartbeat" : ISODate("2024-03-20T07:38:28.653Z"),
"lastHeartbeatRecv" : ISODate("2024-03-20T07:38:27.407Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.145.172:27017",
"syncSourceHost" : "192.168.145.172:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.145.174:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 6918,
"lastHeartbeat" : ISODate("2024-03-20T07:38:28.653Z"),
"lastHeartbeatRecv" : ISODate("2024-03-20T07:38:28.064Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1710920301, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1710920301, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
三、使用者管理
1.建立admin管理使用者
#進入資料庫 建立root使用者
mongodb_rs:PRIMARY> use admin
switched to db admin
mongodb_rs:PRIMARY> db.createUser({
... user:"root",
... pwd:"qwer123123",
... roles:[ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... })
#輸出結果
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
2.新建資料庫並建立相應的管理使用者
#先切換到新的資料庫
mongodb_rs:PRIMARY> use testdb
switched to db testdb
#建立對應的管理使用者
mongodb_rs:PRIMARY> db.createUser({
... user: "test01",
... pwd: "123456",
... roles:[{role: "readWrite",db: "testdb"}]
... })
Successfully added user: {
"user" : "test01",
"roles" : [
{
"role" : "readWrite",
"db" : "testdb"
}
]
}