簡體   English   中英

Spring Boot 2.0.5.RELEASE和Mongo 4.0連接問題

[英]Spring boot 2.0.5.RELEASE and mongo 4.0 connection issues

我正在Spring網站上關注通過MongoDB訪問數據教程
我安裝了Mongo DB服務器版本4作為服務
當我使用客戶端連接到它時,它的身份驗證工作正常。

我面臨以下問題:

MongoCommandException: Command failed with error 18: 'Authentication failed'

我看到代碼正在使用與服務器版本4不兼容的mongodb-driver-core-3.6.4.jar

如何僅在不弄亂項目的情況下升級驅動程序?
為什么他們使用spring-boot-starter-data-mongodb而不是mongodb-driver-sync?

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='petUser', source='pets', password=<hidden>, mechanismProperties={}}
    at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.4.jar:na]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_181]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27014. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
    ... 9 common frames omitted

這是項目POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-accessing-data-mongodb</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

更新資料
maven依賴插件顯示對mongodb的依賴:mongodb-driver-core:jar:3.8.0
所以我不確定此mongodb-driver-core-3.6.4.jar來自哪里?

[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ demo ---
[INFO] com.replaceme:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:2.0.5.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.5.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.5.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] |  +- org.mongodb:mongodb-driver:jar:3.8.0:compile
[INFO] |  |  +- org.mongodb:bson:jar:3.8.0:compile
[INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:3.8.0:compile
[INFO] |  \- org.springframework.data:spring-data-mongodb:jar:2.0.10.RELEASE:compile
[INFO] |     +- org.springframework:spring-context:jar:5.0.9.RELEASE:compile
[INFO] |     +- org.springframework:spring-beans:jar:5.0.9.RELEASE:compile
[INFO] |     +- org.springframework:spring-expression:jar:5.0.9.RELEASE:compile
[INFO] |     +- org.springframework.data:spring-data-commons:jar:2.0.10.RELEASE:compile

更新2
我從M2文件夾中刪除了所有mongodb jar
並將mongodb屬性添加到pom部分:

    <mongodb.version>3.8.0</mongodb.version>

現在我看到一個新錯誤:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster; but it does not exist. Its class, com.mongodb.connection.DefaultClusterFactory, is available from the following locations:

    jar:file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar!/com/mongodb/connection/DefaultClusterFactory.class

It was loaded from the following location:

    file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.DefaultClusterFactory

更新3

外面的月食運行良好,
在命令行中,我做到了:

mvn package  
cd target
java -jar .\demo-0.0.1-SNAPSHOT.jar

而且它運行良好,不確定Eclipse中存在什么問題。

我也遇到了這個問題。 當我第一次添加pom時:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.4.3</version>
    </dependency>

編譯項目后,它發生了:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster; but it does not exist. Its class, com.mongodb.connection.DefaultClusterFactory, is available from the following locations:

jar:file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar!/com/mongodb/connection/DefaultClusterFactory.class

It was loaded from the following location:

file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.DefaultClusterFactory

然后我糾正了pom,解決了這個錯誤,但是我看到了另一個錯誤:這是新的pom,我認為這是由於mongo_version所致。

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.8.0</version>
    </dependency>

這是新的警告:

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_191]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_191]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_191]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_191]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_191]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]
... 3 common frames omitted

所以這個問題是如此的簡單,網絡有很多方法可以解決它,您只需添加一個注釋以讓spring boot忽略默認的mongo_properties即可:

@SpringBootApplication(exclude = MongoAutoConfiguration.class)

然后完成!

查看spring boot提供的方法not found log。

com.mongodb.connection.DefaultClusterFactory.createCluster( Lcom/mongodb/connection/ClusterSettings; Lcom/mongodb/connection/ServerSettings; Lcom/mongodb/connection/ConnectionPoolSettings; Lcom/mongodb/connection/StreamFactory; Lcom/mongodb/connection/StreamFactory; Ljava/util/List;Lcom/mongodb/event/CommandListener; Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation; Ljava/util/List;)Lcom/mongodb/connection/Cluster;

注意第七個參數Lcom / mongodb / client / MongoDriverInformation。 該MongoDriverInformation類已moved從COM /的MongoDB /客戶端COM / MongoDB的3.7版本之外。

因此看來,您的類路徑上同時具有3.6.4和3.8.0 jar。

因此,嘗試清理您的類路徑,並且<mongodb.version>3.8.0</mongodb.version>是覆蓋mongodb依賴項的正確方法,直到Spring Boot發布為止。

順便說一句,還有一個即將發布的2.1.0引導發行版,它將把mongodb依賴關系更新為3.8.2,現在是發行候選階段。

我認為這篇關於使用Spring Boot覆蓋依賴項版本的文章將對您有所幫助。 對於reactor替換為mongodb MongoDB驅動程序本身是向后兼容的,因此在強制使用新版本時,您應該不會有什么大問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM