Browse Source

mongo 密码设置链接方式

pzc 4 years ago
parent
commit
81fd65a141
1 changed files with 5 additions and 1 deletions
  1. 5 1
      ymall/src/main/java/com/liangjian11/ymall/utils/MongoDBUtil.java

+ 5 - 1
ymall/src/main/java/com/liangjian11/ymall/utils/MongoDBUtil.java

@@ -2,6 +2,7 @@ package com.liangjian11.ymall.utils;
 
 
 import com.mongodb.MongoClient;
+import com.mongodb.MongoClientURI;
 import com.mongodb.MongoCredential;
 import com.mongodb.ServerAddress;
 import com.mongodb.client.FindIterable;
@@ -33,7 +34,10 @@ public class MongoDBUtil {
       List<MongoCredential> credentials = new ArrayList<>();
       MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
       credentials.add(mongoCredential);
-      MongoClient mongoClient = new MongoClient(adds, credentials);
+//      MongoClient mongoClient = new MongoClient(adds, credentials);
+      String mongoClientURI = "mongodb://"+username+":"+password+"@" + host + ":" + port;
+      MongoClientURI connectionString = new MongoClientURI(mongoClientURI);
+      MongoClient mongoClient = new MongoClient(connectionString);
       MongoDatabase mongoDatabase = mongoClient.getDatabase(database);
       return mongoDatabase;
     } else {