[英]Error when try to connect to Replica Set in Mongo
When I try to connect to mongo replica set in AWS I get this error: 当我尝试连接到AWS中的mongo副本集时,出现此错误:
slavenode:27017: [Errno -2] Name or service not known,ip-XXX-XX-XX-XX:27017: [Errno -2] Name or service not known
slavenode:27017:[Errno -2]名称或服务未知,ip-XXX-XX-XX-XX:27017:[Errno -2]名称或服务未知
(where XXX-XX.. corresponds to my actual ip address) (其中XXX-XX ..对应于我的实际IP地址)
The code to connect is shown below: 连接代码如下所示:
client = MongoClient("mongodb://Master-PublicIP:27017,Slave-PublicIP:27017/myFirstDB?replicaSet=rs0")
db = client.myFirstDB
try:
db.command("serverStatus")
except Exception as e:
print(e)
else:
print("You are connected!")
client.close()
(where in Master-PublicIP and Slave-PublicIP I have the actual IPv4 Public IP's from AWS console) (在Master-PublicIP和Slave-PublicIP中,我从AWS控制台获得了实际的IPv4公共IP)
I have already a replica set and the configuration is: 我已经有一个副本集,其配置为:
rs0:PRIMARY> rs.conf() rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "ip-XXX-XX-XX-XXX:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "SlaveNode:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
} }
I have create the /data/db in PRIMARY and the /data/db1 in SECONDARY and I have give the proper ownership with sudo chmod -R 755 /data/db
我已经在PRIMARY中创建了/ data / db,在SECONDARY中创建了/ data / db1,并且使用
sudo chmod -R 755 /data/db
赋予了适当的所有权。
My MongoDB version is 3.0.15. 我的MongoDB版本是3.0.15。 Is anyone know what is going wrong?
有人知道出什么事了吗?
Thanks in advance. 提前致谢。
Have you tried removing the myFirstDB
from within the MongoClient()
您是否尝试
myFirstDB
MongoClient()
删除myFirstDB
MongoClient("mongodb://Master-PublicIP:27017,Slave-PublicIP:27017/?replicaSet=rs0")
Because your next line then specifies which db you want to use 因为您的下一行会指定您要使用的数据库
db = client.myFirstDB
Or I think you can specify the db by putting a dot
after the closing brace on the MongoClient()
或者我认为您可以通过在
MongoClient()
上的大括号后面放置一个dot
来指定数据库
MongoClient("mongodb://Master-PublicIP:27017,Slave-PublicIP:27017/?replicaSet=rs0").myFirstDB
I manage to solve the problem. 我设法解决了这个问题。 As @N3i1 suggests in commnets, I use the Public DNS (IPv4).
就像@ N3i1在通讯中所建议的那样,我使用公共DNS(IPv4)。 There was an issue with the hosts that I had declare in /etc/hosts.
我在/ etc / hosts中声明的主机存在问题。
In this file I had define the ips of master/ slaves with some names. 在这个文件中,我用一些名称定义了主/从机的ip。 For some reason this didn't work.
由于某种原因,这不起作用。 I delete them and then I reconfigure the replica set configuration.
我删除它们,然后重新配置副本集配置。
In PRIMARY in mongo shell I did: 在mongo shell的PRIMARY中,我做到了:
cfg = {"_id" : "rs0", "members" : [{"_id" : 0,"host" : "Public DNS (IPv4):27017"},{"_id" : 1,"host" : "Public DNS (IPv4):27017"}]}
rs.reconfig(cfg,{force: true});
Then I connect in the replica set with python with: 然后我用python连接副本集:
MongoClient("mongodb://Public DNS (IPv4):27017,Public DNS (IPv4):27017/?replicaSet=rs0")
Of course change the Public DNS (IPv4) adresses with yours. 当然,请更改您的公共DNS(IPv4)地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.