首页 文章

尝试连接MongoLab时,URL格式出现MongoException

提问于
浏览
0

我有一个Grails 2.4.4应用程序,它使用MongoLab作为它的数据库 . 我之前使用GORM的mongo插件(see plugin here)连接到数据库 . 这不仅仅是一次很好的体验,所以我正在切换到Morpia并远离GORM来获取mongodb .

我目前将morphia库版本1.0.1作为项目中的编译时依赖项 . 我在Mongolab中创建了一个用户名:username,密码:pass(不是实际的信用卡......)

我正在尝试使用MongoClient连接到数据库 . 我一直在查看文档(documentation here),我也引用了this question但它使用了旧的连接方式 .

这是我的错误,即使我没有看到为什么抛出此错误的原因,因为我的字符串的格式似乎是正确的:

ERROR context.GrailsContextLoaderListener
- Error initializing the application: Error creating bean with name 'datastore': 
Cannot resolve reference to bean 'mongoClient' while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'mongoClient': Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; 
nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
Message: Error creating bean with name 'datastore': Cannot resolve reference to bean 'mongoClient' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'mongoClient': 
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; 
nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanInstantiationException: Could not instantiate bean class [com.mongodb.MongoClient]: 
Constructor threw exception; nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by MongoException: host and port should be specified in host:port format
->>  122 | <init>    in com.mongodb.ServerAddress
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|     49 | <init>    in     ''
|    118 | <init> .  in com.mongodb.MongoClient
|    262 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread
| Error Forked Grails VM exited with error

这是我在resources.groovy中的代码片段,我尝试将MongoClient,Morphia和我的数据存储区注册为spring bean .

beans = {

String databaseUrl = "mongodb://username:pass@ds053251.mongolab.com:12345/trendapp" //fake port
String databaseName = "trendapp"

mongoClient(MongoClient, databaseUrl)
morphia(Morphia)

datastore(Datastore, ref('mongoClient'), databaseName) { bean ->
    bean.factoryBean = 'morphia'
    bean.factoryMethod = 'createDatastore'
}
}

感谢任何帮助,谢谢 .

2 回答

  • 0

    您应该为MongoClient提供MongoClientURI

    MongoClientURI mongoClientURI = new MongoClientURI("connection string");
    MongoClient mongoClient = new MongoClient(mongoClientURI);
    
  • 4

    所以我没有弄清楚为什么会抛出这个错误,但是我能够通过使用MongoClient的不同构造函数连接到我的数据库,特别是那个需要 (ServerAddress, List<MongoCredential>) 的构造函数 .

相关问题