Overview
In this guide, you can learn how to authenticate to MongoDB by using the authentication mechanisms available in MongoDB Server. Authentication mechanisms are processes by which the driver and server confirm the identity of a client to ensure security before connecting.
Tip
Connecting to MongoDB
To learn how to establish a connection to your MongoDB deployment, see the Get Started with the C++ Driver guide.
MongoDB Edition Compatibility
The following table lists the authentication mechanisms supported by MongoDB and the MongoDB Server editions that each mechanism is compatible with. Click the name of a mechanism to learn more about how to use it with your application.
Authentication Mechanism | Atlas | Enterprise Advanced | Community |
|---|---|---|---|
Yes | Yes | Yes | |
Yes | Yes | Yes | |
Yes | No | No | |
Yes | Yes | No | |
No | Yes | No |
Error Handling
You can handle authentication errors by catching
mongocxx::exception errors. Inspect the error message string
by using the what() method, as shown in the following
example:
try { auto uri = mongocxx::uri("<connection string>"); auto client = mongocxx::client(uri); client["db"].run_command(<any command requiring authorization>); } catch (const mongocxx::exception& ex) { std::cerr << "Error: " << ex.what() << std::endl; }
For more information, see Operation Exceptions in the API documentation.