Reputation: 7536
I am doing some experiments with Java and MongoDB. I have successfully install MongoDB and able to connect through command line. But when I try to connect through Java then it gives me error of authentication. I tried in following way:
nilkash@nilkash-Inspiron-5559:~$ mongo -u admin -p admin123 --authenticationDatabase admin
MongoDB shell version: 3.2.9
connecting to: test
Server has startup warnings:
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten]
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten]
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-09-17T16:29:31.090+0530 I CONTROL [initandlisten]
>
and through Java code:
package mongo;
import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
/**
* Java + MongoDB Hello world Example
*
*/
public class App {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("testdb");
boolean auth = db.authenticate("admin", "admin123".toCharArray());
if (auth) {
System.out.println("Login is successful!");
} else {
System.out.println("Login is failed!");
}
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
And got the result for it:
Login is failed!
Done
Is there anything wrong?
Upvotes: 0
Views: 6867
Reputation: 1
This code works
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(this.username, this.dbname,
this.password.toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(dbhost, this.dbport), Arrays.asList(mongoCredential));
Upvotes: 0
Reputation: 1639
Since mongo -u admin -p admin123 --authenticationDatabase admin
seems to be working fine from your comment. Change DB db = mongoClient.getDB("testdb");
to DB db = mongoClient.getDB("admin");
Upvotes: 0
Reputation: 39156
In the latest version of MongoDB 3.2, the default authentication mechanism is "SCRAM-SHA-1". There is a separate class available for this authentication. I would recommend using this latest approach.
For more information, please refer this link.
Example below:-
Just the database name, user name, password accordingly in the below code. It should work for you.
public static void main(String[] args) {
MongoClient mongoClient = null;
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("admin", "admin",
"admin123".toCharArray());
mongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(mongoCredential));
DB db = mongoClient.getDB("testdb");
System.out.println(db.getStats());
System.out.println(db.getCollectionNames());
mongoClient.close();
}
Dependency:-
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
User Creation:-
use admin;
db.createUser({ user : "admin", pwd : "admin123", roles : ["dbAdmin"]});
To check the user details:-
use admin;
db.getUsers();
Upvotes: 1
Reputation: 4417
mongo -u admin -p admin123 --authenticationDatabase admin
You are authenticating to db admin when you try to login using mongo
shell. But, in code it looks like you are trying to evaluate your login credentials with db testdb
DB db = mongo.getDB("testdb");
It should be same at both places.
Upvotes: 0