MongoDB原始Java连接

huangapple go评论81阅读模式
英文:

MongoDB raw java connection

问题

I am trying to create a generic mongo connection component that will be used with different mongo DB instances. I managed to make it work with some code:

// Creating a Mongo client 
MongoClient mongo = new MongoClient("localhost", 27017); 

// Creating Credentials 
MongoCredential credential; 
credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); 
System.out.println("Connected to the database successfully");  

// Accessing the database 
MongoDatabase database = mongo.getDatabase("myDb"); 
System.out.println("Credentials ::" + credential);

I don't understand why it needs to specify the database in 2 places: "myDb", once in the credential, and once while it does a "getDatabase". More than that on my setup I need to specify a different DB on "createCredential": "admin" in order to work. Why is the credential database different than the one I will run the query?

英文:

I am trying to create a generic mongo connection component that will be used with different mongo DB instances. I managed to make it work with some code like:

  // Creating a Mongo client 
  MongoClient mongo = new MongoClient( "localhost" , 27017 ); 

  // Creating Credentials 
  MongoCredential credential; 
  credential = MongoCredential.createCredential("sampleUser", "myDb", 
     "password".toCharArray()); 
  System.out.println("Connected to the database successfully");  
  
  // Accessing the database 
  MongoDatabase database = mongo.getDatabase("myDb"); 
  System.out.println("Credentials ::"+ credential); 

I don't understand why it needs to specify the database in 2 places: "myDb", once in the credential, and once while it does a getDatabase. More than that on my setup I need to specify a different DB on createCredential: "admin" in order to work. Why is the credential database different than the one I will run the query?

答案1

得分: 1

当您深入检查代码时,您会发现以下令人信服的原因。

这是所有认证器失效的地方。

private void authenticateAll(final InternalConnection internalConnection, final ConnectionDescription connectionDescription) {
    if (connectionDescription.getServerType() != ServerType.REPLICA_SET_ARBITER) {
        for (final Authenticator cur : authenticators) {
            cur.authenticate(internalConnection, connectionDescription);
        }
    }
}

authenticators 包含一组凭据。有四种实现方式。

  1. 默认
  2. 本机
  3. x509
  4. sasl

> "myDb",在凭证中出现一次 - 为什么

在这里指定的主要原因是,要在哪个数据库上执行认证命令,因为每个数据库可以有不同的用户名。

executeCommand(getCredential().getSource(), authCommand, connection);

> 而在执行 getDatabase 时出现一次 - 为什么

这完全不同。它返回包含 read、write concerns、collection 列表、创建视图、创建集合 等选项的 MongoDatabase 对象。

英文:

When you inspect the code deeper, you will find the below compelling reasons.

This is the place where all the authenticators falls down.

private void authenticateAll(final InternalConnection internalConnection, final ConnectionDescription connectionDescription) {
        if (connectionDescription.getServerType() != ServerType.REPLICA_SET_ARBITER) {
            for (final Authenticator cur : authenticators) {
                cur.authenticate(internalConnection, connectionDescription);
            }
        }
    }

authenticators contains list of credentials. There are four implementations.

  1. Default
  2. Native
  3. x509
  4. sasl

>"myDb", once in the credential - why

Main reason to specify here, on which database the authenticate command has to be executed as each database can have different user name.

executeCommand(getCredential().getSource(), authCommand, connection);

> once while it does a getDatabase - why

It is altogether different. It returns MongoDatabase object which contains options to read, write concerns, list of collections, create view, create collection.

huangapple
  • 本文由 发表于 2020年8月12日 04:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63365976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定