Atlas MongoDB cluster connection string explained - is it tls connection?
The connection string e.g. mongodb+srv://sagar:[email protected]/aomedb
uses the DNS Seed List connection format (denoted by +srv
). This format is designed to simplify connecting to MongoDB clusters hosted on MongoDB Atlas. The +srv
prefix allows for automatic discovery of the cluster's nodes and simplifies the connection process.
Determining TLS or TCP
-
TLS/SSL: By default, MongoDB Atlas connections use TLS/SSL for secure communication. The
mongodb+srv
protocol implies that the connection will be secure by default when connecting to an Atlas cluster, unless explicitly configured otherwise. -
TCP: The
mongodb+srv
connection string itself does not explicitly specify whether the connection is over TCP or TLS. However, given the default security settings of MongoDB Atlas, it is safe to assume that the connection is using TLS.
Example Configuration
If you want to be explicit about using TLS, you can add parameters to the connection string, or you can configure it in your code:
Connection String with TLS Parameters
You can explicitly specify TLS in the connection string by adding tls=true
(or ssl=true
, as ssl
is synonymous with tls
in MongoDB).
mongodb+srv://sagar:[email protected]/awesomedb?tls=true
Using MongoDB Node.js Driver
Here’s how you can ensure TLS/SSL is used explicitly in the MongoDB Node.js driver:
const { MongoClient } = require('mongodb');
const uri = 'mongodb+srv://sagar:[email protected]/awesomedb';
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
tls: true, // Ensures the connection uses TLS
};
const client = new MongoClient(uri, options);
async function connectToDatabase() {
try {
await client.connect();
console.log('Connected to database with TLS/SSL');
} catch (err) {
console.error('Connection error', err);
}
}
connectToDatabase();