I was trying to connect to a local mongodb server using connectionuri
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 and 2 works just fine, I could ping and perform read or write for connectionUri3, it produces timeouterror
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
I didnt even get through the ping with connectionUri3
I already checked with mongocompass with this connection string
mongodb://Mongodb:27020/?tls=true&tlsCAFile=D%3A%5Cca.pem&tlsCertificateKeyFile=D%3A%5Cmongodb.pem&authMechanism=MONGODB-X509&authSource=%24external
and are able to connect just fine
I run both mongodb and the program in linux ubuntu
$ 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
I run the mongodb through service
$ 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 Driver version
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
Providing tlsCertificateKeyFile
is not supported by .NET driver via connection string. You should register it via MongoClientSettings.SslSettings.ClientCertificates. See here for details.
tlsCAFile
is not supported by .NET driver at all. You should register a CA certificate on OS level.