HomeManaged DatabasesDocument DatabasesHow to
Connect to a Database Instance
Jump toUpdate content

How to connect to a Document Database Instance

Reviewed on 01 August 2023 • Published on 01 August 2023
Security & Identity (IAM):

You may need certain IAM permissions to carry out some actions described on this page. This means:

  • you are the Owner of the Scaleway Organization in which the actions will be carried out, or
  • you are an IAM user of the Organization, with a policy granting you the necessary permission sets
Requirements:

How to connect via database client

  1. Click Document Databases under Managed Databases on the side menu. A list of your Database Instances displays.
  2. Click the database name or «See more Icon» > More info to access the Database Instance information page.
  3. Click «Download Icon» next to the TLS certificate to download it.
  4. Transfer the file to where you will execute the connection command.
  5. Copy the connection command located under Connection in the Database Instance information page.
  6. Paste the connection command in your terminal, and edit the username field with the login you chose when creating the database. The full command should look like the following:
    mongosh --tls --tlsCAFile rdb-databasename.pem 'mongodb://username@ip:port/databasename?authMechanism=PLAIN'
    • rdb-databasename.pem - the TLS certificate downloaded on step 3.
    • username - the username you defined upon Database Instance creation.
    • ip - your Database Instance’s endpoint IP address.
    • database name - the database name you entered upon Database Instance creation. The default is called rdb.
    • port - the port on which your Database Instance is accessible
  7. Enter your password. If the connection is successful, you should see the following message display on your console, and be able to write queries:
    The server generated these startup warnings when booting
    Powered by FerretDB v0.9.0 and PostgreSQL 14.6.

How to connect via an application

  1. Click Document Databases under Managed Databases on the side menu. A list of your Database Instances displays.

  2. Click the database name or «See more Icon» > More info to access the Database Instance information page.

  3. Click «Download Icon» next to the TLS certificate to download it.

  4. Transfer the file to where you will execute the connection command.

  5. Get the following connection information from the Database Instance information panel or the string displayed upon connection:

    • username: the username you defined upon Database Instance creation.
    • password: the user password you defined upon Database Instance creation.
    • IP: your Database Instance’s endpoint IP address.
    • port: the port on which your Database Instance is accessible.
    • cert_file_path: the path to your Database Instance TLS certificate.
  6. Enter the information above in your application configuration file.

    You can find examples below for Python, Nodejs, and Go applications:

    Python

    The following code shows you how to use the pymongo library to connect using TLS.

    from pymongo.mongo_client import MongoClient
    user_name = "<replace_with_user_name>"
    password = "<replace_with_password>"
    ip = "instance_ip"
    port = 1234
    cert_file_path = "<instance_certificate.crt>"
    # Estabish the tls connection:
    client = MongoClient(
    f'mongodb://{user_name}:{password}@{ip}:{port}',
    authMechanism='PLAIN',
    tls=True,
    tlsCAFile=cert_file_path,
    )
    # get the database you want to use
    db = client.rdb
    # get the collection cars
    cars = db["cars"]
    # prepare a new car to be inserted
    car = {"name": "Supercar", "price": 100000}
    # execute the insertion
    cars.insert_one(car)
    # read the car you just inserted
    car = cars.find_one()
    # print the car, it should contain an ObjectId
    print(car)

    Nodejs

    The following code shows you how to use the mongodb module to connect using TLS.

    const { MongoClient } = require("mongodb");
    userName = "<replace_with_user_name>"
    password = "<replace_with_password>"
    ip = "instance_ip"
    port = 1234
    certFilePath = "<instance_certificate.crt>"
    client = new MongoClient(
    `mongodb://${userName}:${password}@${ip}:${port}`, {
    tls: true,
    tlsCaFile: certFilePath,
    authMechanism: "PLAIN",
    })
    await client.connect();
    # get the database you want to use
    const db = client.db("rdb")
    # get the collection cars
    const cars = db.collection("cars")
    # prepare a new car to be inserted
    let car = {name: "Supercar", price: 100000}
    # execute the insertion
    cars.insertOne(car)
    # read the car you just inserted
    car = cars.findOne()
    # print the car, it should contain an ObjectId
    print(car)

    Go

    The following code shows you how to use the mongo driver to connect using TLS.

    package main
    import (
    "context"
    "fmt"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    )
    func main() {
    username := "<replace_with_user_name>"
    password := "<replace_with_password>"
    host := "<endpoint ip>"
    port := 1234 // replace with endpoint port number
    caCertPath := "<instance_certificate.crt>"
    // prepare the uri for the connection
    uri := fmt.Sprintf(
    "mongodb://%s:%s@%s:%d/rdb?tls=true&tlsCACert=%s&authMechanism=PLAIN",
    username,
    password,
    host,
    port,
    caCertPath,
    )
    ctx := context.Background()
    // connect to the database
    client, _ := mongo.Connect(ctx, options.Client().ApplyURI(uri))
    // get the database
    db := client.Database("rdb")
    // get the collection
    cars := db.Collection("cars")
    // insert a document
    carToInsert := Car{Name: "Supercar", Year: 2020}
    cars.InsertOne(ctx, carToInsert)
    // read the document
    carToRead := Car{}
    cars.FindOne(ctx, map[string]interface{}{"name": "Supercar"}).Decode(&carToRead)
    // print the document
    fmt.Println(carToRead)
    }
    type Car struct {
    Name string
    Year int
    }
See Also