spring boot中的mongoDB连接异常

时间:2018-03-23 09:16:48

标签: mongodb spring-boot spring-data

我想使用spring boot连接到mongo数据库,但要接受异常和配置。

application.properties

弹簧:

数据:

mongodb的:

host:localhost

port:27017

数据库:VanGuardAuth_DB_AWS_0001

用户名:myUserAdmin

密码:sce @ 3456 @ 345

2018-03-23 08:58:59.163  INFO 18047 --- [ost-startStop-1] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
    2018-03-23 08:58:59.257  INFO 18047 --- [ost-startStop-1] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
    2018-03-23 08:58:59.297  INFO 18047 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:13575}] to localhost:27017
    2018-03-23 08:58:59.300  INFO 18047 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 19]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=2413103}
    2018-03-23 08:58:59.335  INFO 18047 --- [127.0.0.1:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server 127.0.0.1:27017

    com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='myUserAdmin', source='VanGuardAuth_DB_AWS_0001', password=<hidden>, mechanismProperties={}}
        at com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:37) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:66) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:44) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar:na]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
    Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
        at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50) ~[mongodb-driver-core-3.4.3.jar:na]
        ... 9 common frames omitted

尝试使用以下配置但同样的异常

@Bean
public MongoDbFactory mongoDbFactory() throws Exception {

   final MongoCredential credential =MongoCredential.createCredential("myUserAdmin", "VanGuardAuth_DB_AWS_0001", "Admin@321$".toCharArray());
   ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
   MongoClient mongoClient = new MongoClient(serverAddress, new ArrayList<MongoCredential>() {{ add(credential); }});
   return new SimpleMongoDbFactory(mongoClient,"VanGuardAuth_DB_AWS_0001");
   }

   @Bean
   public MongoTemplate mongoTemplate() throws Exception {
   MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
    return mongoTemplate;
  } 

0 个答案:

没有答案