1.服务器分配
os1:PRIMARY
os2:SECONDARY
os3:ARBITER
2.下载mongodb 对应的安装包
下载地址:https://www.mongodb.com/download-center/community
下载对应的安装包,如果当前版本没有对应系统的版本就找其他版本
3. mkdir /app
4.上传压缩包到/app目录并解压
tar -zxvf mongodb-linux-x86_64-4.0.16.tgz
mv mongodb-linux-x86_64-4.0.16 mongodb
5.创建配置文件
mkdir /app/mongodb/conf
cd /app/mongodb/conf
touch mongo.conf
mongo.conf文件内容
dbpath=/app/mongodb/data logpath=/app/mongodb/log/mongo.log pidfilepath=/app/mongodb/pid/mongo.pid directoryperdb=true logappend=true replSet=testrs bind_ip=192.168.5.16 port=27017 oplogSize=10000 fork=true noprealloc=true
6.创建mongo.conf文件中对应的目录
mkdir /app/mongodb/data
mkdir /app/mongodb/log
mkdir /app/mongodb/pid
7.复制mongodb文件 到其他主机
scp -r /app/mongodb os2:/app/
scp -r /app/mongodb os3:/app/
8.修改其他主机的mongo.conf文件中bind_ip为本机ip
见5mongo.conf文件内容
9.所有主机启动mongodb
cd /app/mongodb/bin
./mongod -f /app/mongodb/conf/mongo.conf
10.在非ARBITER的主机连接mongodb
cd /app/mongodb/bin
./mongo 192.168.5.17:27017
11.配置config
cfg={_id:"testrs",members:[{_id:0,host:"192.168.5.16:27017",priority:0},{_id:1,host:"192.168.5.17:27017",priority:1},{_id:2,host:"192.168.5.18:27017",arbiterOnly:true}]};
12.初始化
rs.initiate(cfg)
13.查看状态验证是否配置成功
rs.status()
################## ###窗口的状态显示如下#### ##################
{ "set" : "testrs", "date" : ISODate("2020-03-06T07:53:22.228Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) } }, "lastStableCheckpointTimestamp" : Timestamp(1583481190, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2020-03-06T07:53:08.870Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1583481177, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2020-03-06T07:53:10.272Z"), "wMajorityWriteAvailabilityDate" : ISODate("2020-03-06T07:53:11.379Z") }, "members" : [ { "_id" : 0, "name" : "192.168.5.16:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 24, "optime" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-03-06T07:53:11Z"), "optimeDurableDate" : ISODate("2020-03-06T07:53:11Z"), "lastHeartbeat" : ISODate("2020-03-06T07:53:20.884Z"), "lastHeartbeatRecv" : ISODate("2020-03-06T07:53:21.402Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.5.17:27017", "syncSourceHost" : "192.168.5.17:27017", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 1, "name" : "192.168.5.17:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 575, "optime" : { "ts" : Timestamp(1583481191, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-03-06T07:53:11Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1583481188, 1), "electionDate" : ISODate("2020-03-06T07:53:08Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "192.168.5.18:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 24, "lastHeartbeat" : ISODate("2020-03-06T07:53:20.884Z"), "lastHeartbeatRecv" : ISODate("2020-03-06T07:53:20.253Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1583481191, 1), "$clusterTime" : { "clusterTime" : Timestamp(1583481191, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }