实验环境:准备三台服务器部署MongoDB副本集
cd /data/mongodb
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.18.tgz
tar xf mongodb-linux-x86_64-rhel70-4.4.18.tgz
mv mongodb-linux-x86_64-rhel70-4.4.18 mongodb_install
##配置MongoDB环境变量
echo 'export PATH=/data/mongodb/mongodb_install/bin:$PATH' >> /etc/profile
source /etc/profile
mkdir /data/mongodb/{conf,data,log,keyfile} -p
vim /data/mongodb/conf/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/log/mongod.log
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/mongodb/log/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
#security:
# authorization: enabled
# keyFile: /data/mongodb/keyfile/keyfile
replication:
##配置文件在三台服务器上此项可分别为,rs01,rs02,rs03
replSetName: "rs01"
useradd mongodb -s /sbin/nologin
chown mongodb:mongodb /data/mongodb/ -R
su - mongodb -s /bin/bash
mongod -f /data/mongodb/conf/mongod.conf
exit
> conf={_id:'rs01',members:[
... {_id:0,host:'10.0.1.223:27017'},
... {_id:1,host:'10.0.1.224:27017'},
... {_id:2,host:'10.0.2.79:27017'}]
... }
{
"_id" : "rs01",
"members" : [
{
"_id" : 0,
"host" : "10.0.1.223:27017"
},
{
"_id" : 1,
"host" : "10.0.1.224:27017"
},
{
"_id" : 2,
"host" : "10.0.2.79:27017"
}
]
}
> rs.initiate(conf);
{ "ok" : 1 }
查看集群状态
rs01:PRIMARY> rs.status()
{
"set" : "rs01",
"date" : ISODate("2022-09-22T02:36:07.484Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2022-09-22T02:36:04.376Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2022-09-22T02:36:04.376Z"),
"appliedOpTime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2022-09-22T02:36:04.376Z"),
"lastDurableWallTime" : ISODate("2022-09-22T02:36:04.376Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1663814134, 1),
"lastStableCheckpointTimestamp" : Timestamp(1663814134, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2022-09-22T02:32:34.301Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1663813944, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-09-22T02:32:34.334Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2022-09-22T02:32:34.913Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.8.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 252,
"optime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-09-22T02:36:04Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1663813954, 1),
"electionDate" : ISODate("2022-09-22T02:32:34Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.8.6:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 223,
"optime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-09-22T02:36:04Z"),
"optimeDurableDate" : ISODate("2022-09-22T02:36:04Z"),
"lastHeartbeat" : ISODate("2022-09-22T02:36:06.468Z"),
"lastHeartbeatRecv" : ISODate("2022-09-22T02:36:07.087Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.8.5:27017",
"syncSourceHost" : "192.168.8.5:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.8.7:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 223,
"optime" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1663814164, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-09-22T02:36:04Z"),
"optimeDurableDate" : ISODate("2022-09-22T02:36:04Z"),
"lastHeartbeat" : ISODate("2022-09-22T02:36:06.475Z"),
"lastHeartbeatRecv" : ISODate("2022-09-22T02:36:07.088Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.8.5:27017",
"syncSourceHost" : "192.168.8.5:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1663814164, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1663814164, 1)
}
rs01:PRIMARY> use admin
rs01:PRIMARY>
db.createUser({user:"root",pwd:"********",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
mongod -f /data/mongodb/conf/mongod.conf --shutdown
openssl rand -base64 756 > /data/mongodb/keyfile/keyfile
chmod 600 /data/mongodb/keyfile/keyfile
chown mongodb:mongodb /data/mongodb/keyfile/keyfile
scp keyfile 10.0.1.224:/data/mongodb/keyfile/
scp keyfile 10.0.2.79:/data/mongodb/keyfile/
vim /data/mongodb/conf/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/log/mongod.log
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/mongodb/log/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
keyFile: /data/mongodb/keyfile/keyfile
replication:
replSetName: "rs01"
su - mongodb -s /bin/bash
mongod -f /data/mongodb/conf/mongod.conf
exit
mongo
rs01:PRIMARY> use admin
switched to db admin
rs01:PRIMARY> db.auth("root","******")
1
rs01:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
rs01:PRIMARY>