LDAP (PLAIN) Authentication Mechanism
Overview
The PLAIN
authentication mechanism allows you to use your Lightweight Directory
Access Protocol (LDAP) username and password to authenticate to MongoDB. LDAP
authentication uses the PLAIN Simple Authentication and Security Layer (SASL)
defined in RFC-4616.
You can use this mechanism only when authenticating to MongoDB Atlas or MongoDB Enterprise Advanced.
Code Placeholders
The code examples on this page use the following placeholders:
<ldap_username>
: Your LDAP username.<ldap_password>
: Your LDAP password.<cluster_url>
: The network address of your MongoDB deployment.
To use the code examples, replace these placeholders with your own values.
LDAP (PLAIN)
The PLAIN
authentication mechanism uses your username and password to
authenticate to an LDAP server.
You can specify this authentication mechanism by setting the authMechanism
parameter to PLAIN
and including your LDAP username and password in the
connection string as shown
in the following sample code.
const { MongoClient } = require("mongodb"); // specify the placeholder values for your environment in the following lines const clusterUrl = "<cluster_url>"; const ldapUsername = "<ldap_username>"; const ldapPassword = "<ldap_password>"; const authMechanism = "PLAIN"; // Connection URI const uri = `mongodb+srv://${ldapUsername}:${ldapPassword}@${clusterUrl}/?authMechanism=${authMechanism}`; const client = new MongoClient(uri); // Function to connect to the server async function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); } } run().catch(console.dir);
API Documentation
To learn more about any of the methods or types discussed on this page, see the following API documentation: