How to connect to MongoDB 3.2 in Java with username and password?

Mike B.

I'm using MongoDB 3.2 in my application. The code below demonstrates database initialization logic:

private void dbInit(String dbName) {

    String mongoClientURI = "mongodb://" + DB_URL + ":" + DB_PORT;
    MongoClientURI connectionString = new MongoClientURI(mongoClientURI);

    // enable SSL connection
    MongoClientOptions.builder().sslEnabled(true).build();

    if (this.mongoClient == null) {
        this.mongoClient = new MongoClient(connectionString);
    }

    // create database if doesn't exist
    this.mongoClient.getDatabase(dbName);
}

This code works fine, now I want to introduce access level separation to database.

The steps to do that:

  1. Define users:

    use myAppDB
    db.createUser(
       {
          "user": "myAdmin",
          "pwd": "123090d1487dd4ab7",
          roles: [ "readWrite", "dbAdmin" ]
       }
    )
    
    use myAppDB
    db.createUser(
       {
          "user": "guest",
          "pwd": "guest",
          roles: [ "read" ]
       }
    )
    
  2. Re-create the MongoDB 3.2 service in authentication mode: "C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --install --dbpath=C:\data\db --logpath=C:\data\log\log.txt --auth --service. And run it.

  3. Change the mongoClientURI connection string to

    String mongoClientURI = "mongodb://" + DB_SRV_USR + ":" + DB_SRV_PWD + "@" + DB_URL + ":" + DB_PORT;
    

    where DB_SRV_USR = myAdmin and DB_SRV_PWD = 123090d1487dd4ab7.

  4. Check the authenticated connection in IDEA's Mongo Explorer with the same credentials, everything is OK.

  5. Execute my application and get exception Authentication failed.

My questions:

  1. How to connect to MongoDB 3.2 in Java with username and password? I saw a couple of examples but they are using deprecated methods.
  2. Should I add my users to myAppDB or to admin table? In some tutorials I saw that users are created in admin table is it a good idea or it worth to create users only in a database they are going to work with?
Mike B.

As MarkusWMahlberg correctly noted, it is necessary to note the database name in the connection string.

For instance:

String mongoClientURI = "mongodb://" + DB_SRV_USR + ":" + DB_SRV_PWD + "@" + DB_URL + ":" + DB_PORT + "/" + dbName;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to connect mongodb(mongoose) with username and password?

How to configure MongoDB to be able to connect using username and password?

How to connect LDAP With username and password?

How to use username and password (no SSL) in Java client to connect to Elasticsearch?

How to secure MongoDB with username and password

How to connect with an API that requires username and password

How to connect with coreNLP server with username password?

How to create a Login in userform with 2 or 3 Username and Password?

FTP connect with no username and password?

How can i connect a Java mqtt client with username and password to an emqttd(EMQ) broker?

How can i connect a Java mqtt client with username and password paho client

Java - Connect to MySQL without specifying username and password inside the code

Connect to the LDAP Active Directory in Java without username and password

how to add username and password in java db (derby)

How to automatically enter username and password when prompted to connect to a remote server

How to connect to an ActiveMQ server by setting the username and password programmatically?

Connect to OPCUA server with username and password

connect to openvpn using username and password

orientdb connect invalid password and username

Spring Security 3- How to customize username/password parameters?

how to use a supplied username and password to read a file in Java

How to send username and password in popup box through selenium in java?

How to hide MySQL Connection String- Username and Password on GitHub (Java)

How to compare username password and secret code with text file to login java

How to validate username and password on Android using Java regex?

Using JSoup to connect to a website with username/password

Connect to TFS GIT domain\username + password

Delphi/Rdp check username and password before connect

Use windows explorer to connect to a server with username and password