2 天无法连接到远程 mongo 数据库
Unable to connect to remote mongo db for 2 days
我无法连接到远程 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.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.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:42)
at org.junit.runners.ParentRunner.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:服务器 ds059702.mongolab.com:59702
上的查询失败,错误代码 13 和错误消息 'not authorized for query on test081142.Entity'
我需要一些帮助来找出我到底在哪里犯了错误。我 100% 确定凭据是正确的。无法确定我们的确切问题。
--------------------更新------------------------ -----
我能够使用
连接并执行 CRUD
MongoClientURI uri = new MongoClientURI("mongodb://dbuser:XXXX@ds059702.mongolab.com:59702/test081142");
MongoClient client = new MongoClient(uri);
DB db = client.getDB(uri.getDatabase());
所以一定是我的 spring 配置有问题...
从错误消息来看,您连接到数据库的 "user" 似乎没有 'read' 角色。
必须通过(可能由您的数据库管理员)向用户授予角色:
use reporting
db.grantRolesToUser(
"reportsUser",
[
{ role: "read", db: "accounts" }
]
)
以上代码取自下面link :
http://docs.mongodb.org/manual/tutorial/manage-users-and-roles/#grant-a-role
我可以使用以下配置连接到远程 mongo 数据库:
<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 数据库所需的语法。遗憾的是,spring-data 需要提供更清晰的配置语法。
直接从 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"/>
我无法连接到远程 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.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.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:42)
at org.junit.runners.ParentRunner.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:服务器 ds059702.mongolab.com:59702
上的查询失败,错误代码 13 和错误消息 'not authorized for query on test081142.Entity'我需要一些帮助来找出我到底在哪里犯了错误。我 100% 确定凭据是正确的。无法确定我们的确切问题。
--------------------更新------------------------ -----
我能够使用
连接并执行 CRUD MongoClientURI uri = new MongoClientURI("mongodb://dbuser:XXXX@ds059702.mongolab.com:59702/test081142");
MongoClient client = new MongoClient(uri);
DB db = client.getDB(uri.getDatabase());
所以一定是我的 spring 配置有问题...
从错误消息来看,您连接到数据库的 "user" 似乎没有 'read' 角色。
必须通过(可能由您的数据库管理员)向用户授予角色:
use reporting
db.grantRolesToUser(
"reportsUser",
[
{ role: "read", db: "accounts" }
]
)
以上代码取自下面link :
http://docs.mongodb.org/manual/tutorial/manage-users-and-roles/#grant-a-role
我可以使用以下配置连接到远程 mongo 数据库:
<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 数据库所需的语法。遗憾的是,spring-data 需要提供更清晰的配置语法。 直接从 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"/>