温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

【MongoDB学习笔记32】MongoDB修改副本集配置

发布时间:2020-07-23 18:36:12 来源:网络 阅读:3040 作者:StanlyCheng 栏目:MongoDB数据库

一、删除副本集成员

spock:PRIMARY> rs.config()   
{    
        "_id" : "spock",    
        "version" : 1,    
        "members" : [    
                {    
                        "_id" : 0,    
                        "host" : "192.168.1.112:27017"    
                },    
                {    
                        "_id" : 1,    
                        "host" : "192.168.1.113:27017"    
                }    
        ]    
}    
spock:PRIMARY> rs.remove("192.168.1.113:27017")    
{    
        "errmsg" : "exception: can't find self in new replset config",    
        "code" : 13433,    
        "ok" : 0    
}    
spock:PRIMARY> rs.remove("192.168.1.112:27017")    
2015-02-02T21:59:13.303+0800 DBClientCursor::init call() failed    
2015-02-02T21:59:13.304+0800 Error: error doing query: failed at src/mongo/shell/query.js:81    
2015-02-02T21:59:13.305+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed    
2015-02-02T21:59:13.308+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok    
spock:PRIMARY> rs.config()    
{    
        "_id" : "spock",    
        "version" : 2,    
        "members" : [    
                {    
                        "_id" : 1,    
                        "host" : "192.168.1.113:27017"    
                }    
        ]    
}

   

二、添加副本集成员  

spock:PRIMARY> rs.add("192.168.1.112:27017")    
{ "ok" : 1 }    
spock:PRIMARY> rs.config()    
{    
        "_id" : "spock",    
        "version" : 3,    
        "members" : [    
                {    
                        "_id" : 1,    
                        "host" : "192.168.1.113:27017"    
                },    
                {    
                        "_id" : 2,    
                        "host" : "192.168.1.112:27017"    
                }    
        ]    
}

   
   
从上述过程来看,配置文档中version字段都会自增,它的初始值为1;

 

三、或者通过rs.reconfig()函数修改副本集

例如,将host用主机名来替代ip地址

[root@localhost ~]# ping host113   
PING host113 (192.168.1.113) 56(84) bytes of data.    
64 bytes from host113 (192.168.1.113): icmp_seq=1 ttl=64 time=0.060 ms    
64 bytes from host113 (192.168.1.113): icmp_seq=2 ttl=64 time=0.030 ms    
64 bytes from host113 (192.168.1.113): icmp_seq=3 ttl=64 time=0.026 ms    
64 bytes from host113 (192.168.1.113): icmp_seq=4 ttl=64 time=1.04 ms    
--- host113 ping statistics ---    
4 packets transmitted, 4 received, 0% packet loss, time 3001ms    
rtt min/avg/max/mdev = 0.026/0.289/1.041/0.434 ms    
[root@localhost ~]# mongo    
MongoDB shell version: 2.6.7    
connecting to: test    
spock:PRIMARY> rs.config()    
{            
    "_id" : "spock",            
    "version" : 3,            
    "members" : [                    
                    { "_id" : 1, "host" : "192.168.1.113:27017"},
                    { "_id" : 2, "host" : "192.168.1.112:27017"}            
                ]    
 }    
spock:PRIMARY> var config=rs.config()   
spock:PRIMARY> config.members[0].host="host113:27017"    
host113:27017    
spock:PRIMARY> rs.reconfig(config)    
2015-02-02T22:21:22.793+0800 DBClientCursor::init call() failed    
2015-02-02T22:21:22.795+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) faile
2015-02-02T22:21:22.797+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok    
reconnected to server after rs command (which is normal)
spock:PRIMARY> rs.config()   
{            
    "_id" : "spock",            
    "version" : 4,            
    "members" : [                    
            { "_id" : 1,"host" : "host113:27017"},
            { "_id" : 2, "host" : "192.168.1.112:27017"}            
                ]    
}    
spock:PRIMARY>

 

对于复杂的副本集配置,rs.reconfig函数修改比rs.add和rs.remove更有用;

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI