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 inroot-credentials.txt. - The
pki/ca.pemfile 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.pemtls=trueturns TLS on. Without it, the node refuses the connection.tlsCAFilepoints topki/ca.pemin the data directory of the first node. Copy that file to the machines of your applications; it is not a secret.- The
rootpassword is inroot-credentials.txt. For your applications, create their own user with only the roles they need (see Users and roles).
mongosh
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
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
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
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
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.pemCheck
In mongosh, a ping to the node returns { ok: 1 } when the connection and the user are right:
db.runCommand({ ping: 1 });Common problems
- If the connection closes before authenticating,
tls=trueis missing from the string. The node refuses connections without TLS. - If the driver does not trust the node certificate,
tlsCAFiledoes not point to thepki/ca.pemof 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
- Deploy with systemd, to add nodes and see how the ranges spread.
- Roles and privileges, to create the user of your application.