一、安装mongodb服务
1、下载mongodb二进制包,解压,移动到/usr/local/下。
tar zxf mongodb-linux-x86_64-rhel70-4.4.17.tgz mv mongodb-linux-x86_64-rhel70-4.4.17 /usr/local/mongodb
2、配置环境变量
vim /etc/profile.d/mongo.sh #添加以下配置 export PATH=/usr/local/mongodb/bin:$PATH #加载环境变量配置 source /etc/profile.d/mongo.sh
3、创建数据目录和日志目录
mkdir /usr/local/mongodb/data mkdir /usr/local/mongodb/log
4、创建配置文件
vim /usr/local/mogodb/mogodb.conf #添加以下配置 systemLog: destination: file logAppend: true path: /usr/local/mongodb/log/mongod.log storage: dbPath: /usr/local/mongodb/data journal: enabled: true processManagement: fork: true pidFilePath: /usr/local/mongodb/mongod.pid #timeZoneInfo: /usr/share/zoneinfo net: port: 27017 bindIp: 0.0.0.0
5、启动
mongod -f /usr/local/mongodb/mongodb.conf
二、配置副本集
1、在3台服务器上分别安装好mongodb,可以根据上面的步骤,或者去官网了解更多安装方式。
2、修改3个节点配置文件
vim /usr/local/mongodb/mongodb.conf #添加以下配置,3个节点添加配置必须相同 replication: replSetName: "rs0"
3、重启3个节点服务
mongod -f /usr/local/mongodb/mongodb.conf --shutdown mongod -f /usr/local/mongodb/mongodb.conf
4、登录任意一个节点mongodb,初始化副本集(主+备+仲裁)。
mongo > rs.initiate({ ... _id: "rs0", ... members: [ ... { _id: 0, host: "192.168.1.11:27017" }, ... { _id: 1, host: "192.168.1.12:27017" }, ... { _id: 2, host: "192.168.1.13:27017", arbiterOnly: true } ... ] })
5、查看集群信息
> rs.status() { "set" : "rs0", "date" : ISODate("2022-11-02T08:16:35.330Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "readConcernMajorityWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "appliedOpTime" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "lastDurableWallTime" : ISODate("2022-11-02T08:16:26.250Z") }, "lastStableRecoveryTimestamp" : Timestamp(1667376936, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2022-11-02T08:03:35.546Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1667376204, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2022-11-02T08:03:35.557Z"), "wMajorityWriteAvailabilityDate" : ISODate("2022-11-02T08:03:36.683Z") }, "members" : [ { "_id" : 0, "name" : "192.168.1.11:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2058, "optime" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-11-02T08:16:26Z"), "lastAppliedWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "lastDurableWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1667376215, 1), "electionDate" : ISODate("2022-11-02T08:03:35Z"), "configVersion" : 1, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.1.12:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 790, "optime" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1667376986, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-11-02T08:16:26Z"), "optimeDurableDate" : ISODate("2022-11-02T08:16:26Z"), "lastAppliedWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "lastDurableWallTime" : ISODate("2022-11-02T08:16:26.250Z"), "lastHeartbeat" : ISODate("2022-11-02T08:16:34.218Z"), "lastHeartbeatRecv" : ISODate("2022-11-02T08:16:34.154Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.1.11:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 1 }, { "_id" : 2, "name" : "192.168.1.13:27017", "health" : 1, "state" : 0, "stateStr" : "STARTUP", "uptime" : 790, "lastHeartbeat" : ISODate("2022-11-02T08:16:34.126Z"), "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"), "pingMs" : NumberLong(3), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : -2, "configTerm" : -1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1667376986, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1667376986, 1) }
标签:11,副本,MongoDB,02T08,ISODate,2022,mongodb,NumberLong,搭建 From: https://www.cnblogs.com/NanZhiHan/p/16851445.html