Overview
在本指南中,您可以了解如何使用连接string和 MongoClient
对象连接到不同类型的MongoDB部署。
Atlas
要连接到MongoDB 上的Atlas 部署,请在连接string 中包含以下元素:
Atlas 集群的 URL
MongoDB 用户名
MongoDB 密码
然后,将连接string传递给 MongoClient
构造函数。
提示
按照 Atlas驱动程序连接指南检索连接string 。
当您连接到Atlas时,我们建议使用 Stable API客户端选项,以避免Atlas升级到新版本的MongoDB Server时发生重大更改。 要学习;了解有关 Stable API功能的更多信息,请参阅 Stable API指南。
以下代码显示了如何使用.NET/ C#驱动程序连接到Atlas 集群。该代码还使用 server_api
选项来指定 Stable API版本。
using MongoDB.Driver; using MongoDB.Bson; // Replace the placeholder with your Atlas connection string const string connectionUri = "<connection string>"; var settings = MongoClientSettings.FromConnectionString(connectionUri); // Sets the ServerApi field of the settings object to Stable API version 1 settings.ServerApi = new ServerApi(ServerApiVersion.V1); // Creates a new client and connects to the server var client = new MongoClient(settings); // Sends a ping to confirm a successful connection try { var result = client.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1)); Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!"); } catch (Exception ex) { Console.WriteLine(ex);}
本地部署
要连接到本地 MongoDB 部署,请使用localhost
作为主机名。 默认情况下, mongod
进程在端口27017上运行,但您可以根据部署进行自定义。
以下代码展示了如何使用.NET/ C#驱动程序连接到本地MongoDB 部署:
using MongoDB.Driver; // Sets the connection URI const string connectionUri = "mongodb://localhost:27017"; // Creates a new client and connects to the server var client = new MongoClient(connectionUri);
副本集
要连接到副本集,请在连接IP 中指定副本集集节点的主机名(或string 地址)和端口号。
以下代码展示了如何使用.NET/ C#驱动程序连接到包含三个主机的副本集:
using MongoDB.Driver; // Sets the connection URI than includes the list of hosts in the replica set const string connectionUri = "mongodb://host1:27017,host2:27017,host3:27017"; // Creates a new client and connects to the server var client = new MongoClient(connectionUri);
注意
Docker 中的副本集
当副本集在Docker中运行时,它可能只公开一个MongoDB端点。在这种情况下,无法发现副本集。在连接 URI 中指定 directConnection=false
或未设置此选项,可能会阻止应用程序与之连接。
在测试或开发环境中,您可以通过指定 directConnection=true
来连接到副本集。在生产环境中,我们建议配置集群,以便每个 MongoDB 实例都可以在 Docker 虚拟网络之外访问。
如果无法提供副本集主机的完整列表,则可以指定副本集的一个或多个主机,并指示.NET/ C#驱动程序执行自动发现以查找其他主机。要指示驾驶员执行自动发现,请执行以下操作之一:
将副本集的名称指定为
replicaSet
参数的值。将
false
指定为directConnection
参数的值。您也可以省略此参数,因为它的默认值为false
。在副本集中指定多个主机。
在以下示例中,驱动程序使用样本连接 URI 连接到 MongoDB 副本集 sampleRS
,该副本集在三个不同主机(包括 host1
)的端口 27017
上运行:
using MongoDB.Driver; // Sets the connection URI than includes the replica set name const string connectionUri = "mongodb://host1:27017/?replicaSet=sampleRS"; // Creates a new client and connects to the server var client = new MongoClient(connectionUri);
注意
指定副本集名称
虽然驾驶员可以自动发现副本集节点,而无需指定所有节点的主机名或副本集名称,但我们建议指定副本集名称,以避免副本集无法正确初始化的极端情况。
.NET/ C#驱动程序在客户端的localThresholdMS
值内可访问的部署之间均衡操作负载。要进一步学习;了解.NET/ C#驱动程序如何在多个MongoDB部署之间实现操作负载均衡,请参阅 自定义服务器选择指南。
注意
MongoClient
构造函数是非阻塞的。 连接到副本集时,构造函数会立即返回,而客户端会使用后台线程连接到副本集。
如果构造 MongoClient
并立即打印其 nodes
属性的字符串表示形式,则当客户端连接到副本集成员时,列表可能为空。
连接到单个服务器
要连接到副本集的单个服务器而不是整个副本集,请将 false
指定为 directConnection
连接选项的值。您可以通过两种方式执行此操作:将参数传递给 MongoClient
构造函数,或通过连接字符串中的参数。选择 MongoClientSettings 或 Connection String标签页以查看相应的代码。
using MongoDB.Driver; var settings = MongoClientSettings.FromConnectionString("mongodb://host1:27017"); settings.DirectConnection = true; var client = new MongoClient(settings);
using MongoDB.Driver; const string connectionUri = "mongodb://host1:27017/?directConnection=true"; var client = new MongoClient(connectionUri);
DNS 服务发现
要使用 DNS 服务发现来查找要连接的服务的 DNS SRV记录,请在连接字符串中指定 SRV 连接格式。此外,如果启用SRV 连接格式, .NET/ C#驱动程序会自动重新扫描新主机,而无需更改客户端配置。
以下代码显示了使用 SRV 连接格式的连接字符串:
var uri = "mongodb+srv://<hostname>"
要学习;了解有关 SRV 连接格式的更多信息,请参阅MongoDB Server手册中的 SRV 连接格式 条目。
API 文档
要学习;了解有关本指南中讨论的类型的更多信息,请参阅以下API文档: