【问题标题】:MongoDB can connect from mongo client but not from C# driverMongoDB 可以从 mongo 客户端连接,但不能从 C# 驱动程序连接
【发布时间】:2017-09-12 13:04:35
【问题描述】:

这件事现在会让我发疯。我试图让它工作 3 个小时而没有任何结果。我安装了 mongodb 2.8rc4。我在 admin db 中创建了一个用户帐户。我可以使用 mongo localhost/agp -u dato0011 -p "MyPassword" 连接到 mongod。我可以通过 db.auth 对其进行身份验证,但由于未知原因,这在 C# 驱动程序中不起作用。

连接字符串是mongodb://dato0011:MyPWD@localhost/agp

我使用以下代码初始化数据库:

        var client = new MongoClient(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
        var server = client.GetServer();
        var db = server.GetDatabase("agp");

但是当我尝试保存时,我得到一个异常提示:

Unable to connect to server 192.168.0.100:27017: Invalid credential for database 'agp'..

在内部异常的深处,有这样的消息:

{ "authenticate" : 1, "user" : "dato0011", "nonce" : "fc65b3c269560533", "key" : "35589d31830b76c72358343bc054105f" }

还有这个

{"Command 'authenticate' failed: auth failed (response: { \"ok\" : 0.0, \"errmsg\" : \"auth failed\", \"code\" : 18 })"}

有人知道我做错了什么吗? 谢谢。

更新

不知道它是否会有所帮助,但这是引发异常的代码:

Users.Save(user);

用户在哪里

Users = db.GetCollection<User>("users", WriteConcern.W1);

更新 2 显然异常消息有点误导。驱动程序确实连接到 mongod,但由于未知原因无法进行身份验证。正确传递用户名和密码参数。这是 admin db 中的用户。

> db.system.users.find().toArray()
[
        {
                "_id" : "agp.dato0011",
                "user" : "dato0011",
                "db" : "agp",
                "credentials" : {
                        "SCRAM-SHA-1" : {
                                "iterationCount" : 10000,
                                "salt" : "GjlDJOiKf2Xn1VO/1MhWXA==",
                                "storedKey" : "vAi30QZLFkCZf6ISm5TIfIWPwZY=",
                                "serverKey" : "DBmbbRLrLXEIxFCuZ52VaSnRWwo="
                        }
                },
                "roles" : [
                        {
                                "role" : "readWrite",
                                "db" : "agp"
                        },
                        {
                                "role" : "dbAdmin",
                                "db" : "agp"
                        }
                ]

【问题讨论】:

  • 27017端口设置在哪里?
  • 您从命令行连接到 localhost,但从 C# 连接到 192.168.0.100 - 疏忽还是故意?
  • @HamletHakobyan 我已经试过了,没区别
  • @Davita 这不是连接问题.. 这是身份验证问题。
  • 您使用的是什么版本的驱动程序?服务器 2.8 中的身份验证已更改,当前稳定版本的驱动程序 (1.9.2) 不支持它。这些最简单的解决方案是使用 1.10.0-rc1 驱动程序或使用 2.6 服务器。

标签: c# .net mongodb authentication mongodb-.net-driver


【解决方案1】:

在 MongoClientSettings 上使用 MongoCredentials -

    string username = "user";
    string password = "password";
    string mongoDbAuthMechanism = "SCRAM-SHA-1";
    MongoInternalIdentity internalIdentity = 
              new MongoInternalIdentity("admin", username);
    PasswordEvidence passwordEvidence = new PasswordEvidence(password);
    MongoCredential mongoCredential = 
         new MongoCredential(mongoDbAuthMechanism, 
                 internalIdentity, passwordEvidence);
    List<MongoCredential> credentials = 
               new List<MongoCredential>() {mongoCredential};


    MongoClientSettings settings = new MongoClientSettings();
    // comment this line below if your mongo doesn't run on secured mode
    settings.Credentials = credentials;
    String mongoHost = "127.0.0.1";
    MongoServerAddress address = new MongoServerAddress(mongoHost);
    settings.Server = address;

    MongoClient client = new MongoClient(settings); 

类似的: Error on MongoDB Authentication

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,就我而言,当我阅读服务器日志时。这是由于 mongo 的“authSchema 版本”造成的问题,所以您只需遵循以下步骤,它就会开始工作

    • 不带 --auth 选项重启 mongod
    • 现在在客户端上使用以下内容

      mongo
      use admin;
      db.system.users.remove({})    
      db.system.version.remove({})
      db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 })
      

    *再次创建您的用户并在 --auth 模式下重新启动 mongod

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 2015-11-24
      • 2021-10-23
      相关资源
      最近更新 更多