mongo添加新节点
1、创建一个单节点的副本集,并写入一些测试数据
安装方法参考:https://www.cnblogs.com/nanxiang/p/14228307.html
2、
新开一台虚拟机(IP:172.16.1.11),安装mongo,但不要启动。
从旧节点拷贝 keyfile 文件到新节点
scp /data/mongodb/security/mongodb-keyfile root@172.16.1.11:/data/mongodb/security/mongodb-keyfile
3、修改配置文件,
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
systemLog:
quiet: true
destination: file
logAppend: true
path: /data/mongodb/log/mongod.log
processManagement:
fork: true
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
security:
keyFile: /data/mongodb/security/mongodb-keyfile
operationProfiling:
slowOpThresholdMs: 1000
mode: slowOp
4、启动数据库
mongod --config /etc/mongod.conf
5、
登录主节点数据库,执行
rs.add('172.16.1.11:27017')
6、关闭主节点,primary节点会自动飘到 172.16.1.11 机器
7、去掉集群中某个节点
rs0:PRIMARY> rs.remove('172.16.1.10:27017')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1647249804, 1),
"signature" : {
"hash" : BinData(0,"2gG2fBrjEj+EnepUjOq08wNshrk="),
"keyId" : NumberLong("7071487928572051460")
}
},
"operationTime" : Timestamp(1647249804, 1)
}
8、去除掉的节点状态是 OTHER 状态,现在独立成新集群
注意:副本集名称【rs0】不能改变。
rs0:OTHER> cfg={_id:"rs0",members:[{_id:1,host:"172.16.1.10:27017",priority:1}]}
{
"_id" : "rs0",
"members" : [
{
"_id" : 1,
"host" : "172.16.1.10:27017",
"priority" : 1
}
]
}
rs0:OTHER> rs.reconfig(cfg,{force:true});
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1647249804, 1),
"signature" : {
"hash" : BinData(0,"2gG2fBrjEj+EnepUjOq08wNshrk="),
"keyId" : NumberLong("7071487928572051460")
}
},
"operationTime" : Timestamp(1647249804, 1)
}
rs0:SECONDARY> show dbs
admin 0.000GB
ceshi 0.000GB
config 0.000GB
local 0.000GB
rs0:PRIMARY>