如何为mongodb replicaset配置grails 3

时间:2017-04-06 12:31:29

标签: mongodb grails database-replication

我正在将grails 3应用程序部署到实时服务器。已经在复制模式下设置了mongodb实例,并从活动框中成功进行了测试。我测试了:

mongo -u uname -authenticationDatabase dbname ip-1.ec2.internal:27017/dbname -p password

我已使用指定hereherereplicaSetconnectionString。但在这两种情况下,它都表现为没有提供主机配置,因此默认为 localhost 。由于没有设置,它失败了。这是 application.groovy

mongodb {
            replicaSet = [ "ip-1.ec2.internal", "ip-12.ec2.internal", "ip-3.ec2.internal"]
            host = "ip-1.ec2.internal" //This works for any of the hosts
            port = 27017
            username = "username"
            password = "password"
            databaseName = "dbname"
        }

我使用的是最新版本:

grailsVersion=3.2.8 //It was the same for 3.2.5
gormVersion=6.0.9.RELEASE
gradleWrapperVersion=3.4.1 //It was the same for 3.0

以下是其版本的插件列表:

compile 'org.grails.plugins:mongodb:6.1.0'
compile 'org.grails:grails-datastore-rest-client:6.0.9.RELEASE'

这是我遇到的错误:

01:22:26.410 - [localhost-startStop-1] INFO  org.mongodb.driver.cluster - No server chosen by 
ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, 
all=[ServerDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: 
Exception authenticating MongoCredential{mechanism=null, userName='username', source='dbname', password=<hidden>, 
mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 
'Authentication failed.' on server 127.0.0.1:27017. The full response 
is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]}. Waiting for 30000 ms before timing out

我现在已经没有选择了,有人可以帮我指点一下吗?感谢。

1 个答案:

答案 0 :(得分:2)

James Kleeh建议的connection strings解决了这个问题。这就是我使用它来参考example

的方法
mongodb {
    url = "mongodb://ip-1.ec2.internal,ip-2.ec2.internal,ip-3.ec2.internal/?replicaSet=hostname-of-mongod-instance"
    port = 27017
    ...
}