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

MongoDB not able to connect in rocket.rs #1617

Closed
mooyg opened this issue Apr 26, 2021 · 5 comments
Closed

MongoDB not able to connect in rocket.rs #1617

mooyg opened this issue Apr 26, 2021 · 5 comments
Labels
triage A bug report being investigated upstream An unresolvable issue: an upstream dependency bug

Comments

@mooyg
Copy link

mooyg commented Apr 26, 2021

So I am trying to establish mongodb connection through the rocket_contrib/rocket. I looked into the docs https://rocket.rs/v0.4/guide/state/#databases did what was necessary

[global.databases]
mongodb_logs = { url = "mongodb://Username:password@cluster0-shard-00-00.vegb1.mongodb.net:27017,cluster0-shard-00-01.vegb1.mongodb.net:27017,cluster0-shard-00-02.vegb1.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority" }

this is my Rocket.toml configuration as you can see I am using old connection version cause it complained for url to start with "mongodb://" and it's normally "mongodb+srv://" but this throws this error

🔧 Configured for development.
    => address: localhost
    => port: 8000
    => log: normal
    => workers: 12
    => secret key: generated
    => limits: forms = 32KiB
    => keep-alive: 5s
    => read timeout: 5s
    => write timeout: 5s
    => tls: disabled
    => [extra] databases: { mongodb_logs = { url = "mongodb://username:password@cluster0-shard-00-00.vegb1.mongodb.net:27017,cluster0-shard-00-01.vegb1.mongodb.net:27017,cluster0-shard-00-02.vegb1.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority" } }
🛰  Mounting /:
    => GET / (index)
    => POST /new/message application/json (new_message)
Error: Failed to initialize pool for 'mongodb_logs'
    => Error: PoolError(Error(None))
Error: Rocket failed to launch due to failing fairings:
    => 'mongodb_logs' Database Pool
thread 'main' panicked at 'launch fairing failure', /workspace/.cargo/registry/src/github.com-1ecc6299db9ec823/rocket-0.4.7/src/error.rs:214:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

When trying the latest version whose URL starts with mongodb+srv:// and removing the +srv in it I get the same error so I don't really know what's this about but any help would be helpful.

@jebrosen
Copy link
Collaborator

I am confused by the log output because it's for the mongodb:// instead of mongodb+srv://; did you get the two outputs switched around?

The log does not include any reason for the failure, but in my experience the reason is usually printed somewhere. PoolError(Error(None)) is not useful; if Rocket is accidentally suppressing error details we should fix that, but unfortunately in this case I think mongodb or r2d2_mongodb is "hiding" them. Rocket 0.4 is also using an older version of the mongodb rust driver, which might not support the same syntax or all of the same parameters.

Does this same connection string work fine with other mongodb drivers?

The next step here is to try using the same versions of mongodb or r2d2_mongodb, without Rocket, to find out where the actual problem is. In case it's actually a configuration or connection issue instead of a syntax issue, is there a public mongodb cluster I or someone else could try connecting to with the same parameters, or a straightforward way to set one up locally?

@jebrosen jebrosen added triage A bug report being investigated upstream An unresolvable issue: an upstream dependency bug labels Apr 26, 2021
@mooyg
Copy link
Author

mooyg commented Apr 27, 2021

I tried working without Rocket with mongodb rust driver and it worked. I thought they might be using older version that's why I tried the connection string which was for older version and started with "mongodb://" as the error first told me when I tried using the newer connection string "mongodb+srv://". So basically I am pretty sure it's rocket but don't know how to fix it. I am new to rust too 😄 (I am not using local mongodb thing btw)

@jebrosen
Copy link
Collaborator

the connection string which was for older version and started with "mongodb://" as the error first told me when I tried using the newer connection string "mongodb+srv://"

I found documentation on this newer syntax at https://docs.mongodb.com/manual/reference/connection-string/#std-label-connections-dns-seedlist. The meaning of the names in the connection string are completely different, so simply removing +srv from the URI is not expected to work. To properly convert a mongodb+srv URI to a mongodb:// URI, you would need to first do DNS record lookups to find each DNS seed name's corresponding target name in a DNS records, and compose a new connection string with these target names. However, this whole process seemingly defeats the purpose of using mongodb+srv in the first place.

Rocket 0.4's database support is hardcoded around the use of r2d2, and the mongodb adapter r2d2_mongodb does not appear to be actively maintained and is using the older mongodb version without support for mongodb+srv. Unfortunately, we can't upgrade the version in Rocket 0.4 without breaking existing users whose code is written against the old API. Normally I would advocate for side-by-side support for the latest version of mongodb, but since other features in the latest official mongodb driver have made r2d2_mongodb redundant this is not so straightforward.

This whole situation will be fixed, in a more future-compatible way, by #1117, by directly supporting the latest mongodb driver and its built-in connection pooling. But with Rocket 0.4 today, I recommend manually using the latest driver which supports mongodb+srv syntax. In particular, you can put a mongodb::sync::Client in managed state.

@mooyg
Copy link
Author

mooyg commented Apr 30, 2021

Okay I guess I'll proceed with that thank you so much :D

@SergioBenitez
Copy link
Member

Closing as an upstream issue no longer applicable to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage A bug report being investigated upstream An unresolvable issue: an upstream dependency bug
Projects
None yet
Development

No branches or pull requests

3 participants