Home  Database   Atlas mongo ...

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

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();
Published on: Jul 12, 2024, 12:08 AM  
 

Comments

Add your comment