您好我正在使用java和mongoDB进行一些实验。我已成功安装mongoDB并能够通过命令行连接。但是当我尝试通过java连接时,它会给我带来身份验证错误。我尝试了以下方式:
public void saveExam() {
date = (DatePicker)findViewById(R.id.examDate);
Integer day = date.getDayOfMonth();
Integer month = date.getMonth();
Integer year = date.getYear();
StringBuilder temp = new StringBuilder();
temp.append(day.toString()).append("/").append(month.toString()).append("/").append(year.toString());
String dateStr = temp.toString();
time = (TimePicker)findViewById(R.id.examTime);
Integer hour, minutes;
if (Build.VERSION.SDK_INT >= 23 ) {
hour = time.getHour();
minutes = time.getMinute();
} else {
hour = time.getCurrentHour();
minutes = time.getCurrentMinute();
}
temp = new StringBuilder();
temp.append(hour.toString()).append(":").append(minutes.toString());
String timeStr = temp.toString();
Toast.makeText(add_exam.this, dateStr + ", " + timeStr, Toast.LENGTH_LONG).show();
}
并通过java代码:
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]
>
并得到了结果:
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();
}
}
}
有什么不对吗?需要一些帮助。谢谢。
答案 0 :(得分:1)
在最新版本的MongoDB 3.2中,默认的身份验证机制是" SCRAM-SHA-1"。有一个单独的类可用于此身份验证。我建议使用这种最新的方法。
有关详细信息,请参阅此link。
以下示例: -
以下代码中的数据库名称,用户名和密码。它应该适合你。
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>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
用户创建: -
use admin;
db.createUser({ user : "admin", pwd : "admin123", roles : ["dbAdmin"]});
要查看用户详细信息: -
use admin;
db.getUsers();
答案 1 :(得分:0)
mongo -u admin -p admin123 --authenticationDatabase admin
当您尝试使用mongo
shell登录时,您正在对db admin进行身份验证。但是,在代码中,您似乎正在尝试使用db testdb评估您的登录凭据
DB db = mongo.getDB("testdb");
两地应该相同。
答案 2 :(得分:0)
根据您的评论,mongo -u admin -p admin123 --authenticationDatabase admin
似乎运行良好。将DB db = mongoClient.getDB("testdb");
更改为DB db = mongoClient.getDB("admin");
答案 3 :(得分:0)
此代码有效
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(this.username, this.dbname,
this.password.toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(dbhost, this.dbport), Arrays.asList(mongoCredential));