简体   繁体   中英

How to connect to mongo database in docker from another machine using ruby code

In my ruby code, I'm trying to use Mongo::Client.new("mongodb://#{uname}:#{passwd}@#{host}:#{port}/#{db}") to connect to a mongo database on another machine's docker in Kubernetes cluster.

But the result says that

getaddrinfo: Name or service not known. #<Mongo::Client:0x16434420 cluster=#<Cluster topology=ReplicaSetNoPrimary[ip:port,mongo-0.mongo-hs.db.svc.cluster.local:27017,mongo-1.mongo-hs.db.svc.cluster.local:27017,mongo-2.mongo-hs.db.svc.cluster.local:27017,name=MainRepSet] servers=[#<Server address=mongo-0.mongo-hs.db.svc.cluster.local:27017 UNKNOWN>,#<Server address=mongo-1.mongo-hs.db.svc.cluster.local:27017 UNKNOWN>,#<Server address=mongo-2.mongo-hs.db.svc.cluster.local:27017 UNKNOWN>]>>

However, when I use pymongo in a python script. I can connect to the database.

Is there a way for ruby code to connect to the database directly?

  1. In an irb console, execute the call to create the client that you have posted. The default logging level of the driver is info which will give you a lot of information about what is going on. Use that information to further debug and include it in your question.

  2. If pymongo can connect and ruby driver cannot, one possibility is your dns is misconfigured in one or both of the containers. Ruby driver requires your system to be able to resolve the hostnames given to the driver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM