java连接带密码的mongodb集群_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java连接带密码的mongodb集群

java连接带密码的mongodb集群

 2018/2/5 18:11:13  welcome66  程序员俱乐部  我要评论(0)
  • 摘要:当mongodb增加了密码登录验证之后,原来使用的java连接mongo的方式需要进行改变,主要是增加用户和密码的属性到连接里。List<ServerAddress>addresses=null;MongoCredentialcredential=MongoCredential.createCredential(dbUser,dbName,dbPwd.toCharArray());MongoClientOptionsoptions=builder.build()
  • 标签:Java 连接 MongoDB

当mongodb增加了密码登录验证之后,原来使用的java连接mongo的方式需要进行改变,主要是增加用户和密码的属性到连接里。

class="java">    List<ServerAddress> addresses = null;        
    MongoCredential credential = MongoCredential.createCredential(dbUser, dbName, dbPwd.toCharArray());  
    MongoClientOptions options = builder.build();
    client = new MongoClient(addresses, Arrays.asList(credential),options);
    DB = client.getDB(dbName);

?其中关键的是monospace;">new MongoClient()这个构造函数,里面的参数可以根据需求进行设定,如:

1.MongoClient(String host, MongoClientOptions options)
2.MongoClient(String host, int port)
3.MongoClient(ServerAddress addr)
4.MongoClient(ServerAddress addr, List<MongoCredential> credentialsList)
5.MongoClient(ServerAddress addr, MongoClientOptions options)
6.MongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options)
7.MongoClient(List<ServerAddress> seeds)
8.MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList)
9.MongoClient(List<ServerAddress> seeds, MongoClientOptions options)
10.MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options)
11.MongoClient(MongoClientURI uri)

?可以根据具体的参数来创建对应的mongoClient。

?

发表评论
用户名: 匿名