解决:mongodb的rs.add()报错can'tuselocalhostinreplsetmembernames
这个问题多出现在虚拟机单机多实例的情况下,并且rs.initiate()配置里使用了localhost。
通过错误提示可以看出不能在repl set成员名字中使用localhost
当前文章:解决:mongodb的rs.add()报错can'tuselocalhostinreplsetmembernames
标题路径:http://lswzjz.com/article/gpgdij.html
如下面的配置
在一个虚拟机启动了多个mongod实例,在不同端口监听
mongod --replSet rs1 --keyFile /data/k1 --fork --port 27017 --dbpath /data/db1 --logpath /data/log/r1.log --logappend
mongod --replSet rs1 --keyFile /data/k2 --fork --port 27018 --dbpath /data/db2 --logpath /data/log/r2.log --logappend
mongod --replSet rs1 --keyFile /data/k3 --fork --port 27019 --dbpath /data/db3 --logpath /data/log/r3.log --logappend
使用下面的配置初始化
[root@vm3 ~]# mongo
MongoDB shell version: 2.2.2
connecting to: test
rs1:PRIMARY>rs.initiate({_id:'rs1',members:[{_id:0,host:'localhost:27017',priority:1},{_id:1,host:'localhost:27018'},{_id:2,host:'localhost:27019'}]})
MongoDB shell version: 2.2.2
connecting to: test
rs1:PRIMARY>rs.initiate({_id:'rs1',members:[{_id:0,host:'localhost:27017',priority:1},{_id:1,host:'localhost:27018'},{_id:2,host:'localhost:27019'}]})
当我们想添加其他机器的mongodb实例进入集群时,问题来了,出现下面错误:
[root@vm3 ~]# mongo
MongoDB shell version: 2.2.2
connecting to: test
rs1:PRIMARY> rs.add('192.168.18.241:27020')
{
"errmsg" : "exception: can't use localhost in repl set member names except when using it for all members",
"code" : 13393,
"ok" : 0
}
MongoDB shell version: 2.2.2
connecting to: test
rs1:PRIMARY> rs.add('192.168.18.241:27020')
{
"errmsg" : "exception: can't use localhost in repl set member names except when using it for all members",
"code" : 13393,
"ok" : 0
}
我们自己想想也是有道理的,因为当这个配置传输到别的机器实例后,那台机器会查找配置里的master,
而master的配置是 localhost:27017(初始时由priority:1决定),相当于master在本机,而本机却没有master,所以添加失败
一般在生产环境不容易遇到这个问题
我建议使用内网ip来代替localhost,如192.168.xxx.xxx,也不要用127.0.0.1,建议配置如下:
rs.initiate({_id:'rs1',members:[{_id:0,host:'192.168.18.240:27017',priority:1},{_id:1,host:'192.168.18.240:27018'},{_id:2,host:'192.168.18.240:27019'}]})
当前文章:解决:mongodb的rs.add()报错can'tuselocalhostinreplsetmembernames
标题路径:http://lswzjz.com/article/gpgdij.html