虚拟机模拟mongodb配置多节点副本集,实现事务特性
一、环境环境:Ubuntu 22,完成mongd、mongosh、mongodump安装,第一步先进行备份(这里端口用的是27070):(注意,这里配置内部地址,在远程连接时无法连接,或者加上直连参数;正式环境时可以配置内部地址,因为数据库和服务器在同一设备或者一个网络内)
mongodb://35.109.172.62:27070/emanyuen?directConnection=true&replicaSet=rs0'}) #地址非真实
mongodump --port 27070 --out ./backup
二、新建2个节点,1个仲裁节点,通过docker创建。事先下载mongo:latest镜像
docker network create mongo-cluster #创建虚拟网络 docker run -d --name mongo-secondary1 --network mongo-cluster -p 27071:27017 mongo:latest mongod --replSet rs0 docker run -d --name mongo-secondary2 --network mongo-cluster -p 27072:27017 mongo:latest mongod --replSet rs0 docker run -d --name mongo-arbiter --network mongo-cluster -p 27073:27017 mongo:latest mongod --replSet rs0

三、修改/etc/mongd.conf配置,重新启动 mongod
storage: dbPath: /var/lib/mongodb systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27070 bindIp: 0.0.0.0 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo #security: security: authorization: disabled #operationProfiling: replication: replSetName: rs0 #这里重点统一副本集名称
四、进入mongo shell进行配置
mongo --port 27070
// 初始化副本集,注意host要么都使用localhost,要么全部不能使用,这里使用mongo-cluster的ip
var config = {
"_id": "rs0",
"members": [
{
"_id": 0,
"host": "172.18.0.1:27070"
},
{
"_id": 1,
"host": "172.18.0.2:27071"
},
{
"_id": 2,
"host": "172.18.0.3:27072"
},
{
"_id": 3,
"host": "172.18.0.4:27073",
"arbiterOnly": true
}
]
};
rs.initiate(config);可以通过rs.status()查看状态
rs0 [direct: primary] test> rs.status()
{
set: 'rs0',
date: ISODate('2025-03-08T06:11:27.651Z'),
myState: 1,
term: Long('2'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 3,
writeMajorityCount: 3,
votingMembersCount: 4,
writableVotingMembersCount: 3,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
lastCommittedWallTime: ISODate('2025-03-08T06:11:18.725Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
appliedOpTime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
durableOpTime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
lastAppliedWallTime: ISODate('2025-03-08T06:11:18.725Z'),
lastDurableWallTime: ISODate('2025-03-08T06:11:18.725Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1741414238, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2025-03-08T05:45:38.697Z'),
electionTerm: Long('2'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long('-1') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1741412554, i: 1 }), t: Long('1') },
numVotesNeeded: 1,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
newTermStartDate: ISODate('2025-03-08T05:45:38.699Z'),
wMajorityWriteAvailabilityDate: ISODate('2025-03-08T05:45:38.702Z')
},
members: [
{
_id: 0,
name: '172.18.0.1:27070',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 1550,
optime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
optimeDate: ISODate('2025-03-08T06:11:18.000Z'),
lastAppliedWallTime: ISODate('2025-03-08T06:11:18.725Z'),
lastDurableWallTime: ISODate('2025-03-08T06:11:18.725Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1741412738, i: 1 }),
electionDate: ISODate('2025-03-08T05:45:38.000Z'),
configVersion: 6,
configTerm: 2,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '172.18.0.2:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 44,
optime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
optimeDurable: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
optimeDate: ISODate('2025-03-08T06:11:18.000Z'),
optimeDurableDate: ISODate('2025-03-08T06:11:18.000Z'),
lastAppliedWallTime: ISODate('2025-03-08T06:11:18.725Z'),
lastDurableWallTime: ISODate('2025-03-08T06:11:18.725Z'),
lastHeartbeat: ISODate('2025-03-08T06:11:25.990Z'),
lastHeartbeatRecv: ISODate('2025-03-08T06:11:25.987Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '172.18.0.1:27070',
syncSourceId: 0,
infoMessage: '',
configVersion: 6,
configTerm: 2
},
{
_id: 2,
name: '172.18.0.3:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 44,
optime: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
optimeDurable: { ts: Timestamp({ t: 1741414278, i: 1 }), t: Long('2') },
optimeDate: ISODate('2025-03-08T06:11:18.000Z'),
optimeDurableDate: ISODate('2025-03-08T06:11:18.000Z'),
lastAppliedWallTime: ISODate('2025-03-08T06:11:18.725Z'),
lastDurableWallTime: ISODate('2025-03-08T06:11:18.725Z'),
lastHeartbeat: ISODate('2025-03-08T06:11:25.992Z'),
lastHeartbeatRecv: ISODate('2025-03-08T06:11:25.993Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '172.18.0.1:27070',
syncSourceId: 0,
infoMessage: '',
configVersion: 6,
configTerm: 2
},
{
_id: 3,
name: '172.18.0.4:27017',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 44,
lastHeartbeat: ISODate('2025-03-08T06:11:25.988Z'),
lastHeartbeatRecv: ISODate('2025-03-08T06:11:25.990Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 6,
configTerm: 2
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1741414278, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1741414278, i: 1 })
}五、修改配置方法,先查出cfg,然后类似js语法,对members进行修改,然后rs.reconfig(cfg),同时关注rs.add,rs.addArb用法
rs0 [direct: primary] test> var cfg = rs.conf();
rs0 [direct: primary] test> cfg
{
_id: 'rs0',
version: 3,
term: 2,
members: [
{
_id: 0,
host: '172.18.0.1:27070',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
{
_id: 1,
host: '172.18.0.2:27017',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
{
_id: 2,
host: '172.18.0.3:27017',
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('67cbd7ee890e374fac85a194')
}
}
rs0 [direct: primary] test> cfg.members.push({"_id":3,"host":"172.18.0.4:27017","arbiterOnly":true})
rs0 [direct: primary] test> rs.reconfig(cfg);
#或者直接对cfg.members重新赋值,最后在rs.reconfig(cfg)

