Mongo连接不能与c#驱动程序版本2.4.3一起使用

时间:2017-05-15 18:14:12

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

我的代码

    using MongoDB.Bson;
    using MongoDB.Driver;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace MongoDriver2._4._3Test
    {
        class Program
        {
            protected static MongoCredential _credentials;
            protected static MongoClientSettings mongoClientSettings;
            protected static IMongoClient _client;
            protected static IMongoDatabase _database;
            static void Main(string[] args)
            {
                _credentials = MongoCredential.CreateMongoCRCredential("dbName", "userName", "password");
                mongoClientSettings = new MongoClientSettings
                {
                    Credentials = new[] { _credentials },
                    Server = new MongoServerAddress("172.x.x.x", 27017)
                };
                _client = new MongoClient(mongoClientSettings);
                getDataFromMongo();
                Console.ReadLine();
            }
            static async void getDataFromMongo()
            {
                _database = _client.GetDatabase("dbName");
                var collection = _database.GetCollection<BsonDocument>("_project");
                var builder = Builders<BsonDocument>.Filter;
                var filter = new BsonDocument();
                var count = 0;
                using (var cursor = await collection.FindAsync(filter))
                {
                    while (await cursor.MoveNextAsync())
                    {
                        var batch = cursor.Current;
                        foreach (var document in batch)
                        {
                            Console.WriteLine(count + "_id -> " + document[0] + "ProjectName -> " + document[7] + "\n");
                            // process document
                            count++;
                        }
                    }
                }
            }
        }
    }

问题:

{"A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode : Primary } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : \"1\", ConnectionMode : \"Automatic\", Type : \"Unknown\", State : \"Disconnected\", Servers : [{ ServerId: \"{ ClusterId : 1, EndPoint : \"172.24.17.3:27017\" }\", EndPoint: \"172.24.17.3:27017\", State: \"Disconnected\", Type: \"Unknown\", HeartbeatException: \"MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoAuthenticationException: Unable to authenticate username 'devTeam' on database 'BAC_Test'. ---> MongoDB.Driver.MongoCommandException: Command authenticate failed: auth failed.\r\n   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)\r\n   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.<ExecuteAsync>d__11.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Authentication.MongoDBCRAuthenticator.<AuthenticateAsync>d__7.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at MongoDB.Driver.Core.Authentication.MongoDBCRAuthenticator.<AuthenticateAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Authentication.AuthenticationHelper.<AuthenticateAsync>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Connections.ConnectionInitializer.<InitializeConnectionAsync>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Servers.ServerMonitor.<HeartbeatAsync>d__27.MoveNext()\" }] }."}

我无法连接到mongodb。我的包版本是2.4.3 Mongodb c#的官方驱动程序,我试过两个,用连接字符串连接

_client = new MongoClient("mongodb://user:password@172.x.x.x:27017/dbname");

并使用mongoClientSettings连接,如上面给出的代码所示

        _credentials = MongoCredential.CreateMongoCRCredential("dbName", "userName", "password");
                mongoClientSettings = new MongoClientSettings
                {
                    Credentials = new[] { _credentials },
                    Server = new MongoServerAddress("172.x.x.x", 27017)
                };
                _client = new MongoClient(mongoClientSettings);

不幸的是两个都没有用。我已经看到可用的解决方案MongoDB-CR Authentication failed要求我降级authSchema版本,但我不想这样做。在我的情况下,我使用mongo 3.4和c#驱动程序2.4.3进行了更新,所以我想要一个符合最新更新的解决方案。

1 个答案:

答案 0 :(得分:1)

请查看是否为此数据库配置了用户名。

无法在数据库'BAC_Test'上验证用户名'devTeam'。可能是应该验证的数据库是admin?

尝试像这样单独进行身份验证。

MongoCredential.CreateCredential(“admin”,用户名,密码)

让我知道你的发现

谢谢, Phanivikranth