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> 

posted on   柴米油盐酱醋  阅读(624)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示