文档菜单
文档首页
/ / /
C 驱动程序
/

选择连接目标

本页内容

  • 概述
  • Atlas
  • 本地部署
  • 副本集
  • API 文档

在本指南中,您可以学习如何使用连接字符串和mongoc_client_t 对象连接到不同类型的 MongoDB 部署。

要连接到Atlas上的MongoDB部署,请在连接字符串中包含以下元素

  • 您的Atlas集群的URL

  • 您的MongoDB用户名

  • 您的MongoDB密码

然后,将您的连接字符串传递给mongoc_client_new()函数。

提示

按照Atlas驱动程序连接指南检索您的连接字符串。

以下代码展示了如何使用C驱动程序连接到Atlas集群。代码还使用mongoc_server_api_new()函数指定稳定API版本。

bson_t *ping = BCON_NEW ("ping", BCON_INT32 (1));
bson_t reply = BSON_INITIALIZER;
bson_error_t error;
mongoc_client_t *client = NULL;
// Create a new client
client = mongoc_client_new ("mongodb+srv://<db_username>:<db_password>@<hostname:port>/?<options>");
// Set the version of the Stable API on the client
mongoc_server_api_t *api = mongoc_server_api_new(MONGOC_SERVER_API_V1);
if (!mongoc_client_set_server_api(client, api, &error)) {
fprintf(stderr, "error: %s\n", error.message);
goto cleanup;
}
// Send a ping to confirm a successful connection
if (!mongoc_client_command_simple (client, "admin", ping, NULL, &reply, &error)) {
fprintf(stderr, "error: %s\n", error.message);
goto cleanup;
}
printf ("Pinged your deployment. You successfully connected to MongoDB!\n");
cleanup:
bson_destroy (&reply);
bson_destroy (ping);
mongoc_server_api_destroy (api);
mongoc_client_destroy (client);

提示

当您连接到Atlas时,我们建议使用稳定API客户端选项,以避免Atlas升级到MongoDB服务器的新版本时出现破坏性更改。有关稳定API的更多信息,请参阅稳定API指南

要连接到本地MongoDB部署,请使用localhost作为主机名。默认情况下,mongod进程在端口27017上运行,尽管您可以为您自己的部署自定义此设置。

以下代码展示了如何使用C驱动程序连接到本地MongoDB部署

bson_t *ping = BCON_NEW ("ping", BCON_INT32 (1));
bson_t reply = BSON_INITIALIZER;
bson_error_t error;
mongoc_client_t *client = NULL;
// Create a new client
mongoc_uri_t *uri = mongoc_uri_new_with_error ("mongodb://localhost:27017", &error);
if (!uri) {
fprintf (stderr, "failed to parse URI, error: %s\n", error.message);
goto cleanup;
}
client = mongoc_client_new_from_uri (uri);
if (!client) {
goto cleanup;
}
// Send a ping to confirm a successful connection
if (!mongoc_client_command_simple (client, "admin", ping, NULL, &reply, &error)) {
fprintf(stderr, "error: %s\n", error.message);
goto cleanup;
}
printf ("Pinged your deployment. You successfully connected to MongoDB!\n");
cleanup:
bson_destroy (&reply);
bson_destroy (ping);
mongoc_client_destroy (client);
mongoc_uri_destroy (uri);

要连接到副本集,请指定副本集成员的主机名(或IP地址)和端口号。

您不需要提供副本集的完整主机列表,可以指定副本集中的一台或多台主机,并指导C驱动程序自动发现其他主机。要指导驱动程序执行自动发现,请执行以下操作之一

  • 将副本集的名称指定为replicaSet参数的值。

  • false指定为directConnection参数的值。

  • 指定副本集中一台以上的主机。

以下示例使用一个示例连接URI连接到运行在三个不同主机上端口27017的MongoDB副本集myreplset

bson_t *ping = BCON_NEW ("ping", BCON_INT32 (1));
bson_t reply = BSON_INITIALIZER;
bson_error_t error;
mongoc_client_t *client = NULL;
// Create a new client
client = mongoc_client_new ("mongodb://host01:27017,host02:27017,host03:27017/?replicaSet=myreplset");
if (!client) {
goto cleanup;
}
// Send a ping to confirm a successful connection
if (!mongoc_client_command_simple (client, "admin", ping, NULL, &reply, &error)) {
fprintf(stderr, "error: %s\n", error.message);
goto cleanup;
}
printf ("Pinged your deployment. You successfully connected to MongoDB!\n");
cleanup:
bson_destroy (&reply);
bson_destroy (ping);
mongoc_client_destroy (client);

注意

mongoc_client_new()函数是非阻塞的。当您连接到副本集时,构造函数会立即返回,而客户端使用后台线程连接到副本集。

如果您构建了一个mongoc_client_t对象,并立即打印其nodes属性的字符串表示形式,在客户端连接到副本集成员时,列表可能为空。

有关本指南中提到的对象和函数的更多信息,请参阅以下API文档

返回

稳定 API