mongdb7副本集修改IP
环境信息
主机名 | IP | 端口 | 资源 |
---|---|---|---|
mongodb1 | 192.168.56.11 | 1C/2G/50G | |
mongodb2 | 192.168.56.12 | 1C/2G/50G | |
mongodb3 | 192.168.56.13 | 1C/2G/50G |
operating system:CentOS-7.6-x86_64-DVD-1810.iso
Master IP:192.168.56.11
Slave IP:192.168.56.12
Arbiter IP:192.168.56.13
Software:mongodb-linux-x86_64-rhel70-7.0.11.tgz
操作系统配置
修改主机名
hostnamectl set-hostname mongodb1
hostnamectl set-hostname mongodb2
hostnamectl set-hostname mongodb3
修改hosts文件
vim /etc/hosts
192.168.56.11 mongodb1
192.168.56.12 mongodb2
192.168.56.13 mongodb3
关闭防火墙和SELINUX
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
vim /etc/selinux/config #将SELINUX设置为disabled
配置网络
网卡:enp0s3
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=enp0s3
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.56.11
NETWORK=255.255.255.0
GATWAY=192.168.56.1
配置本地yum
将CentOS-7.6-x86_64-DVD-1810.iso文件传到/package文件夹
mount -t iso9660 -o loop /package/CentOS-7.6-x86_64-DVD-1810.iso /mnt
创建Centos7.repo
vim Centos7.repo
[local]
name=Centos7
baseurl=file:///mnt
gpgcheck=0
enabled=1
yum clean all&&yum makecache
安装mongodb
上传mongodb的安装包
mongodb-database-tools-rhel70-x86_64-100.9.5.tgz
mongodb-linux-x86_64-rhel70-7.0.11.tgz
mongodb-mongosh-2.2.10.x86_64.rpm
安装mongodb所需要的依赖
yum install -y libcurl openssl xz-libs
解压安装
tar -zxvf mongodb-linux-x86_64-rhel70-7.0.11.tgz -C /usr/local
mv mongodb-linux-x86_64-rhel70-7.0.11 mongodb
rpm -ivh mongodb-mongosh-2.2.10.x86_64.rpm
tar -zxvf mongodb-database-tools-rhel70-x86_64-100.9.5.tgz
cp /package/mongodb-database-tools-rhel70-x86_64-100.9.5/bin/* /usr/local/mongodb/bin/
添加环境变量
vim /etc/profile ##增加一行
export PATH=/usr/local/mongodb/bin:$PATH
配置mongodb.conf
vim /usr/local/mongodb/mongodb.conf
# 设置绑定ip
bind_ip=192.168.56.11
# 设置端口
port=27011
# 是否以守护进程方式运行,默认false
fork=true
# 数据库文件位置
dbpath = /usr/local/mongodb/data
#日志文件的路径
logpath = /usr/local/mongodb/logs/mongo.log
# 是否追加方式写入日志,默认True
logappend=true
# pid文件的路径
pidfilepath=/usr/local/mongodb/logs/mongo.pid
# 设置副本集的名字,所有的实例指定相同的名字属于一个副本集
replSet=bsg
# replication操作日志的最大尺寸
oplogSize=512
具体配置可以参考以下网址
https://blog.csdn.net/yuanwei1144/article/details/103779345
启动mongodb
mkdir /usr/local/mongodb/data
mkdir /usr/local/mongodb/logs
mongod -f /usr/local/mongodb/mongodb.conf
启动完成
[root@mongodb1 mongodb]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 30114
child process started successfully, parent exiting
使用mongosh登录
[root@mongodb1 mongodb]# mongosh 192.168.56.11:27011
Current Mongosh Log ID: 66f4d2e6c29a18edbf149f47
Connecting to: mongodb://192.168.56.11:27011/?directConnection=true&appName=mongosh+2.2.10
Using MongoDB: 7.0.11
Using Mongosh: 2.2.10
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy)
.You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-09-26T11:18:31.875+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestrict
ed 2024-09-26T11:18:31.875+08:00: You are running this process as the root user, which is not recommended
2024-09-26T11:18:31.875+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary vers
ion 2024-09-26T11:18:31.875+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary versi
on 2024-09-26T11:18:31.875+08:00: Soft rlimits for open file descriptors too low
------
Deprecation warnings:
Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
将mongdb2和mongdb3以同样方法安装
添加mongdb集群
初始化mongdb1,将其设置为PRIMARY
test> rs.initiate()
{
info2: 'no configuration specified. Using a default configuration for the set',
me: '192.168.56.11:27011',
ok: 1
}
bsg [direct: other] test> rs.isMaster()
true
bsg [direct: primary] test> rs.status()
[
{
_id: 0,
name: '192.168.56.11:27011',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 344,
optime: { ts: Timestamp({ t: 1727321047, i: 1 }), t: Long('1') },
optimeDate: ISODate('2024-09-26T03:24:07.000Z'),
lastAppliedWallTime: ISODate('2024-09-26T03:24:07.662Z'),
lastDurableWallTime: ISODate('2024-09-26T03:24:07.662Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: 'Could not find member to sync from',
electionTime: Timestamp({ t: 1727321007, i: 2 }),
electionDate: ISODate('2024-09-26T03:23:27.000Z'),
configVersion: 1,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
}
]
添加mongdb2为SECONDARY
bsg [direct: primary] test> rs.add("192.168.56.12:27011")
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727321181, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727321181, i: 1 })
}
bsg [direct: primary] test> rs.status()
{
set: 'bsg',
date: ISODate('2024-09-26T03:26:47.874Z'),
myState: 1,
term: Long('1'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 2,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1727321207, i: 1 }), t: Long('1') },
lastCommittedWallTime: ISODate('2024-09-26T03:26:47.677Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1727321207, i: 1 }), t: Long('1') },
appliedOpTime: { ts: Timestamp({ t: 1727321207, i: 1 }), t: Long('1') },
durableOpTime: { ts: Timestamp({ t: 1727321207, i: 1 }), t: Long('1') },
lastAppliedWallTime: ISODate('2024-09-26T03:26:47.677Z'),
lastDurableWallTime: ISODate('2024-09-26T03:26:47.677Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1727321183, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-09-26T03:23:27.563Z'),
electionTerm: Long('1'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1727321007, i: 1 }), t: Long('-1') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1727321007, i: 1 }), t: Long('-1') },
numVotesNeeded: 1,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
newTermStartDate: ISODate('2024-09-26T03:23:27.631Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-09-26T03:23:27.663Z')
},
members: [
{
_id: 0,
name: '192.168.56.11:27011',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 496,
optime: { ts: Timestamp({ t: 1727321207, i: 1 }), t: Long('1') },
optimeDate: ISODate('2024-09-26T03:26:47.000Z'),
lastAppliedWallTime: ISODate('2024-09-26T03:26:47.677Z'),
lastDurableWallTime: ISODate('2024-09-26T03:26:47.677Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1727321007, i: 2 }),
electionDate: ISODate('2024-09-26T03:23:27.000Z'),
configVersion: 3,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '192.168.56.12:27011',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 26,
optime: { ts: Timestamp({ t: 1727321197, i: 1 }), t: Long('1') },
optimeDurable: { ts: Timestamp({ t: 1727321197, i: 1 }), t: Long('1') },
optimeDate: ISODate('2024-09-26T03:26:37.000Z'),
optimeDurableDate: ISODate('2024-09-26T03:26:37.000Z'),
lastAppliedWallTime: ISODate('2024-09-26T03:26:47.677Z'),
lastDurableWallTime: ISODate('2024-09-26T03:26:47.677Z'),
lastHeartbeat: ISODate('2024-09-26T03:26:47.176Z'),
lastHeartbeatRecv: ISODate('2024-09-26T03:26:47.677Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.56.11:27011',
syncSourceId: 0,
infoMessage: '',
configVersion: 3,
configTerm: 1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727321207, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727321207, i: 1 })
}
添加mongdb3为ARBITER
bsg [direct: primary] test> rs.addArb("192.168.56.13:27011")
MongoServerError[NewReplicaSetConfigurationIncompatible]: Reconfig attempted to install a config that would change the implicit default write c
oncern. Use the setDefaultRWConcern command to set a cluster-wide write concern and try the reconfig again.
官方文档中发现,默认的写关注并一定是majority(文档中还特别点名了具有arbiters的情况),而是会受到arbiter个数的影响。在添加了仲裁节点之后可能会导致默认写关注的改变,所以需要手工配置一下setDefaultRWConcern的内容。
bsg [direct: primary] test> db.adminCommand({
... "setDefaultRWConcern" : 1,
... "defaultWriteConcern" : {
... "w" : 2
... }
... })
{
defaultReadConcern: { level: 'local' },
defaultWriteConcern: { w: 2, wtimeout: 0 },
updateOpTime: Timestamp({ t: 1727322577, i: 1 }),
updateWallClockTime: ISODate('2024-09-26T03:49:41.648Z'),
defaultWriteConcernSource: 'global',
defaultReadConcernSource: 'implicit',
localUpdateWallClockTime: ISODate('2024-09-26T03:49:41.668Z'),
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727322581, i: 2 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727322581, i: 2 })
}
bsg [direct: primary] test> rs.addArb("192.168.56.13:27011")
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727322597, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727322597, i: 1 })
}
bsg [direct: primary] test> rs.addArb("192.168.56.13:27011")
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727322597, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727322597, i: 1 })
}
bsg [direct: primary] test> rs.status()
{
set: 'bsg',
date: ISODate('2024-09-26T05:09:41.111Z'),
myState: 1,
term: Long('1'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
lastCommittedWallTime: ISODate('2024-09-26T05:09:38.333Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
appliedOpTime: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
durableOpTime: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
lastAppliedWallTime: ISODate('2024-09-26T05:09:38.333Z'),
lastDurableWallTime: ISODate('2024-09-26T05:09:38.333Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1727327368, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-09-26T03:23:27.563Z'),
electionTerm: Long('1'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1727321007, i: 1 }), t: Long('-1') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1727321007, i: 1 }), t: Long('-1') },
numVotesNeeded: 1,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
newTermStartDate: ISODate('2024-09-26T03:23:27.631Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-09-26T03:23:27.663Z')
},
members: [
{
_id: 0,
name: '192.168.56.11:27011',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 6670,
optime: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
optimeDate: ISODate('2024-09-26T05:09:38.000Z'),
lastAppliedWallTime: ISODate('2024-09-26T05:09:38.333Z'),
lastDurableWallTime: ISODate('2024-09-26T05:09:38.333Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1727321007, i: 2 }),
electionDate: ISODate('2024-09-26T03:23:27.000Z'),
configVersion: 4,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '192.168.56.12:27011',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 6199,
optime: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
optimeDurable: { ts: Timestamp({ t: 1727327378, i: 1 }), t: Long('1') },
optimeDate: ISODate('2024-09-26T05:09:38.000Z'),
optimeDurableDate: ISODate('2024-09-26T05:09:38.000Z'),
lastAppliedWallTime: ISODate('2024-09-26T05:09:38.333Z'),
lastDurableWallTime: ISODate('2024-09-26T05:09:38.333Z'),
lastHeartbeat: ISODate('2024-09-26T05:09:39.786Z'),
lastHeartbeatRecv: ISODate('2024-09-26T05:09:41.106Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.56.11:27011',
syncSourceId: 0,
infoMessage: '',
configVersion: 4,
configTerm: 1
},
{
_id: 2,
name: '192.168.56.13:27011',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 4783,
lastHeartbeat: ISODate('2024-09-26T05:09:39.785Z'),
lastHeartbeatRecv: ISODate('2024-09-26T05:09:41.066Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 4,
configTerm: 1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727327378, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727327378, i: 1 })
}
集群修改ip-方案一(主从切换修改)
节点 | 原ip | 新ip |
---|---|---|
primary | 192.168.56.11 | 192.168.56.21 |
secondary | 192.168.56.12 | 192.168.56.22 |
arbiter | 192.168.56.13 | 192.168.56.23 |
停止从节点
[root@mongodb2 ~]# ps -ef|grep mongodb
root 19412 1 1 07:37 ? 00:01:03 mongod -f /usr/local/mongodb/mongodb.conf
root 19524 6251 0 07:37 pts/1 00:00:02 mongosh mongodb://192.168.5
root 24266 23338 0 08:58 pts/2 00:00:00 grep --color=auto mongodb
[root@mongodb2 ~]# kill -9 19412
在主节点可以查看到,从节点无法连接
修改从节点ip
[root@mongodb2 network-scripts]# vim ifcfg-enp0s3
IPADDR=192.168.56.22
[root@mongodb2 mongodb]# vim mongodb.conf
bind_ip=192.168.56.22
[root@mongodb2 mongodb]# systemctl restart network
[root@mongodb2 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 25639
child process started successfully, parent exiting
启动完成
修改主节点配置从节点
bsg [direct: primary] test> cfg = rs.conf()
{
_id: 'bsg',
version: 4,
term: 4,
members: [
{
_id: 0,
host: '192.168.56.11:27011',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
{
_id: 1,
host: '192.168.56.12:27011',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
{
_id: 2,
host: '192.168.56.13:27011',
arbiterOnly: true,
buildIndexes: true,
hidden: false,
priority: 0,
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('66f4d3afe2aeecd4d89b5ea0')
}
}
###注意这里的members[1] 中的数值1是member数组的序号
bsg [direct: primary] test> cfg.members[1].host="192.168.56.22:27011"
192.168.56.22:27011
bsg [direct: primary] test> rs.reconfig(cfg)
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727399667, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727399667, i: 1 })
}
bsg [direct: primary] test> rs.status()
{
set: 'bsg',
date: ISODate('2024-09-27T01:14:42.606Z'),
myState: 1,
term: Long('4'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
lastCommittedWallTime: ISODate('2024-09-27T01:14:35.560Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
appliedOpTime: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
durableOpTime: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
lastAppliedWallTime: ISODate('2024-09-27T01:14:35.560Z'),
lastDurableWallTime: ISODate('2024-09-27T01:14:35.560Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1727398710, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-09-26T10:12:55.526Z'),
electionTerm: Long('4'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1727345521, i: 1 }), t: Long('3') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1727345531, i: 1 }), t: Long('3') },
numVotesNeeded: 2,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
numCatchUpOps: Long('0'),
newTermStartDate: ISODate('2024-09-26T10:12:56.607Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-09-26T10:13:01.620Z')
},
members: [
{
_id: 0,
name: '192.168.56.11:27011',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 57519,
optime: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
optimeDate: ISODate('2024-09-27T01:14:35.000Z'),
lastAppliedWallTime: ISODate('2024-09-27T01:14:35.560Z'),
lastDurableWallTime: ISODate('2024-09-27T01:14:35.560Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1727345575, i: 1 }),
electionDate: ISODate('2024-09-26T10:12:55.000Z'),
configVersion: 5,
configTerm: 4,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '192.168.56.22:27011',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 14,
optime: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
optimeDurable: { ts: Timestamp({ t: 1727399675, i: 2 }), t: Long('4') },
optimeDate: ISODate('2024-09-27T01:14:35.000Z'),
optimeDurableDate: ISODate('2024-09-27T01:14:35.000Z'),
lastAppliedWallTime: ISODate('2024-09-27T01:14:35.560Z'),
lastDurableWallTime: ISODate('2024-09-27T01:14:35.560Z'),
lastHeartbeat: ISODate('2024-09-27T01:14:41.819Z'),
lastHeartbeatRecv: ISODate('2024-09-27T01:14:40.831Z'),
pingMs: Long('1'),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.56.11:27011',
syncSourceId: 0,
infoMessage: '',
configVersion: 5,
configTerm: 4
},
{
_id: 2,
name: '192.168.56.13:27011',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 53512,
lastHeartbeat: ISODate('2024-09-27T01:14:41.706Z'),
lastHeartbeatRecv: ISODate('2024-09-27T01:14:41.705Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 5,
configTerm: 4
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727399675, i: 2 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727399675, i: 2 })
}
修改仲裁节点ip
按照和从节点的操作相同
1、停止仲裁节点
2、修改ip和配置文件,重启网络
3、启动mongodb
4、主节点进行修改
cfg = rs.conf()
cfg.members[2].host = "192.168.56.23:27011"
rs.reconfig(cfg)
停止主节点
关闭主节点,副本集选举mongodb2成为主节点。
[root@mongodb1 ~]# ps -ef|grep mongodb
root 18839 1 1 07:36 ? 00:01:25 mongod -f /usr/local/mongodb/mongodb.conf
root 18991 14566 0 07:37 pts/0 00:00:02 mongosh mongodb://192.168.5
root 25505 22839 0 09:26 pts/2 00:00:00 grep --color=auto mongodb
[root@mongodb1 ~]# kill -9 18839
按照和从节点的操作相同
1、停止仲裁节点
2、修改ip和配置文件,重启网络
3、启动mongodb
4、主节点进行修改
cfg = rs.conf()
cfg.members[0].host = "192.168.56.21:27011"
rs.reconfig(cfg, {force: true})
查看集群状态
bsg [direct: primary] test> rs.status()
{
set: 'bsg',
date: ISODate('2024-09-27T02:29:41.761Z'),
myState: 1,
term: Long('5'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
lastCommittedWallTime: ISODate('2024-09-27T02:29:35.653Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
appliedOpTime: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
durableOpTime: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
lastAppliedWallTime: ISODate('2024-09-27T02:29:35.653Z'),
lastDurableWallTime: ISODate('2024-09-27T02:29:35.653Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1727404175, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-09-27T01:26:55.282Z'),
electionTerm: Long('5'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1727400400, i: 1 }), t: Long('4') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1727400400, i: 1 }), t: Long('4') },
numVotesNeeded: 2,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
numCatchUpOps: Long('0'),
newTermStartDate: ISODate('2024-09-27T01:26:55.304Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-09-27T02:08:22.123Z')
},
members: [
{
_id: 0,
name: '192.168.56.21:27011',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 1280,
optime: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
optimeDurable: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
optimeDate: ISODate('2024-09-27T02:29:35.000Z'),
optimeDurableDate: ISODate('2024-09-27T02:29:35.000Z'),
lastAppliedWallTime: ISODate('2024-09-27T02:29:35.653Z'),
lastDurableWallTime: ISODate('2024-09-27T02:29:35.653Z'),
lastHeartbeat: ISODate('2024-09-27T02:29:40.554Z'),
lastHeartbeatRecv: ISODate('2024-09-27T02:29:41.598Z'),
pingMs: Long('1'),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.56.22:27011',
syncSourceId: 1,
infoMessage: '',
configVersion: 37410,
configTerm: -1
},
{
_id: 1,
name: '192.168.56.22:27011',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 4807,
optime: { ts: Timestamp({ t: 1727404175, i: 1 }), t: Long('5') },
optimeDate: ISODate('2024-09-27T02:29:35.000Z'),
lastAppliedWallTime: ISODate('2024-09-27T02:29:35.653Z'),
lastDurableWallTime: ISODate('2024-09-27T02:29:35.653Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1727400415, i: 1 }),
electionDate: ISODate('2024-09-27T01:26:55.000Z'),
configVersion: 37410,
configTerm: -1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 2,
name: '192.168.56.23:27011',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 3879,
lastHeartbeat: ISODate('2024-09-27T02:29:40.531Z'),
lastHeartbeatRecv: ISODate('2024-09-27T02:29:40.724Z'),
pingMs: Long('1'),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 37410,
configTerm: -1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727404175, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727404175, i: 1 })
}
集群修改ip-方案二(同时更改所有主机名)
节点 | 原ip | 新ip |
---|---|---|
primary | 192.168.56.11 | 192.168.56.111 |
secondary | 192.168.56.12 | 192.168.56.112 |
arbiter | 192.168.56.13 | 192.168.56.113 |
停止副本集中的所有节点
停止顺序:仲裁-从节点-主节点
将[root@mongodb3 ~]# ps -ef|grep mongodb
root 25464 1 1 09:24 ? 00:00:45 mongod -f /usr/local/mongodb/mongodb.conf
root 29728 25403 0 10:35 pts/4 00:00:00 grep --color=auto mongodb
[root@mongodb3 ~]# kill -9 25464
所有节点都执行
修改所有节点ip
[root@mongodb1 network-scripts]# vim ifcfg-enp0s3
IPADDR=192.168.56.111
[root@mongodb1 mongodb]# vim mongodb.conf
bind_ip=192.168.56.111
#replSet=bsg #单机模式启动
[root@mongodb1 mongodb]# systemctl restart network
[root@mongodb1 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3451
child process started successfully, parent exiting
所有节点都执行
编辑副本集配置
1、切换到 local 数据库。
[root@mongodb1 ~]# mongosh 192.168.56.111:27011
test> use localuse local
2、为配置文档创建 JavaScript 变量。修改 _id 字段的值以与您的副本集相匹配。
local> cfg = db.system.replset.findOne( { "_id": "bsg" } )
3、为副本集的每个成员提供新的主机名和端口。修改主机名和端口,确保它们与您的副本集相匹配。
local> cfg.members[0].host = "192.168.56.111:27011"
local> cfg.members[1].host = "192.168.56.112:27011"
local> cfg.members[2].host = "192.168.56.113:27011"
4、更新 system.replset 集合中的主机名和端口:
local> db.system.replset.updateOne( { "_id": "bsg" }, { $set: cfg } )
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
5、验证变更:
local> db.system.replset.find( {}, { "members.host": 1 } )
[
{
_id: 'bsg',
members: [
{ host: '192.168.56.111:27011' },
{ host: '192.168.56.112:27011' },
{ host: '192.168.56.113:27011' }
]
}
]
6、停止所有节点上的mongodb
将[root@mongodb1 ~]# ps -ef|grep mongodb
root 25464 1 1 09:24 ? 00:00:45 mongod -f /usr/local/mongodb/mongodb.conf
root 29728 25403 0 10:35 pts/4 00:00:00 grep --color=auto mongodb
[root@mongodb1 ~]# kill -9 25464
所有节点都执行
启动集群
[root@mongodb1 mongodb]# vim mongodb.conf
bind_ip=192.168.56.111
replSet=bsg #将注释去掉
[root@mongodb1 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2871
child process started successfully, parent exiting
所有节点都执行
登录检查
bsg [direct: primary] test> rs.status()
{
set: 'bsg',
date: ISODate('2024-09-27T06:14:10.102Z'),
myState: 1,
term: Long('6'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
lastCommittedWallTime: ISODate('2024-09-27T06:14:09.484Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
appliedOpTime: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
durableOpTime: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
lastAppliedWallTime: ISODate('2024-09-27T06:14:09.484Z'),
lastDurableWallTime: ISODate('2024-09-27T06:14:09.484Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1727417629, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-09-27T03:45:08.453Z'),
electionTerm: Long('6'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long('-1') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1727404565, i: 1 }), t: Long('5') },
numVotesNeeded: 2,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
numCatchUpOps: Long('0'),
newTermStartDate: ISODate('2024-09-27T03:45:08.617Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-09-27T03:45:08.639Z')
},
members: [
{
_id: 0,
name: '192.168.56.111:27011',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 9382,
optime: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
optimeDate: ISODate('2024-09-27T06:14:09.000Z'),
lastAppliedWallTime: ISODate('2024-09-27T06:14:09.484Z'),
lastDurableWallTime: ISODate('2024-09-27T06:14:09.484Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1727408708, i: 1 }),
electionDate: ISODate('2024-09-27T03:45:08.000Z'),
configVersion: 91908,
configTerm: -1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '192.168.56.112:27011',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 8949,
optime: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
optimeDurable: { ts: Timestamp({ t: 1727417649, i: 1 }), t: Long('6') },
optimeDate: ISODate('2024-09-27T06:14:09.000Z'),
optimeDurableDate: ISODate('2024-09-27T06:14:09.000Z'),
lastAppliedWallTime: ISODate('2024-09-27T06:14:09.484Z'),
lastDurableWallTime: ISODate('2024-09-27T06:14:09.484Z'),
lastHeartbeat: ISODate('2024-09-27T06:14:09.674Z'),
lastHeartbeatRecv: ISODate('2024-09-27T06:14:09.822Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.56.111:27011',
syncSourceId: 0,
infoMessage: '',
configVersion: 91908,
configTerm: -1
},
{
_id: 2,
name: '192.168.56.113:27011',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 8947,
lastHeartbeat: ISODate('2024-09-27T06:14:09.674Z'),
lastHeartbeatRecv: ISODate('2024-09-27T06:14:09.674Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 91908,
configTerm: -1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1727417649, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1727417649, i: 1 })
}
标签:副本,IP,09,Long,2024,mongodb,ISODate,Timestamp,mongdb7
From: https://blog.csdn.net/kkkbbbsg/article/details/142595502