sfgroups
sfgroups

Reputation: 19099

Grails 3 MongoDB not reading connectionString from application.yml

I am testing the Grail 3 application to connect mogoDB running on another server.

Standalone java program connect to database successfully. But Grail 3 application not able to connect to DB. Exception show its connecting to localhost.

I would like to understand why its not reading connectionstring from aplication.yml file.

application.yml file:

environments:
    development:
        grails:
            mongodb:
                connectionString: "mongodb://192.168.1.13:27017/test"

when I access the page, seeing this error message.

grails> 2017-02-14 22:52:28.116 ERROR --- [nio-8080-exec-9] o.g.web.errors.GrailsExceptionResolver   : MongoTimeoutException occurred when processing request: [GET] /book/index
Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]. Stacktrace follows:

why its connecting to localhost?

tried from this answer didn't work.

Installing and using MongoDB in Grails 3.x

Thanks

Upvotes: 1

Views: 583

Answers (2)

emerson
emerson

Reputation: 151

If you still want to use a connection string instead of supplying all of the parameters separately, you can use url as listed below:

environments:
    development:
        grails:
            mongodb:
                url: "mongodb://192.168.1.13:27017/test"

This approach also allows you to supply query parameters where necessary. The documentation can be found in the gorm manual under the MongoDB Connection Strings section.

Upvotes: 2

Djamware
Djamware

Reputation: 126

This configuration work for me in both development and production.

environments:
development:
    grails:
        mongodb:
            host: "localhost"
            port: 27017
            username: ""
            password: ""
            databaseName: "mydb-dev"
production:
    grails:
        mongodb:
            host: "1.1.1.1"
            # host: "localhost"
            port: 27017
            username: ""
            password: ""
            databaseName: "mydb-prod"

I'm using Grails 3.1.9 and latest MongoDB plugin.

compile 'org.grails.plugins:mongodb'

Upvotes: 3

Related Questions