Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues connecting to Mongo Atlas #116

Open
gillchristian opened this issue Apr 26, 2020 · 6 comments
Open

Issues connecting to Mongo Atlas #116

gillchristian opened this issue Apr 26, 2020 · 6 comments

Comments

@gillchristian
Copy link

I'm trying to connect to Mongo Atlas but to no success. Getting the following error:

Network.BSD.getHostByName: does not exist (no such host entry)

I've tried the suggestions from #84 but it seems like the issue is different.

This is how I connect:

import qualified Database.MongoDB as DB
import qualified Database.MongoDB.Transport.Tls as DBTLS

runDb :: DB.Action IO a -> App a
runDb action = do
  -- ...
  pipe <- liftIO $ case env of
    Production -> DBTLS.connect (dbHostname dbConf) (DB.PortNumber 27017)
    Development -> DB.connect $ DB.host $ dbHostname dbConf
  -- ...

Code is open source, rest can be found here.

On Development mode connecting to a local db it works fine (dbHostname: "127.0.0.1").

But when trying to connect to Mongo Atlas, either by using DBTLS.connect or DB.connect it does not work. Either by trying with the full connect URL provided by Mongo Atlas or only the host

dbHostname: "mongodb+srv://<user>:<password>@xxxxxx-xxxxx.gcp.mongodb.net/test?retryWrites=true&w=majority"

dbHostname: "xxxxxx-xxxxx.gcp.mongodb.net"

As error is from Network.BSD.getHostByName I'm not sure if issue should be opened here or not.

@dreamsmasher
Copy link

Getting the same error, it's not very clear how to connect to MongoDB Atlas. I've tried manually splitting the shard urls into a (ReplicaSetName, [Host]) tuple and making any sort of connection with (primary . openReplicateSet), but to no avail. Any help would be appreciated.

@why-not-try-calmer
Copy link
Contributor

Same here. The API docs don't whisper a word about MongoDB Atlas. The user is left to wonder whether it should be done using the experimental Tls module, or by some other means.

@why-not-try-calmer
Copy link
Contributor

why-not-try-calmer commented May 9, 2021

Okay everyone, here is how I finally succeeded:

{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE LambdaCase           #-}

import           Database.MongoDB
import qualified Database.MongoDB.Transport.Tls as DbTLS

shard = "..."    -- please note that you need to you use the reference to the * PRIMARY * shard in the target cluster, which often satisfies the following numbering pattern 'cluster0-shard-00-02.XXXXX.mongodb.net'
user = "..."
password = "..."
run_some_db_action = do -- the actual action you want to do
    ...
database_name = "..." -- the actual database against which the action should be registered

main = try (DbTLS.connect shard (PortNumber 27017)) >>= \case
        Left e -> let e'= e :: SomeException in print ("Error while trying to connect: " ++ show e)
        Right pipe -> do
            isAuthorized <- access pipe UnconfirmedWrites "admin" $ auth user password -- "admin" is just used once for authentication; notice that 'UnconfirmedWrites' might not be required, I didn't survey all possible combinations of settings here;
            if isAuthorized then access pipe master database_name run_some_db_action else throwIO $ userError "Failed to authenticate."
            close pipe

@KovaxG
Copy link

KovaxG commented Jul 18, 2022

While this worked for a while, the primary shard changed, and now for some reason I can insert documents, but I always get zero documents back whenever I do a query.

@KovaxG
Copy link

KovaxG commented Jul 18, 2022

I actually managed to connect to the Atlas by using openReplicaSetTLS

replicaSet <- Mongo.openReplicaSetTLS
    ( Text.pack "atlas-abcd123-shard-0"
    , [ Mongo.host "something-shard-00-00.mfbis.mongodb.net"
      , Mongo.host "something-shard-00-01.mfbis.mongodb.net"
      , Mongo.host "something-shard-00-02.mfbis.mongodb.net"
      ]
    )
pipe <- Mongo.primary replicaSet
authenticated <- Mongo.access pipe Mongo.master Mongo.admin (Mongo.auth mongoUser mongoPass

However I still have this weird problem that queries continually return nothing.

@why-not-try-calmer
Copy link
Contributor

why-not-try-calmer commented Jul 19, 2022

@KovaxG Since this is no longer problem with connecting but with empty returned documents, mind continuing this discussion here? #131 That would also help me narrow down on the culprit(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants