无法连接到远程mongo db 2天

时间:2015-07-19 16:46:10

标签: mongodb spring-data mlab

我无法连接到远程mongo数据库。 我在我的pom中使用以下spring依赖项:

<dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>3.0.3</version>
  </dependency>

  <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-mongodb</artifactId>
      <version>1.7.1.RELEASE</version>
  </dependency>

我的数据库连接xml的相关部分是:

<mongo:mongo id="mongo" host="ds059702.mongolab.com" port="59702"  />
<mongo:db-factory
        id="mongoDbFactory"
        host="ds059702.mongolab.com"
        port="59702"
        username="XXX"
        password="XXX"
        dbname="test081142"
        mongo-ref="mongo"/>

mongo db的版本是2.6。 连接(基本上是阅读)时我得到的例外是

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 13 and error message 'not authorized for query on test081142.Entity' on server ds059702.mongolab.com:59702; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized for query on test081142.Entity' on server ds059702.mongolab.com:59702
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:96)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2011)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1894)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1300)
at org.springhbx.common.dao.TestEntityDao.readAllRecords(TestEntityDao.java:34)
at org.springhbx.common.test.DaoTest.getAllRecords(DaoTest.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

引起:com.mongodb.MongoQueryException:查询失败,错误代码为13,错误消息未经授权在test081142上查询。实体&#39;在服务器ds059702.mongolab.com:59702

我需要一些帮助来找出我错误的地方。我100%肯定凭据是对的。无法确定我们的确切问题。

------------------- UPDATE --------------------------- ---

我可以使用

连接并执行CRUD
 MongoClientURI uri  = new MongoClientURI("mongodb://dbuser:XXXX@ds059702.mongolab.com:59702/test081142");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

所以我的弹簧配置一定有问题......

2 个答案:

答案 0 :(得分:2)

我可以使用以下配置连接到远程mongo db:

<mongo:mongo-client id="mongo" host="ds059702.mongolab.com" port="59702" credentials="dbuser:XXXX@test081142" >
</mongo:mongo-client>
<mongo:db-factory
        id="mongoDbFactory"
        host="ds059702.mongolab.com"
        port="59702"
        username="dbuser"
        password="XXXX"
        dbname="test081142"
        mongo-ref="mongo" authentication-dbname="test081142"/>

在spring-data文档中没有写到这是使用spring配置连接到mongo db所需的语法。可悲的是,弹簧的数据需要提供的配置语法更清晰。 直接从spring docs

获取语法
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      xsi:schemaLocation=
      "http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017"/>

答案 1 :(得分:0)

从错误消息中,似乎&#34; user&#34;您连接到数据库时没有读取&#39;作用。

用户必须通过(可能由您的db-admin)授予角色:

Shell.Application

以上代码取自以下链接:

http://docs.mongodb.org/manual/tutorial/manage-users-and-roles/#grant-a-role