如何在Grails2.4.4中使用mongodb?

如何在Grails2.4.4中使用mongodb?,mongodb,grails,gorm-mongodb,Mongodb,Grails,Gorm Mongodb,我一直在尝试将mongodb与grails结合使用。为此,在BuildConfig.groovy的插件部分中添加以下代码 compile ":mongodb:3.0.3" 然后,我将DataSource.groovy更改为以下内容: grails { mongo { host = "localhost" port = 27107 username = "user"

我一直在尝试将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
你能告诉我如何修正这个错误吗。顺便说一句,我一直遵循的教程可以在下面的链接中找到:

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

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


下面是正在发生的事情:

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

在任何情况下,如果MongoDB服务正在运行,那么您首先需要启动服务

sudo服务mongod start
#适用于Ubuntu和CentOS
#或
mongod--config/etc/mognod.conf

(我已经给你举了Ubuntu和CentOS的例子。如果你使用的是不同的操作系统,请阅读。)

现在,在您启动服务或确认MongoDB服务器已启动并运行后,您需要执行
运行应用程序

我发现你的问题中有一点有问题,那就是端口号是
27107
,而不是
27017
。我不确定这是问题中的错误,还是您将其添加到了Grails
DataSource.groovy文件中。
默认情况下,MongoDB在端口号
27017
上运行(尽管您可以对其进行配置)。所以这可能是你的问题


因此,将端口号更改为
27017
,然后执行
grailsrunapp
。这应该行。

我是grails的初学者。您能告诉我如何解决此问题吗?请检查mongo服务器是否已启动并正在运行,是否可以通过
localhost:27107