下面我们来就举例MongoDB库的副本集群节点扩容和收缩
[root@localhost mongo_clusetr]# cp -rp mongodb_28017/ mongodb_28010
[root@localhost mongo_clusetr]# cp -rp /data/mongo_clusetr/mongodb_27017/ /data/mongo_clusetr/mongodb_28010
[root@localhost mongo_clusetr]# sed -i 's#28017#28010#g' mongodb_28010/conf/mongodb.yml
[root@localhost mongo_clusetr]# mongodb/bin/mongod -f mongodb_28010/conf/mongodb.yml
[root@localhost mongo_clusetr]# mongodb/bin/mongo db:28010
dba1:PRIMARY> rs.add("192.168.40.4:28010")
可以看到很快就切换到了集群的状态
在主库插入数据在新增加的从库查询
dba1:PRIMARY> db.test.insert({"name":"zhangsan","age":12})
WriteResult({ "nInserted" : 1 })
dba1:PRIMARY>
dba1:SECONDARY> db.test.find()
{ "_id" : ObjectId("637c826c7e632a69cc9d80c3"), "name" : "zhangsan", "age" : 11 }
{ "_id" : ObjectId("637c828d7e632a69cc9d80c4"), "name" : "zhangsan", "age" : 12 }
剔除和扩容都是非常简单,只要注意一些命令不要操作失误
dba1:PRIMARY> rs.remove("192.168.40.4:28010")
{
"ok" : 1,
"operationTime" : Timestamp(1669104604, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1669104604, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
被剔除的集群默认还是会占用资源,我们把他给关闭掉
[root@localhost mongo_clusetr]# mongodb/bin/mongod -f mongodb_28010/conf/mongodb.yml --shutdown
可以看到id_3的端口已经消失了,节点被剔除
删除数据文件添加仲裁节点,仲裁节点不参与选举只会投票
[root@localhost mongodb_28010]# rm -fr /data/mongo_clusetr/mongodb_28010/data/
[root@localhost mongo_clusetr]# mongodb/bin/mongod -f mongodb_28010/conf/mongodb.yml
在主库操作
rs.addArb("192.168.40.4:28010")
再次登陆之后可以看到已经变成了仲裁节点
dba1:ARBITER>