How to connect to a Document Database Instance
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
- You have an account and are logged into the Scaleway console
- You have a Document Database Instance
How to connect via database client
- Click Document Databases under Managed Databases on the side menu. A list of your Database Instances displays.
- Click the database name or «See more Icon» > More info to access the Database Instance information page.
- Click «Download Icon» next to the TLS certificate to download it.
- Transfer the file to where you will execute the connection command.
- Copy the connection command located under Connection in the Database Instance information page.
- 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 calledrdb
.port
- the port on which your Database Instance is accessible
- 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 bootingPowered by FerretDB v0.9.0 and PostgreSQL 14.6.
How to connect via an application
-
Click Document Databases under Managed Databases on the side menu. A list of your Database Instances displays.
-
Click the database name or «See more Icon» > More info to access the Database Instance information page.
-
Click «Download Icon» next to the TLS certificate to download it.
-
Transfer the file to where you will execute the connection command.
-
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.
-
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 MongoClientuser_name = "<replace_with_user_name>"password = "<replace_with_password>"ip = "instance_ip"port = 1234cert_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 usedb = client.rdb# get the collection carscars = db["cars"]# prepare a new car to be insertedcar = {"name": "Supercar", "price": 100000}# execute the insertioncars.insert_one(car)# read the car you just insertedcar = cars.find_one()# print the car, it should contain an ObjectIdprint(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 = 1234certFilePath = "<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 useconst db = client.db("rdb")# get the collection carsconst cars = db.collection("cars")# prepare a new car to be insertedlet car = {name: "Supercar", price: 100000}# execute the insertioncars.insertOne(car)# read the car you just insertedcar = cars.findOne()# print the car, it should contain an ObjectIdprint(car)Go
The following code shows you how to use the
mongo
driver to connect using TLS.package mainimport ("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 numbercaCertPath := "<instance_certificate.crt>"// prepare the uri for the connectionuri := 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 databaseclient, _ := mongo.Connect(ctx, options.Client().ApplyURI(uri))// get the databasedb := client.Database("rdb")// get the collectioncars := db.Collection("cars")// insert a documentcarToInsert := Car{Name: "Supercar", Year: 2020}cars.InsertOne(ctx, carToInsert)// read the documentcarToRead := Car{}cars.FindOne(ctx, map[string]interface{}{"name": "Supercar"}).Decode(&carToRead)// print the documentfmt.Println(carToRead)}type Car struct {Name stringYear int}