spring boot app unable to connect to mongodb

mattmHOBBY

a year ago

I have a SpringBoot app deployed on Railway and it works fine when I connect it to Mongo hosted on MongoDB's own cloud service.
I can interact with the db (insert, read, update, delete) documents no problem.

I then spin up MongoDB running on Railway and try to point my SB app to it.
It is never able to get connected (my app won't even startup).

The way I point my SB app to any Mongo DB is the standard way which is to set a property named
spring.data.mongodb.uri
with the URI to the DB.

That is what works with the MongoDB running via Mongo's own cloud service.
What am I doing wrong when I try to point my app to the Mongo running on Railway?

For the value of the spring.data.mongodb.uri prop I am passing to my app, I have tried the literal Mongo URI given under the MongoDB card (I tried both the private one and the public one).
I have also tried the cool variable replacement syntax as the value:

${{MongoDB.MONGOPRIVATEURL}}/test

but I always get:

2024-01-16T20:40:51.204Z INFO 7 --- [dbME] [ngodb.net:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server ac-a6opmgx-shard-00-01.aibjbyk.mongodb.net:27017
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
at com.mongodb.internal.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:728) ~[mongodb-driver-core-4.11.1.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessageWithAdditionalTimeout(InternalStreamConnection.java:606) ~[mongodb-driver-core-4.11.1.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:451) ~[mongodb-driver-core-4.11.1.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.receive(InternalStreamConnection.java:404) ~[mongodb-driver-core-4.11.1.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:224) ~[mongodb-driver-core-4.11.1.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:156) ~[mongodb-driver-core-4.11.1.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.SocketTimeoutException: Read timed out
at java.base/sun.nio.ch.NioSocketImpl.timedRead(NioSocketImpl.java:283) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:309) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803) ~[na:na]
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966) ~[na:na]
at java.base/[sun.security](sun.security).ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:484) ~[na:na]
at java.base/[sun.security](sun.security).ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:478) ~[na:na]
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:70) ~[na:na]
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1465) ~[na:na]
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:1069) ~[na:na]
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:175) ~[mongodb-driver-core-4.11.1.jar!/:na]

11 Replies

mattmHOBBY

a year ago

I now seem to have gotten past that error, only to exchange it for this one:

Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server mongodb.railway.internal:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}


mattmHOBBY

a year ago

I am wondering what version of MongoDB is getting deployed onto Railway.
I've looked much but can't seem to see that anywhere.
The one on MongoDB Cloud is v6.0.12.
If the one on Railway is a different major version (v7.x for example) maybe I need to update the Mongo driver being used by my app??


a year ago

I don't know anything about java, all I can say is that at the time of writing this if you were to deploy a mongo database on railway you would be deploying mongo version 7.0.5


mattmHOBBY

a year ago

Thanks brody.
Would anyone else know what I'm doing wrong?
My code (upon startup) tries to insert some documents into Mongo… it seems my user does not have the proper permissions to do so.
Is there some switch I must flip for this?


mattmHOBBY

a year ago

yeah I see it is definitely getting connected to Mongo running on Railway:

2024-01-17T17:25:39.727Z INFO 7 --- [dbME] [.internal:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=mongodb.railway.internal:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=21, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=35331380}

but my code is unable to read from Mongo b/c when it tries I get:

Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='mongo', source='dbme', password=, mechanismProperties=}

NOTE: I created the dbme database in Mongo on Railway, but the same issue occurs if I try to connect to the "test" DB that's created automatically.


mattmHOBBY

a year ago

I guess Railway's Mongo is not supported from Java??
I may have to just use MongoDB Cloud's hosting for my Mongo.
Railway will just miss-out on the revenue??


a year ago

Is your mongodb password protected? If you've deployed it on railway, usually, databases are password protected by default. Since i can't see if you're passing the password along, and you're getting an authentication error, that might be the case?


mattmHOBBY

a year ago

Thanks.

The JDBC URL has both user and password embedded.
In fact the entire URL (other than the /databaseName suffix) is exposed to me by Railway as a variable named

MongoDB.MONGOPRIVATEURL

So the (auto-generated) password is there.

Basically I am using the "Reference Variables" concept described here in Railway's own docs:

https://docs.railway.app/guides/variables#referencing-another-services-variable

and this post speaks of the user:pass with Mongo:

https://docs.railway.app/guides/mongodb#tcp-proxy-connection


a year ago

Hmm. Is the connection uri to mongo's own service structured differently? I've had a similar issue when using the uri given by railway (postgres db), I assumed this was because the password contained reserved characters ('+', among others) and so I had to pass the password and username separately in my application.properties. (And thus had to remove those parts from the connection url.)

Not sure if this is actually the problem though, especially because Railway should've issued a fix where reserved characters won't appear in those generated passwords anymore


mattmHOBBY

a year ago

Thanks again.

The structure/format of the Railway-provided mongo uri is the same as the one I use to to connect to "offsite" mongo (mongo running on MongoDB's cloud).
And seems like if that were the issue I wouldn't even be able to get connected.
Instead I am getting a MongoSecurityException that mentions userName='mongo'
so to me just seems like that user does not have permission to connect… yet it is the Railway-provided mongo user.


splawrenceHOBBY

a year ago

I am experiencing similar issue but with Postgres. I am also using referencing variables and have tried several different patterns in my application.yml, but with no luck. Any help is appreciated.