2

我一直在尝试将 mongodb 与 grails 一起使用。为此,在 BuildConfig.groovy 的插件部分中添加以下代码

compile ":mongodb:3.0.3" 

然后我将 DataSource.groovy 更改为以下内容:

          grails {
            mongo {
              host = "localhost"
              port = 27107
              username = "user"
              password="secretpassword"
              databaseName = "physicians"
            }
          }

但它会导致编译错误如下:

Error |
2015-07-14 14:29:53,412 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'mongoTransactionManager' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoBean' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'mongoTransactionManager' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoBean' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
    Line | Method
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'mongoTransactionManager' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoBean' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoBean' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoBean' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
Caused by MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 10000 ms
->>   87 | getServer in com.mongodb.BaseCluster
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    654 | getServer in com.mongodb.DBTCPConnector
|     39 | access$300 in     ''
|    503 | getConnection in com.mongodb.DBTCPConnector$MyPort
|    451 | get . . . in     ''
|    624 | authenticate in com.mongodb.DBTCPConnector
|    195 | doAuthenticate in com.mongodb.DBApiLayer
|    765 | authenticateCommandHelper in com.mongodb.DB
|    721 | authenticate in     ''
|    149 | afterPropertiesSet in org.grails.datastore.gorm.mongo.bean.factory.GMongoFactoryBean
|    334 | innerRun  in java.util.concurrent.FutureTask$Sync
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
Error |
Forked Grails VM exited with error

你能告诉我如何解决这个错误吗?顺便说一句,我一直在关注的教程可以在下面的链接中找到:http: //blog.mongodb.org/post/18510469058/grails-in-the-land-of-mongodb

4

2 回答 2

0

您的服务器无法访问,您可以在异常跟踪中清楚地看到:

由 MongoTimeoutException 引起:在 10000 毫秒后等待与 AnyServerSelector{} 匹配的服务器时超时

于 2015-07-14T09:19:47.973 回答
0

所以这就是正在发生的事情:

首先,您需要确认您的 Mongo 服务是否正在运行,只需输入mongo命令(并查看您是否登陆 Mongo shell)或通过ps -A | grep mongod.

无论如何,如果 MongoDB 服务正在运行,那么您首先需要启动您的服务。

sudo service mongod start# 对于 Ubuntu 和 CentOS
# 或者
mongod --config /etc/mognod.conf

(我已经为您提供了 Ubuntu 和 CentOS 的示例。如果您使用不同的操作系统,请阅读此内容。)

现在,在您开始服务或确认 MongoDB 服务器已启动并运行后,您需要执行run-app.

我在端口号的问题中看到有问题的一件事,2710727017. 我不确定这是否是问题中的错误,或者您已将其添加到您的 GrailsDataSource.groovy文件中。默认情况下,MongoDB 在端口号上运行27017(尽管您可以配置它)。所以这可能是你的问题。

因此,将该端口号更改为27017,然后执行grails run-app. 这应该有效。

于 2015-07-14T13:06:40.450 回答