问:无法通过Mongo Java Driver进行身份验证。在mongo shell上工作正常。
MongoDB服务器版 -
db version v3.2.6
Java Mongodb驱动程序Maven依赖
version 2.6.2
这就是我做的 -
Java方
MongoOptions moptions = new MongoOptions();
moptions.autoConnectRetry = Boolean.getBoolean(prop.getProperty("MONGO_OPTIONS_AUTOCONNECT_RETRY"));
moptions.connectionsPerHost = Integer.parseInt(prop.getProperty("MONGO_OPTIONS_CONNECTIONS_PER_HOST"));
moptions.threadsAllowedToBlockForConnectionMultiplier = Integer.parseInt(prop.getProperty("MONGO_OPTIONS_THREADS_BLOCK"));
ServerAddress srvAddr = new ServerAddress(prop.getProperty("MONGODB_HOST"), Integer.parseInt(prop.getProperty("MONGODB_PORT")));
client = new Mongo(srvAddr, moptions);
DB adminDB = client.getDB("admin");
Boolean auth = adminDB.authenticate("abc", "abc".toCharArray());
结果
auth总是出现错误。
答案 0 :(得分:1)
MongoDB Java驱动程序2.x的身份验证过程我知道如下:
MongoCredential credential = MongoCredential.createCredential(
user, "admin", password.toCharArray());
Mongo mongo = new MongoClient(new ServerAddress(mongoHostname),
Arrays.asList(credential));
注意使用2.x驱动程序访问Mongo 3.2数据库至少需要MongoDB Java驱动程序版本2.14(参见MongoDB compatibility info)。您的2.6.2版本太旧了。你需要升级它。