【问题标题】:Grails 3 MongoDB not reading connectionString from application.ymlGrails 3 MongoDB没有从application.yml读取connectionString
【发布时间】:2017-02-15 13:32:45
【问题描述】:

我正在测试 Grail 3 应用程序以连接在另一台服务器上运行的 mogoDB。

独立java程序成功连接数据库。但 Grail 3 应用程序无法连接到数据库。异常显示它连接到本地主机。

我想了解它为什么不从 aplication.yml 文件中读取连接字符串。

application.yml 文件:

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

当我访问该页面时,看到此错误消息。

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:

为什么要连接到本地主机?

从这个答案试过没有用。

Installing and using MongoDB in Grails 3.x

谢谢

【问题讨论】:

    标签: mongodb grails


    【解决方案1】:

    此配置在开发和生产中都适用于我。

    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"
    

    我正在使用 Grails 3.1.9 和最新的 MongoDB 插件。

    compile 'org.grails.plugins:mongodb'
    

    【讨论】:

      【解决方案2】:

      如果您仍想使用连接字符串而不是单独提供所有参数,则可以使用url,如下所示:

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

      此方法还允许您在必要时提供查询参数。该文档可以在 MongoDB 连接字符串部分下的 gorm manual 中找到。

      【讨论】:

      • 感谢您的提示。
      猜你喜欢
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多