MongoDB 6.0.3副本集搭建
環境說明:
主:192.168.31.200 埠:27017 從:192.168.31.200 埠:27018 從:192.168.31.200 埠:27019
mongodb介質下載
上傳
[root@dcs01 ~]# ls -lrth /soft/montodb/ total 69M -rw-r--r-- 1 root root 69M Dec 9 16:51 mongodb-linux-x86_64-rhel70-6.0.3.tgz
檢查MD5值
[root@dcs01 ~]# md5sum /soft/montodb/mongodb-linux-x86_64-rhel70-6.0.3.tgz 39d04009f64a939c3c457a706738f0db /soft/montodb/mongodb-linux-x86_64-rhel70-6.0.3.tgz
建立目錄
mkdir /mongodb/{27017,27018,27019}/{dbms,data,log,conf} -p [root@dcs01 ~]# tree /mongodb/ /mongodb/ ├── 27017 │ ├── conf │ ├── data │ ├── dbms │ └── log ├── 27018 │ ├── conf │ ├── data │ ├── dbms │ └── log └── 27019 ├── conf ├── data ├── dbms └── log 15 directories, 0 files
解壓
[root@cjc-db-01 montodb]# tar -zxvf mongodb-linux-x86_64-rhel70-6.0.3.tgz mongodb-linux-x86_64-rhel70-6.0.3/LICENSE-Community.txt mongodb-linux-x86_64-rhel70-6.0.3/MPL-2 mongodb-linux-x86_64-rhel70-6.0.3/README mongodb-linux-x86_64-rhel70-6.0.3/THIRD-PARTY-NOTICES mongodb-linux-x86_64-rhel70-6.0.3/bin/install_compass mongodb-linux-x86_64-rhel70-6.0.3/bin/mongod mongodb-linux-x86_64-rhel70-6.0.3/bin/mongos
複製
[root@cjc-db-01 montodb]# cp -r mongodb-linux-x86_64-rhel70-6.0.3/* /mongodb/27017/dbms/ [root@cjc-db-01 montodb]# cp -r mongodb-linux-x86_64-rhel70-6.0.3/* /mongodb/27018/dbms/ [root@cjc-db-01 montodb]# cp -r mongodb-linux-x86_64-rhel70-6.0.3/* /mongodb/27019/dbms/
建立使用者
[root@cjc-db-01 montodb]# useradd mongo [root@cjc-db-01 montodb]# passwd mongo [root@cjc-db-01 montodb]# id mongo uid=2002(mongo) gid=2002(mongo) groups=2002(mongo)
授權
[root@cjc-db-01 montodb]# chown mongo.mongo /mongodb -R
配置使用者環境變數
[root@cjc-db-01 montodb]# su - mongo [mongo@cjc-db-01 ~]$ vi .bash_profile export MONGODB_HOME=/mongodb/27017/dbms/ #export MONGODB_HOME=/mongodb/27018/dbms/ #export MONGODB_HOME=/mongodb/27019/dbms/ export PATH=$PATH:$MONGODB_HOME/bin
準備配置檔案
[mongo@cjc-db-01 ~]$ vi /mongodb/27017/conf/mongodb.conf dbpath=/mongodb/27017/data logpath=/mongodb/27017/log/mongodb.log logappend=true bind_ip = 0.0.0.0 fork = true port = 27017
啟動資料庫
[mongo@cjc-db-01 ~]$ mongod -config /mongodb/27017/conf/mongodb.conf
登入資料庫
沒有mongo命令,需要下載mongoshell工具
[mongo@cjc-db-01 ~]$ mongo bash: mongo: command not found...
下載mongoshell
[root@cjc-db-01 montodb]# ls -lrht mongosh-1.6.1-linux-x64.tgz -rw-r--r-- 1 root root 57M Dec 9 17:49 mongosh-1.6.1-linux-x64.tgz
檢查md5值
[root@cjc-db-01 montodb]# md5sum mongosh-1.6.1-linux-x64.tgz 8f66bb1f47531d9fc798af866c044bba mongosh-1.6.1-linux-x64.tgz
解壓
[root@cjc-db-01 montodb]# tar -zxvf mongosh-1.6.1-linux-x64.tgz [root@cjc-db-01 montodb]# mv mongosh-1.6.1-linux-x64/* /mongodb/mongosh/ [root@cjc-db-01 montodb]# chown mongo.mongo /mongodb -R
新增環境變數
[mongo@cjc-db-01 ~]$ vi .bash_profile export MONGODB_HOME=/mongodb/27017/dbms export MONGOSH_HOME=/mongodb/mongosh #export MONGODB_HOME=/mongodb/27018/dbms/ #export MONGODB_HOME=/mongodb/27019/dbms/ export PATH=$PATH:$MONGODB_HOME/bin:$MONGOSH_HOME/bin [mongo@cjc-db-01 ~]$ source .bash_profile
再次登入資料庫
mongosh "mongodb://ip:port" mongosh ip:port/資料庫 -u 使用者名稱 -p 密碼 mongosh -u 使用者名稱 -p 密碼 --port 埠號 --host ip 資料庫名
[mongo@cjc-db-01 ~]$ ls /mongodb/mongosh/bin/ mongosh mongosh_crypt_v1.so [mongo@cjc-db-01 ~]$ vi .bash_profile [mongo@cjc-db-01 ~]$ source .bash_profile [mongo@cjc-db-01 ~]$ mongosh --port 27017 Current Mongosh Log ID:639305da87453024db5129bd Connecting to:mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.1 Using MongoDB:6.0.3 Using Mongosh:1.6.1 For mongosh info see: To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (). You can opt-out by running the disableTelemetry() command. ------ The server generated these startup warnings when booting 2022-12-09T17:21:24.329+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2022-12-09T17:21:24.329+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2022-12-09T17:21:24.329+08:00: vm.max_map_count is too low ------ ------ 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() ------ test> show dbs; admin 40.00 KiB config 12.00 KiB local 40.00 KiB
插入測試資料
cjc> db.t01.insert({"tname":"cjc"}) DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite. { acknowledged: true, insertedIds: { '0': ObjectId("6393063ca39bb9bd2b389670") } }
cjc> db.t01.find() [ { _id: ObjectId("6393063ca39bb9bd2b389670"), tname: 'cjc' } ]
關閉資料庫
cjc> db.shutdownServer(); MongoNetworkError: connection 6 to 127.0.0.1:27017 closed
啟動所有節點資料庫
mongod -config /mongodb/27017/conf/mongodb.conf mongod -config /mongodb/27018/conf/mongodb.conf mongod -config /mongodb/27019/conf/mongodb.conf
檢視程式
[mongo@cjc-db-01 mongodb]$ ps -ef|grep mongod|grep -v grep mongo 2198 1 1 09:01 ? 00:00:07 mongod -config /mongodb/27017/conf/mongodb.conf mongo 3158 1 6 09:12 ? 00:00:00 mongod -config /mongodb/27018/conf/mongodb.conf mongo 3200 1 9 09:13 ? 00:00:00 mongod -config /mongodb/27019/conf/mongodb.conf
刪除多餘的資料庫
test> use cjc switched to db cjc cjc> db.dropDatabase() { ok: 1, dropped: 'cjc' }
配置副本集
登入任一節點
mongosh --port 27017
配置副本集
配置檔案新增引數
[mongo@cjc-db-01 ~]$ vi /mongodb/27017/conf/mongodb.conf [mongo@cjc-db-01 ~]$ vi /mongodb/27018/conf/mongodb.conf [mongo@cjc-db-01 ~]$ vi /mongodb/27019/conf/mongodb.conf replSet=cjcmonset
建立副本集
test> rs.initiate( { _id : "cjcmonset", members: [ { _id: 0, host: "192.168.31.200:27017" }, { _id: 1, host: "192.168.31.200:27018" }, { _id: 2, host: "192.168.31.200:27019" } ]})
檢視副本集配置
cjcmonset [direct: other] test> rs.conf() { _id: 'cjcmonset', version: 1, term: 1, members: [ { _id: 0, host: '192.168.31.200:27017', arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1, tags: {}, secondaryDelaySecs: Long("0"), votes: 1 }, { _id: 1, host: '192.168.31.200:27018', arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1, tags: {}, secondaryDelaySecs: Long("0"), votes: 1 }, { _id: 2, host: '192.168.31.200:27019', arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1, tags: {}, secondaryDelaySecs: Long("0"), votes: 1 } ], protocolVersion: Long("1"), writeConcernMajorityJournalDefault: true, settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, catchUpTimeoutMillis: -1, catchUpTakeoverDelayMillis: 30000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 }, replicaSetId: ObjectId("63a111fd0e30878fbeca3b6b") } } cjcmonset [direct: primary] test>
檢視副本集狀態
cjcmonset [direct: primary] test> rs.status() { set: 'cjcmonset', date: ISODate("2022-12-20T01:40:36.443Z"), myState: 1, term: Long("1"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, lastCommittedWallTime: ISODate("2022-12-20T01:40:27.265Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, appliedOpTime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, durableOpTime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, lastAppliedWallTime: ISODate("2022-12-20T01:40:27.265Z"), lastDurableWallTime: ISODate("2022-12-20T01:40:27.265Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671500397, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'electionTimeout', lastElectionDate: ISODate("2022-12-20T01:38:16.879Z"), electionTerm: Long("1"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671500285, i: 1 }), t: Long("-1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671500285, i: 1 }), t: Long("-1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-20T01:38:17.119Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-20T01:38:18.561Z") }, members: [ { _id: 0, name: '192.168.31.200:27017', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 792, optime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, optimeDate: ISODate("2022-12-20T01:40:27.000Z"), lastAppliedWallTime: ISODate("2022-12-20T01:40:27.265Z"), lastDurableWallTime: ISODate("2022-12-20T01:40:27.265Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671500296, i: 1 }), electionDate: ISODate("2022-12-20T01:38:16.000Z"), configVersion: 1, configTerm: 1, self: true, lastHeartbeatMessage: '' }, { _id: 1, name: '192.168.31.200:27018', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 150, optime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, optimeDurable: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, optimeDate: ISODate("2022-12-20T01:40:27.000Z"), optimeDurableDate: ISODate("2022-12-20T01:40:27.000Z"), lastAppliedWallTime: ISODate("2022-12-20T01:40:27.265Z"), lastDurableWallTime: ISODate("2022-12-20T01:40:27.265Z"), lastHeartbeat: ISODate("2022-12-20T01:40:35.270Z"), lastHeartbeatRecv: ISODate("2022-12-20T01:40:34.639Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: '192.168.31.200:27017', syncSourceId: 0, infoMessage: '', configVersion: 1, configTerm: 1 }, { _id: 2, name: '192.168.31.200:27019', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 150, optime: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, optimeDurable: { ts: Timestamp({ t: 1671500427, i: 1 }), t: Long("1") }, optimeDate: ISODate("2022-12-20T01:40:27.000Z"), optimeDurableDate: ISODate("2022-12-20T01:40:27.000Z"), lastAppliedWallTime: ISODate("2022-12-20T01:40:27.265Z"), lastDurableWallTime: ISODate("2022-12-20T01:40:27.265Z"), lastHeartbeat: ISODate("2022-12-20T01:40:35.267Z"), lastHeartbeatRecv: ISODate("2022-12-20T01:40:34.638Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: '192.168.31.200:27017', syncSourceId: 0, infoMessage: '', configVersion: 1, configTerm: 1 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671500427, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671500427, i: 1 }) }
資料寫入測試
cjcmonset [direct: primary] test> use cjc cjc> db.t01.insert({"tname":"cjc"}) cjc> db.t01.find()
檢視其他節點
[mongo@cjc-db-01 mongodb]$ mongosh --port 27018 cjcmonset [direct: secondary] test> use cjc switched to db cjc cjcmonset [direct: secondary] cjc> db.t01.find() MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string
[mongo@cjc-db-01 mongodb]$ mongosh --port 27019 cjcmonset [direct: secondary] cjc> db.t01.find() MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string
cjcmonset [direct: secondary] cjc> db.getMongo().setSlaveOk(); MongoshDeprecatedError: [COMMON-10003] Setting slaveOk is deprecated, use setReadPref instead.
設定副本集可以從secondary讀取資料
cjcmonset [direct: secondary] cjc> db.getMongo().setReadPref("secondary") cjcmonset [direct: secondary] cjc> db.t01.find() [ { _id: ObjectId("63a113836be69aceb7e1ae9b"), tname: 'cjc' } ]
主從切換
登入主庫
[mongo@cjc-db-01 mongodb]$ mongosh --port 27017
停主庫
cjcmonset [direct: primary] test> db.shutdownServer(); MongoNetworkError: connection 1 to 127.0.0.1:27017 closed
檢查當前主庫
可以看到原主庫27017故障後,primary切換到27018節點。
[mongo@cjc-db-01 mongodb]$ mongosh --port 27018 cjcmonset [direct: primary] test> rs.status(); { set: 'cjcmonset', date: ISODate("2022-12-20T02:15:33.806Z"), myState: 1, term: Long("2"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, lastCommittedWallTime: ISODate("2022-12-20T02:15:26.248Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, appliedOpTime: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, durableOpTime: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, lastAppliedWallTime: ISODate("2022-12-20T02:15:26.248Z"), lastDurableWallTime: ISODate("2022-12-20T02:15:26.248Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671502516, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'stepUpRequestSkipDryRun', lastElectionDate: ISODate("2022-12-20T02:14:36.145Z"), electionTerm: Long("2"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671502467, i: 1 }), t: Long("1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671502467, i: 1 }), t: Long("1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), priorPrimaryMemberId: 0, numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-20T02:14:36.240Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-20T02:14:37.176Z") }, electionParticipantMetrics: { votedForCandidate: true, electionTerm: Long("1"), lastVoteDate: ISODate("2022-12-20T01:38:16.912Z"), electionCandidateMemberId: 0, voteReason: '', lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1671500285, i: 1 }), t: Long("-1") }, maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1671500285, i: 1 }), t: Long("-1") }, priorityAtElection: 1 }, members: [ { _id: 0, name: '192.168.31.200:27017', health: 0, state: 8, stateStr: '(not reachable/healthy)', uptime: 0, optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDate: ISODate("1970-01-01T00:00:00.000Z"), optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"), lastAppliedWallTime: ISODate("2022-12-20T02:14:46.243Z"), lastDurableWallTime: ISODate("2022-12-20T02:14:46.243Z"), lastHeartbeat: ISODate("2022-12-20T02:15:32.419Z"), lastHeartbeatRecv: ISODate("2022-12-20T02:14:49.286Z"), pingMs: Long("0"), lastHeartbeatMessage: 'Error connecting to 192.168.31.200:27017 :: caused by :: Connection refused', syncSourceHost: '', syncSourceId: -1, infoMessage: '', configVersion: 1, configTerm: 1 }, { _id: 1, name: '192.168.31.200:27018', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 2883, optime: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-20T02:15:26.000Z"), lastAppliedWallTime: ISODate("2022-12-20T02:15:26.248Z"), lastDurableWallTime: ISODate("2022-12-20T02:15:26.248Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671502476, i: 1 }), electionDate: ISODate("2022-12-20T02:14:36.000Z"), configVersion: 1, configTerm: 2, self: true, lastHeartbeatMessage: '' }, { _id: 2, name: '192.168.31.200:27019', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 2247, optime: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, optimeDurable: { ts: Timestamp({ t: 1671502526, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-20T02:15:26.000Z"), optimeDurableDate: ISODate("2022-12-20T02:15:26.000Z"), lastAppliedWallTime: ISODate("2022-12-20T02:15:26.248Z"), lastDurableWallTime: ISODate("2022-12-20T02:15:26.248Z"), lastHeartbeat: ISODate("2022-12-20T02:15:32.305Z"), lastHeartbeatRecv: ISODate("2022-12-20T02:15:33.355Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: '192.168.31.200:27018', syncSourceId: 1, infoMessage: '', configVersion: 1, configTerm: 2 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671502526, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671502526, i: 1 }) } cjcmonset [direct: primary] test>
新增測試資料
cjc> db.t01.insert({"txx":"abc"}) cjcmonset [direct: primary] cjc> db.t01.find() [ { _id: ObjectId("63a113836be69aceb7e1ae9b"), tname: 'cjc' }, { _id: ObjectId("63a1217973fc637ac59b96f0"), txx: 'abc' } ]
繼續停主庫
[mongo@cjc-db-01 mongodb]$ mongosh --port 27018 cjcmonset [direct: primary] test> db.shutdownServer();
[mongo@cjc-db-01 mongodb]$ mongosh --port 27019 cjcmonset [direct: secondary] test> rs.status() { set: 'cjcmonset', date: ISODate("2022-12-20T03:01:49.481Z"), myState: 2, term: Long("3"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671504345, i: 1 }), t: Long("3") }, lastCommittedWallTime: ISODate("2022-12-20T02:45:45.661Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671504345, i: 1 }), t: Long("3") }, appliedOpTime: { ts: Timestamp({ t: 1671504355, i: 1 }), t: Long("3") }, durableOpTime: { ts: Timestamp({ t: 1671504355, i: 1 }), t: Long("3") }, lastAppliedWallTime: ISODate("2022-12-20T02:45:55.661Z"), lastDurableWallTime: ISODate("2022-12-20T02:45:55.661Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671504345, i: 1 }), electionParticipantMetrics: { votedForCandidate: true, electionTerm: Long("2"), lastVoteDate: ISODate("2022-12-20T02:14:36.224Z"), electionCandidateMemberId: 1, voteReason: '', lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1671502467, i: 1 }), t: Long("1") }, maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1671502467, i: 1 }), t: Long("1") }, priorityAtElection: 1 }, members: [ { _id: 0, name: '192.168.31.200:27017', health: 0, state: 8, stateStr: '(not reachable/healthy)', uptime: 0, optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDate: ISODate("1970-01-01T00:00:00.000Z"), optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"), lastAppliedWallTime: ISODate("2022-12-20T02:14:46.243Z"), lastDurableWallTime: ISODate("2022-12-20T02:14:46.243Z"), lastHeartbeat: ISODate("2022-12-20T03:01:49.143Z"), lastHeartbeatRecv: ISODate("2022-12-20T02:14:49.286Z"), pingMs: Long("0"), lastHeartbeatMessage: 'Error connecting to 192.168.31.200:27017 :: caused by :: Connection refused', syncSourceHost: '', syncSourceId: -1, infoMessage: '', configVersion: 1, configTerm: 2 }, { _id: 1, name: '192.168.31.200:27018', health: 0, state: 8, stateStr: '(not reachable/healthy)', uptime: 0, optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDate: ISODate("1970-01-01T00:00:00.000Z"), optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"), lastAppliedWallTime: ISODate("2022-12-20T02:45:45.661Z"), lastDurableWallTime: ISODate("2022-12-20T02:45:45.661Z"), lastHeartbeat: ISODate("2022-12-20T03:01:49.143Z"), lastHeartbeatRecv: ISODate("2022-12-20T02:45:48.697Z"), pingMs: Long("0"), lastHeartbeatMessage: 'Error connecting to 192.168.31.200:27018 :: caused by :: Connection refused', syncSourceHost: '', syncSourceId: -1, infoMessage: '', configVersion: 1, configTerm: 3 }, { _id: 2, name: '192.168.31.200:27019', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 5653, optime: { ts: Timestamp({ t: 1671504355, i: 1 }), t: Long("3") }, optimeDate: ISODate("2022-12-20T02:45:55.000Z"), lastAppliedWallTime: ISODate("2022-12-20T02:45:55.661Z"), lastDurableWallTime: ISODate("2022-12-20T02:45:55.661Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: 'Could not find member to sync from', configVersion: 1, configTerm: 3, self: true, lastHeartbeatMessage: '' } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671504355, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671504355, i: 1 }) } cjcmonset [direct: secondary] test>
插入資料
cjcmonset [direct: secondary] test> use cjc switched to db cjc cjcmonset [direct: secondary] cjc> db.t01.insert({"aaa":"ccc"}) DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite. Uncaught: MongoBulkWriteError: not primary Result: BulkWriteResult { result: { ok: 1, writeErrors: [], writeConcernErrors: [], insertedIds: [ { index: 0, _id: ObjectId("63a126025f708e296b832629") } ], nInserted: 0, nUpserted: 0, nMatched: 0, nModified: 0, nRemoved: 0, upserted: [] } }
檢視
cjcmonset [direct: secondary] cjc> db.getMongo().setReadPref("secondary") cjcmonset [direct: secondary] cjc> db.t01.find() [ { _id: ObjectId("63a113836be69aceb7e1ae9b"), tname: 'cjc' }, { _id: ObjectId("63a1217973fc637ac59b96f0"), txx: 'abc' } ]
啟動
[mongo@cjc-db-01 mongodb]$ mongod -config /mongodb/27017/conf/mongodb.conf [mongo@cjc-db-01 mongodb]$ mongod -config /mongodb/27018/conf/mongodb.conf
27019變成新主庫
[mongo@cjc-db-01 mongodb]$ mongosh --port 27019 cjcmonset [direct: primary] test> rs.status(); { set: 'cjcmonset', date: ISODate("2022-12-20T03:06:08.740Z"), myState: 1, term: Long("4"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, lastCommittedWallTime: ISODate("2022-12-20T03:06:03.084Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, appliedOpTime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, durableOpTime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, lastAppliedWallTime: ISODate("2022-12-20T03:06:03.084Z"), lastDurableWallTime: ISODate("2022-12-20T03:06:03.084Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671505523, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'electionTimeout', lastElectionDate: ISODate("2022-12-20T03:04:53.017Z"), electionTerm: Long("4"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671504345, i: 1 }), t: Long("3") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671504355, i: 1 }), t: Long("3") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-20T03:04:53.059Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-20T03:04:53.679Z") }, electionParticipantMetrics: { votedForCandidate: true, electionTerm: Long("2"), lastVoteDate: ISODate("2022-12-20T02:14:36.224Z"), electionCandidateMemberId: 1, voteReason: '', lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1671502467, i: 1 }), t: Long("1") }, maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1671502467, i: 1 }), t: Long("1") }, priorityAtElection: 1 }, members: [ { _id: 0, name: '192.168.31.200:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 81, optime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, optimeDurable: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, optimeDate: ISODate("2022-12-20T03:06:03.000Z"), optimeDurableDate: ISODate("2022-12-20T03:06:03.000Z"), lastAppliedWallTime: ISODate("2022-12-20T03:06:03.084Z"), lastDurableWallTime: ISODate("2022-12-20T03:06:03.084Z"), lastHeartbeat: ISODate("2022-12-20T03:06:07.159Z"), lastHeartbeatRecv: ISODate("2022-12-20T03:06:08.167Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: '192.168.31.200:27019', syncSourceId: 2, infoMessage: '', configVersion: 1, configTerm: 4 }, { _id: 1, name: '192.168.31.200:27018', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 76, optime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, optimeDurable: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, optimeDate: ISODate("2022-12-20T03:06:03.000Z"), optimeDurableDate: ISODate("2022-12-20T03:06:03.000Z"), lastAppliedWallTime: ISODate("2022-12-20T03:06:03.084Z"), lastDurableWallTime: ISODate("2022-12-20T03:06:03.084Z"), lastHeartbeat: ISODate("2022-12-20T03:06:07.159Z"), lastHeartbeatRecv: ISODate("2022-12-20T03:06:08.166Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: '192.168.31.200:27019', syncSourceId: 2, infoMessage: '', configVersion: 1, configTerm: 4 }, { _id: 2, name: '192.168.31.200:27019', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 5912, optime: { ts: Timestamp({ t: 1671505563, i: 1 }), t: Long("4") }, optimeDate: ISODate("2022-12-20T03:06:03.000Z"), lastAppliedWallTime: ISODate("2022-12-20T03:06:03.084Z"), lastDurableWallTime: ISODate("2022-12-20T03:06:03.084Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671505493, i: 1 }), electionDate: ISODate("2022-12-20T03:04:53.000Z"), configVersion: 1, configTerm: 4, self: true, lastHeartbeatMessage: '' } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671505563, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671505563, i: 1 }) } cjcmonset [direct: primary] test>
插入資料測試
cjcmonset [direct: primary] cjc> db.t01.insert({"aaa":"ccc"}) DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite. { acknowledged: true, insertedIds: { '0': ObjectId("63a126e5406e8620614e4fd5") } } cjcmonset [direct: primary] cjc> db.t01.find() [ { _id: ObjectId("63a113836be69aceb7e1ae9b"), tname: 'cjc' }, { _id: ObjectId("63a1217973fc637ac59b96f0"), txx: 'abc' }, { _id: ObjectId("63a126e5406e8620614e4fd5"), aaa: 'ccc' } ]
檢視資料庫版本
cjcmonset [direct: primary] admin> db.version() 6.0.3
檢視幫助資訊
cjcmonset [direct: primary] admin> rs.help() Replica Set Class: initiate Initiates the replica set. config Returns a document that contains the current replica set configuration. conf Calls replSetConfig reconfig Reconfigures an existing replica set, overwriting the existing replica set configuration. reconfigForPSASet Reconfigures an existing replica set, overwriting the existing replica set configuration, if the reconfiguration is a transition from a Primary-Arbiter to a Primary-Secondary-Arbiter set. status Calls replSetGetStatus isMaster Calls isMaster hello Calls hello printSecondaryReplicationInfo Calls db.printSecondaryReplicationInfo printSlaveReplicationInfo DEPRECATED. Use rs.printSecondaryReplicationInfo printReplicationInfo Calls db.printReplicationInfo add Adds replica set member to replica set. addArb Calls rs.add with arbiterOnly=true remove Removes a replica set member. freeze Prevents the current member from seeking election as primary for a period of time. Uses the replSetFreeze command stepDown Causes the current primary to become a secondary which forces an election. If no stepDownSecs is provided, uses 60 seconds. Uses the replSetStepDown command syncFrom Sets the member that this replica set member will sync from, overriding the default sync target selection logic. secondaryOk This method is deprecated. Use db.getMongo().setReadPref() instead For more information on usage:
###chenjuchao 20221220 14:30###
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2928737/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 搭建MongoDB副本集MongoDB
- MongoDB 副本集搭建MongoDB
- mongodb 4.0副本集搭建MongoDB
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- 手把手教你搭建mongodb副本集MongoDB
- linux下Mongodb叢集搭建:分片+副本集LinuxMongoDB
- MongoDB日常運維-04副本集搭建MongoDB運維
- 第六章 MongoDB副本集搭建MongoDB
- MongoDB副本集MongoDB
- 單機Linux下搭建MongoDB副本集-三節點LinuxMongoDB
- MongoDB部署副本集MongoDB
- MongoDB 副本集管理MongoDB
- 使用Docker搭建MongoDB 5.0版本副本集叢集DockerMongoDB
- 006.MongoDB副本集MongoDB
- MongoDB - 副本集簡介MongoDB
- MongoDB日常運維-06副本集搭建錯誤彙總MongoDB運維
- MongoDB 副本集切換方法MongoDB
- MongoDB 副本集原理及管理MongoDB
- mongo副本集搭建Go
- mongdb副本集搭建
- MongoDB Replica Set 副本集實踐MongoDB
- MongoDB 刪除,新增副本集,並修改副本集IP等資訊MongoDB
- Python連線訪問mongodb副本集PythonMongoDB
- mongodb副本集新增刪除節點MongoDB
- Mongodb分散式叢集副本集+分片MongoDB分散式
- MongoDB4.2 副本集掃盲說明MongoDB
- MongoDB日常運維-05副本集故障切換MongoDB運維
- MongoDB--副本集基本資訊【面試必備】MongoDB面試
- 分散式文件儲存資料庫之MongoDB副本集分散式資料庫MongoDB
- Docker 搭建 MongoDBDockerMongoDB
- 銀河麒麟系統安裝mongodb副本集故障處理MongoDB
- mongodb replicaSet 模式搭建MongoDB模式
- MongoDB副本集keyFile認證檔案必須滿足的條件MongoDB
- k8s-mongodb叢集部署(副本集) (包括規劃文件-攢)K8SMongoDB
- 在Windows上使用Docker 建立MongoDB 副本集的極簡方法(翻譯)WindowsDockerMongoDB
- 搭建MongoDB分片叢集MongoDB
- MongoDB 分片叢集搭建MongoDB
- Docker 搭建叢集 MongoDBDockerMongoDB