gpt4 book ai didi

mongodb - 2天无法连接到远程mongo db

转载 作者:可可西里 更新时间:2023-11-01 10:28:03 26 4
gpt4 key购买 nike

我无法连接到远程 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)

Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized for query on test081142.Entity' 在服务器 ds059702.mongolab.com:59702

我需要一些帮助来找出我到底在哪里犯了错误。我 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 配置有问题...

最佳答案

我可以使用以下配置连接到远程 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"/>

关于mongodb - 2天无法连接到远程mongo db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com