Connect your application

Connect with mongosh and with the official MongoDB driver for your language.

Connect mongosh or your application to TeselaDB with the official MongoDB driver for your language. TeselaDB presents itself to drivers as a MongoDB mongos, so the driver connects without changes.

Requirements

  • The address of a node of the cluster.
  • A user and its password. To begin with you can use root, whose password is in root-credentials.txt.
  • The pki/ca.pem file of the cluster, because the client port always requires TLS.

The connection string

mongodb://root:<password>@127.0.0.1:27017/?tls=true&tlsCAFile=./data/pki/ca.pem
  • tls=true turns TLS on. Without it, the node refuses the connection.
  • tlsCAFile points to pki/ca.pem in the data directory of the first node. Copy that file to the machines of your applications; it is not a secret.
  • The root password is in root-credentials.txt. For your applications, create their own user with only the roles they need (see Users and roles).

mongosh

Shell
mongosh "mongodb://root:<password>@127.0.0.1:27017/?tls=true&tlsCAFile=./data/pki/ca.pem"

From your language

Every official driver accepts the same connection string. These are four examples; the other drivers (C#, Rust, PHP, Ruby, Kotlin, Swift, C and C++) work the same way.

Node.js

JavaScript
import { MongoClient } from "mongodb";

const client = new MongoClient("mongodb://root:<password>@127.0.0.1:27017/?tls=true&tlsCAFile=./data/pki/ca.pem");
await client.connect();
await client.db("shop").collection("orders").insertOne({ total: 149.5 });

Python

Python
from pymongo import MongoClient

client = MongoClient(
    "mongodb://root:<password>@127.0.0.1:27017/?tls=true&tlsCAFile=./data/pki/ca.pem"
)
client.shop.orders.insert_one({"total": 149.5})

Go

Go
client, err := mongo.Connect(options.Client().ApplyURI(
    "mongodb://root:<password>@127.0.0.1:27017/?tls=true&tlsCAFile=./data/pki/ca.pem",
))
if err != nil {
    panic(err)
}
_, err = client.Database("shop").Collection("orders").InsertOne(ctx, bson.M{"total": 149.5})

Java

Java
try (MongoClient client = MongoClients.create(
        "mongodb://root:<password>@127.0.0.1:27017/?tls=true")) {
    client.getDatabase("shop").getCollection("orders").insertOne(new Document("total", 149.5));
}

In Java the CA goes in a trust store instead of the string. The steps are in Java.

Several nodes in the string

Any node routes each request to the range it touches, so one is enough. Put two or three in the string so the driver has another to go to if one does not answer:

mongodb://node-01:27017,node-02:27017,node-03:27017/?tls=true&tlsCAFile=./ca.pem

Check

In mongosh, a ping to the node returns { ok: 1 } when the connection and the user are right:

JavaScript
db.runCommand({ ping: 1 });

Common problems

  • If the connection closes before authenticating, tls=true is missing from the string. The node refuses connections without TLS.
  • If the driver does not trust the node certificate, tlsCAFile does not point to the pki/ca.pem of this cluster. Each cluster has its own certificate authority.
  • If authentication fails, check in the string the user, its password and the authentication database (authSource).

See also