无法使用C#驱动程序1.10连接到MongoDB 3.0.0 [英] Cannot connect to MongoDB 3.0.0 by using C# Driver 1.10
问题描述
我在Azure服务器上安装了 MongoDB 3.0.0 实例,并在admin数据库上创建了一个用户:
I installed a MongoDB 3.0.0 instance on an Azure server and created a user on admin db:
{
"_id" : "admin.skMongo",
"user" : "skMongo",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : NumberInt(10000),
"salt" : "(someSalt)",
"storedKey" : "(somekey)",
"serverKey" : "(somekey)"
}
},
"customData" : {
},
"roles" : [
{
"role" : "dbOwner",
"db" : "(my database)"
},
{
"role" : "readWrite",
"db" : "(my database)"
},
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
}
]
}
通过使用MongoChef作为客户端,我可以连接到该实例而不会遇到问题.我正在使用以下设置:
By using MongoChef as a client, I can connect to this instance without running into problems. I am using these settings:
Server: (my ip address)
Port: 27017
Database: admin
Username: skMongo
Password: (mypassword)
但是当我尝试使用Offical C#MongoDB驱动程序1.10.0.62 连接时,出现此错误:
But when I try to connect by using Offical C# MongoDB Driver 1.10.0.62, I get this error:
无法连接到服务器23.97.171.16:27017:无效的凭据数据库管理员".
Unable to connect to server 23.97.171.16:27017: Invalid credential for database 'admin'..
在此行:
return collection.Find(q).ToList();
这是我用来初始化mongo db服务器的代码:
This is the code I am using to initialize mongo db server:
var credentials = MongoCredential.CreateMongoCRCredential("admin", "skMongo", "(my password)");
_server = new MongoServer(
new MongoServerSettings
{
Server = string.IsNullOrEmpty(port) ? new MongoServerAddress(host) : new MongoServerAddress(host, int.Parse(port)),
Credentials = new[] { credentials },
});
_database = _server.GetDatabase("(my database)");
return _database;
此代码在mongo的早期版本上有效.我在做什么错了?
This code was working on the previous version of mongo. What am I doing wrong?
推荐答案
我已经通过更改以下行来解决了这个问题:
I have solved this by changing this line:
var credentials = MongoCredential.CreateMongoCRCredential("admin", "skMongo", "(my password)");
对此:
var credentials = MongoCredential.CreateScramSha1Credential("admin", "skMongo", ("my password"));
这篇关于无法使用C#驱动程序1.10连接到MongoDB 3.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!