Overview
在本指南中,您可以学习;了解如何使用连接字符串和 Client实例连接到不同类型的MongoDB部署。
连接到 Atlas 部署
要连接到MongoDB 上的Atlas 部署,请在连接string 中包含以下元素:
Atlas 集群的 URL
MongoDB 用户名
MongoDB 密码
然后,使用连接字符串构造一个 Client实例。
提示
按照 Atlas驱动程序连接指南检索连接string 。
当您连接到Atlas时,我们建议使用 Stable API客户端选项,以避免Atlas升级到新版本的MongoDB Server时发生重大更改。要学习;了解有关 Stable API功能的更多信息,请参阅 Stable API指南。
以下代码显示如何创建使用 Atlas 连接字符串和 Stable API 版本的客户端、连接到 MongoDB 并验证连接是否成功。 从下面的 Asynchronous API或Synchronous API标签页中选择相应的连接代码示例。
提示
如需了解异步和同步运行时相关详情,请参阅《异步和同步 API 指南》。
use mongodb::{ bson::doc, options::{ ClientOptions, ServerApi, ServerApiVersion }, Client }; async fn main() -> mongodb::error::Result<()> { // Replace the placeholder with your Atlas connection string let uri = "<connection string>"; let mut client_options = ClientOptions::parse(uri).await?; // Set the server_api field of the client_options object to Stable API version 1 let server_api = ServerApi::builder().version(ServerApiVersion::V1).build(); client_options.server_api = Some(server_api); // Create a new client and connect to the server let client = Client::with_options(client_options)?; // Send a ping to confirm a successful connection client.database("admin").run_command(doc! { "ping": 1 }).await?; println!("Pinged your deployment. You successfully connected to MongoDB!"); Ok(()) }
use mongodb::{ bson::doc, options::{ ClientOptions, ServerApi, ServerApiVersion }, sync::Client }; fn main() -> mongodb::error::Result<()> { // Replace the placeholder with your Atlas connection string let uri = "<connection string>"; let mut client_options = ClientOptions::parse(uri).run()?; // Set the server_api field of the client_options object to Stable API version 1 let server_api = ServerApi::builder().version(ServerApiVersion::V1).build(); client_options.server_api = Some(server_api); // Create a new client and connect to the server let client = Client::with_options(client_options)?; // Send a ping to confirm a successful connection client.database("admin").run_command(doc! { "ping": 1 }).run()?; println!("Pinged your deployment. You successfully connected to MongoDB!"); Ok(()) }
连接 MongoDB 的其他方式
如果必须连接到未托管在 Atlas 上的单个 MongoDB 服务器实例或副本集,请参阅以下部分了解如何连接。
连接到本地计算机上的 MongoDB Server
如果出于开发目的必须在本地计算机上运行 MongoDB Server,则须完成以下步骤:
按照安装 MongoDB 教程在您的计算机上安装 MongoDB Server。根据您的计算机和操作系统选择相应的安装教程。
安装完毕后,启动该服务器。
重要
务必保护您的服务器免受恶意攻击。请参阅安全检查清单,获取安全建议清单。
成功启动 MongoDB Server 后,执行以下步骤以连接到本地实例:
将前面示例中
uri变量所存储的连接字符串替换为本地 MongoDB 实例的连接字符串。如果您的 MongoDB Server 在本地运行,则可使用以下连接字符串连接到 MongoDB:
mongodb://localhost:<port> 在此连接字符串中,
<port>为您配置的服务器侦听传入连接的端口号。运行连接代码。如果代码执行成功,您应该会在控制台中看到以下输出:
Pinged your deployment. You successfully connected to MongoDB!
提示
要了解有关连接字符串和自定义格式的更多信息,请参阅服务器手册中的连接字符串。
连接到副本集
MongoDB 副本集部署是一组连接的实例或节点,其中节点存储相同数据集。这种实例配置提供了数据冗余和高数据可用性。
要连接到副本集部署,请指定每个实例的主机名和端口号(以逗号分隔),并将副本集名称指定为连接字符串中 replicaSet 参数的值。
在以下示例中,主机名为 host1、host2 和 host3,端口号均为 27017。副本集名称为 myRS。以下代码显示了使用这些规范的副本集的连接 URI:
mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS
连接到副本集时,驱动程序默认执行以下操作:
提示
您只需要指定一台主机来连接到副本集。但是,为了确保指定主机不可用时保持连接,请提供完整的主机列表。
DirectConnection
要强制在连接 URI 中指定的主机上执行操作,请指定 directConnection 选项。直接连接具有以下行为:
注意
Docker 中的副本集
当副本集在Docker中运行时,它可能只公开一个MongoDB端点。在这种情况下,无法发现副本集。在连接 URI 中指定 directConnection=false 或未设置此选项,可能会阻止应用程序与之连接。
在测试或开发环境中,您可以通过指定 directConnection=true 来连接到副本集。在生产环境中,我们建议配置集群,以便每个 MongoDB 实例都可以在 Docker 虚拟网络之外访问。
通过 DNS 服务发现进行连接
要使用 DNS 服务发现来查找要连接的服务的 DNS SRV记录,请在连接字符串中指定 SRV 连接格式。如果指定此格式, Rust驾驶员会自动重新扫描新主机。您的部署可以将主机添加到其拓扑结构中,而无需更改客户端配置。
以下代码显示了使用 SRV 连接格式的连接字符串:
let uri = "mongodb+srv://<hostname>/";
要学习;了解有关 SRV 连接格式的更多信息,请参阅MongoDB Server手册中的 SRV 连接格式 条目。