Mongodb 尝试读取超过流末尾的内容

问题描述 投票:0回答:1

我试图使用connectionuri连接到本地mongodb服务器

const string connectionUri1 = "mongodb://Mongodb:27123/admin?retryWrites=true&w=majority";
const string connectionUri2 = "mongodb://admin:password@Mongodb:27017/admin?authMechanism=SCRAM-SHA-1";
const string connectionUri3 = "mongodb://Mongodb:27020/?authMechanism=MONGODB-X509&tls=true&tlsInsecure=true&tlsCertificateKeyFile=/path/to/mongodbca.pem&tlsCAFile=/path/to/mongodbcli.pem&authSource=$external";

var settings = MongoClientSettings.FromConnectionString(connectionUri3);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);

// Create a new client and connect to the server
var client = new MongoClient(settings);

// Send a ping to confirm a successful connection
var result = client.GetDatabase("test").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
Console.WriteLine("Reply: " + result);

connectionUri1 和 2 工作得很好,我可以 ping 并执行读取或写入 对于connectionUri3,它会产生超时错误

System.TimeoutException: A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode : Primary } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/mongodb:27020" }", EndPoint: "Unspecified/mongodb:27020", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server.
 ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
   at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytes(Stream stream, Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer(Int32 responseTo, CancellationToken cancellationToken)
--- End of stack trace from previous location ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.Dropbox.RemoveMessage(Int32 responseTo)
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer(Int32 responseTo, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessage(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.HelloHelper.GetResult(IConnection connection, CommandWireProtocol`1 helloProtocol, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.ConnectionInitializer.SendHello(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelper(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelper(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.Open(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnection(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.Heartbeat(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2023-10-30T21:05:52.9901869Z", LastUpdateTimestamp: "2023-10-30T21:05:52.9909228Z" }] }.
   at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
   at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
   at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Clusters.IClusterExtensions.SelectServerAndPinIfNeeded(ICluster cluster, ICoreSessionHandle session, IServerSelector selector, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Bindings.ReadPreferenceBinding.GetReadChannelSource(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Bindings.ReadBindingHandle.GetReadChannelSource(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.Initialize(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.Create(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.ReadCommandOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoDatabaseImpl.ExecuteReadOperation[T](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoDatabaseImpl.RunCommand[TResult](IClientSessionHandle session, Command`1 command, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoDatabaseImpl.<>c__DisplayClass50_0`1.<RunCommand>b__0(IClientSessionHandle session)
   at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoDatabaseImpl.RunCommand[TResult](Command`1 command, ReadPreference readPreference, CancellationToken cancellationToken)
   at Program.Main() in /path/to/cs/mongodb_cs/Program.cs:line 29

我什至没有通过connectionUri3 ping通

我已经用这个连接字符串检查了 mongocompass

mongodb://Mongodb:27020/?tls=true&tlsCAFile=D%3A%5Cca.pem&tlsCertificateKeyFile=D%3A%5Cmongodb.pem&authMechanism=MONGODB-X509&authSource=%24external

并且能够很好地连接

我在linux ubuntu中运行mongodb和程序

$ mongod --version
db version v7.0.2
Build Info: {
    "version": "7.0.2",
    "gitVersion": "02b3c655e1302209ef046da6ba3ef6749dd0b62a",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal

我通过服务运行mongodb

$ sudo service mongod status
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-10-31 03:52:38 KST; 2h 30min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 944888 (mongod)
     Memory: 143.9M
     CGroup: /system.slice/mongod.service
             └─944888 /usr/bin/mongod --auth --tlsMode requireTLS --tlsCAFile /opt/mongodb/ssl/ca.pem --tlsCertificateKeyFile /opt/mongodb/ssl/mongodb.pem --config >
Oct 31 03:52:38 nw systemd[1]: Started MongoDB Database Server.
Oct 31 03:52:38 nw mongod[944888]: /usr/bin/mongod: /usr/local/lib/libcurl.so.4: no version information available (required by /usr/bin/mongod)
Oct 31 03:52:38 nw mongod[944888]: {"t":{"$date":"2023-10-30T18:52:38.076Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGOD>

.NET驱动版本

<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
c# linux mongodb runtime-error mongodb-.net-driver
1个回答
0
投票

.NET 驱动程序不支持通过连接字符串提供

tlsCertificateKeyFile
。您应该通过 MongoClientSettings.SslSettings.ClientCertificates 注册它。详情请参阅此处

.NET 驱动程序根本不支持

tlsCAFile
。您应该在操作系统级别注册 CA 证书。

© www.soinside.com 2019 - 2024. All rights reserved.