mongodb/mongo-cxx-driverMongoDB是一个跨平台的文档型NoSQL数据库,使用类JSON文档和可选模式。本Docker镜像提供MongoDB的C++驱动(mongo-cxx-driver)和C驱动(mongo-c-driver,又称libmongoc),用于在开发环境中连接MongoDB数据库。该镜像基于Red Hat UBI(通用基础镜像)构建,包含开发所需的驱动库和头文件。
重要提示:本镜像提供的标签仅用于开发目的,不接收安全更新。
本镜像适用于以下场景:
首先,获取MongoDB数据库服务访问权限。最简单的方式是使用Atlas,可免费运行M0实例(详见免费Atlas集群)。
Dockerfile# Dockerfile FROM mongodb/mongo-cxx-driver:3.10.1-redhat-ubi-9.4 WORKDIR /build RUN microdnf upgrade -y && microdnf install -y g++ COPY ping.cpp /build/ RUN g++ \ -o ping \ ping.cpp \ -I/usr/local/include/bsoncxx/v_noabi/ \ -I/usr/local/include/mongocxx/v_noabi/ \ -lmongocxx \ -lbsoncxx CMD /build/ping
创建ping.cpp文件,用于测试连接MongoDB。连接字符串通过环境变量在运行时获取:
C// ping.cpp #include <cstdlib> #include <string> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> std::string lookup_env(const std::string &name) { char *env = std::getenv(name.c_str()); if (!env) { throw std::runtime_error("缺少环境变量: " + name); } return env; } int main() { try { // 创建实例 mongocxx::instance inst{}; std::string connection_string = lookup_env("MONGO_CONNECTION_STRING"); const auto uri = mongocxx::uri{connection_string}; // 设置客户端的稳定API版本 mongocxx::options::client client_options; const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1}; client_options.server_api_opts(api); // 建立连接并获取"admin"数据库句柄 mongocxx::client conn{uri, client_options}; mongocxx::database db = conn["admin"]; // Ping数据库 const auto ping_cmd = bsoncxx::builder::basic::make_document( bsoncxx::builder::basic::kvp("ping", 1)); db.run_command(ping_cmd.view()); std::cout << "使用MongoDB C++驱动Ping部署成功。 " << "已成功连接到MongoDB!" << std::endl; } catch (const std::exception &e) { // 错误处理 std::cerr << "异常: " << e.what() << std::endl; } return 0; }
确保Dockerfile和ping.cpp在同一目录:
$ tree . . ├── Dockerfile └── ping.cpp
shdocker build . -t mongocxx-ping
构造MongoDB连接字符串(Atlas集群示例):
'mongodb+srv://<用户名>:<密码>@<数据库URL>/'
在Atlas中,可通过以下步骤获取连接字符串:导航至“Deployment”下的“Database”,选择“Connect”,点击“Drivers”,选择“C++”驱动,填写密码参数。更多连接字符串构造说明见MongoDB文档。
运行容器:
shdocker run --env MONGO_CONNECTION_STRING='<你的连接字符串>' --rm mongocxx-ping
使用MongoDB C++驱动Ping部署成功。 已成功连接到MongoDB!
由于C++驱动是C驱动的封装,本镜像也包含MongoDB C驱动。
同C++驱动,获取MongoDB数据库服务访问权限(如Atlas免费M0实例)。
Dockerfile# Dockerfile FROM mongodb/mongo-cxx-driver:3.10.1-redhat-ubi-9.4 WORKDIR /build RUN microdnf upgrade -y && microdnf install -y gcc COPY ping.c /build/ RUN gcc \ -o ping \ ping.c \ -I/usr/local/include/libmongoc-1.0/ \ -I/usr/local/include/libbson-1.0 \ -L/usr/local/lib64/ \ -lmongoc-1.0 \ -lbson-1.0 CMD /build/ping
创建ping.c文件,用于测试连接MongoDB。连接字符串通过环境变量在运行时获取:
C/* ping.c */ #include <mongoc/mongoc.h> int main(void) { mongoc_client_t *client = NULL; bson_error_t error = {0}; mongoc_server_api_t *api = NULL; mongoc_database_t *database = NULL; bson_t *command = NULL; bson_t reply = BSON_INITIALIZER; int rc = 0; bool ok = true; /* 初始化MongoDB C驱动 */ mongoc_init(); const char *connection_string = getenv("MONGO_CONNECTION_STRING"); if (!connection_string) { fprintf( stderr, "缺少环境变量 'MONGO_CONNECTION_STRING'\n" ); rc = 1; goto cleanup; } client = mongoc_client_new(connection_string); if (!client) { fprintf(stderr, "创建MongoDB客户端失败\n"); rc = 1; goto cleanup; } /* 设置客户端的稳定API版本 */ api = mongoc_server_api_new(MONGOC_SERVER_API_V1); if (!api) { fprintf(stderr, "创建MongoDB服务器API失败\n"); rc = 1; goto cleanup; } ok = mongoc_client_set_server_api(client, api, &error); if (!ok) { fprintf(stderr, "错误: %s\n", error.message); rc = 1; goto cleanup; } /* 获取"admin"数据库句柄 */ database = mongoc_client_get_database(client, "admin"); if (!database) { fprintf(stderr, "获取MongoDB数据库句柄失败\n"); rc = 1; goto cleanup; } /* Ping数据库 */ command = BCON_NEW("ping", BCON_INT32(1)); ok = mongoc_database_command_simple( database, command, NULL, &reply, &error ); if (!ok) { fprintf(stderr, "错误: %s\n", error.message); rc = 1; goto cleanup; } bson_destroy(&reply); printf( "使用MongoDB C驱动Ping部署成功。 " "已成功连接到MongoDB!\n" ); cleanup: bson_destroy(command); mongoc_database_destroy(database); mongoc_server_api_destroy(api); mongoc_client_destroy(client); mongoc_cleanup(); return rc; }
确保Dockerfile和ping.c在同一目录:
$ tree . . ├── Dockerfile └── ping.c
shdocker build . -t mongoc-ping
构造MongoDB连接字符串(Atlas集群示例):
'mongodb+srv://<用户名>:<密码>@<数据库URL>/'
在Atlas中,可通过以下步骤获取连接字符串:导航至“Deployment”下的“Database”,选择“Connect”,点击“Drivers”,选择“C”驱动,填写密码参数。更多连接字符串构造说明见MongoDB文档。
运行容器:
shdocker run --env MONGO_CONNECTION_STRING='<你的连接字符串>' --rm mongoc-ping
使用MongoDB C驱动Ping部署成功。 已成功连接到MongoDB!
Apache License Version 2.0
manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务