Spring Boot 1.5.1 MongoDB连接错误

时间:2017-07-17 11:14:11

标签: spring mongodb spring-boot database

您好我无法使用Spring Boot 1.5.1连接到MongoDB。 任何建议都表示赞赏。

以下是我的Application.properties文件。

server.port = 9026
spring.data.mongodb.uri=mongodb://username:password@primaryurl:32020,secondaryurl:32020/name?replicaSet=name&connectTimeoutMS=300000

spring.data.mongodb.authentication-database=admin

logging.file = configDD-service.log

这是我的错误判断:

No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE,
serverDescriptions=[ServerDescription{address=zlp14853.vci.att.com:32020, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: 
 Exception authenticating MongoCredential{mechanism=null, userName='vtmUsr', source='vtm', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException:
Command failed with error 18: 'Authentication failed.' on server zlp14853.vci.att.com:32020. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}},
ServerDescription{address=zlp14852.vci.att.com:32020, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException:
Exception authenticating MongoCredential{mechanism=null, userName='vtmUsr', source='vtm', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException:
Command failed with error 18: 'Authentication failed.' on server zlp14852.vci.att.com:32020. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]}.
Waiting for 30000 ms before timing out

父POM.xml

     <?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>com.att.vtm</groupId>
  <artifactId>configdd</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <name>configdd</name>
  <description>configdd  Management</description>

  <dependencyManagement>
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
             <version>1.5.1.RELEASE</version>

            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  <modules>
    <module>configdd-dao-adapter</module>
    <module>configdd-service</module>


  </modules>
</project>

模块1 pom.xml。

<?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">

    <parent>
        <groupId>com.att.vtm</groupId>
        <artifactId>configdd</artifactId>
        <version>1.0</version>
        <relativePath>..</relativePath>
    </parent>
    <groupId>com.att.vtm</groupId>
    <version>1.0</version>

  <modelVersion>4.0.0</modelVersion>
  <artifactId>configdd-dao-adapter</artifactId>
  <name>configdd-daoadapter</name>
  <description>configdd Dao Adapter</description>

    <dependencies>
      <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <!-- <version>1.8.4.RELEASE</version> -->
            <exclusions>
                <exclusion>
                    <artifactId>mongo-java-driver</artifactId>
                    <groupId>org.mongodb</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- Make a jar -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>               
            </plugin>


        </plugins>

    </build>


</project>

模块2 POM.xml。

<?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>

  <parent>
    <groupId>com.att.vtm</groupId>
    <artifactId>configdd</artifactId>
    <version>1.0</version>
    <relativePath>..</relativePath>
  </parent>

  <artifactId>configdd-service</artifactId>
  <version>1.0</version>
  <name>configdd-service</name>
  <description>configdd Service</description>

  <!-- <dependencyManagement>-->
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <!--  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>  -->

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>


        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency> -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>


        <dependency>
            <groupId>com.att.vtm</groupId>
            <artifactId>configdd-dao-adapter</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.4.5</version>
        </dependency>

       <!--  <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency> -->

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency> 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>

       <!-- <dependency>
            <groupId>com.att.vtm</groupId>
            <artifactId>cbus-soap-adapter</artifactId>
            <version>1.0</version>
        </dependency> -->

       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
       </dependency>

       <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

  </dependencies>

     <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.att.Application</start-class>
    <java.version>1.8</java.version>
  </properties>

  <build>
    <plugins>

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.2.3.RELEASE</version>
         <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
      </plugin>

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <skipTests>true</skipTests>
          <jvmArgs>-Xmx2048m -Xms1536m -XX:PermSize=512m -XX:MaxPermSize=1024m</jvmArgs>
        </configuration>
      </plugin> 

    </plugins>
  </build>



</project>

1 个答案:

答案 0 :(得分:-1)

我能够使用此连接字符串进行连接:

spring.data.mongodb.uri=mongodb://username:password@primaryurl:32020,secondaryurl:32020/name?replicaSet=name&connectTimeoutMS=300000

副本集名称在连接字符串中是mandoary。