c#连接到远程mongodb时的MongoAuthenticationException(3.2)

本文关键字:MongoAuthenticationException 时的 mongodb 连接 | 更新日期: 2023-09-27 18:16:51

我正在尝试连接到远程mongodb(3.2版本)服务器使用以下c#代码:

  _client = new MongoClient(
                      new MongoClientSettings
                      {
                          Credentials = new[]
        {
                MongoCredential.CreateMongoCRCredential("test", "usernamex", "password")
        },
                          Server = new MongoServerAddress("xx.xx.xx.xxx", 27017)
                      });
        var databases = _client.ListDatabasesAsync().Result;

会引发以下异常:MongoAuthenticationException -无法在数据库"test"上验证用户名"usernamex"。在远端服务器上存在用户usernamex。在远程服务器netstat -a -n给出xx.xx.xx。Xxx:27017连接建立。任何想法?

c#连接到远程mongodb时的MongoAuthenticationException(3.2)

对于将来面临此类问题的所有人,以下是解决方案:

  1. 确保连接字符串中的用户确实有足够的权限。使用mongo shell,它将是:>使用test> db.getUser("usernamex")

  2. 替换下面的代码行:

    MongoCredential。CreateMongoCRCredential("test", "usernamex", "password")

和这个:

MongoCredential.CreateCredential("test", "usernamex", "password")
相关文章:
  • 没有找到相关文章