Skip to content

Commit 35cb421

Browse files
feat(mdb): vpc - MTA-5613 (#4434)
1 parent a543c94 commit 35cb421

File tree

7 files changed

+212
-81
lines changed

7 files changed

+212
-81
lines changed

menu/navigation.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,6 +2564,10 @@
25642564
"label": "Create a Database Instance",
25652565
"slug": "create-a-database-instance"
25662566
},
2567+
{
2568+
"label": "Connect a Database Instance to a Private Network",
2569+
"slug": "connect-database-private-network"
2570+
},
25672571
{
25682572
"label": "Connect to a Database Instance",
25692573
"slug": "connect-database-instance"

pages/managed-databases-for-postgresql-and-mysql/how-to/connect-database-private-network.mdx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ Managed Databases for PostgreSQL and MySQL are currently not compatible with the
6363
You can use Public Gateways in [SSH bastion](/public-gateways/how-to/use-ssh-bastion/) mode and perform port forwarding.
6464
</Message>
6565

66-
<Message type="tip">
67-
If you are using Scaleway Kubernetes Kapsule, you can use the [scaleway-k8s-vpc](https://github.com/Sh4d1/scaleway-k8s-vpc) GitHub project to connect a Database Instance to your pods using a Private Network.<br /><br />
68-
Once your Kapsule node is connected to the Private Network you can directly reach the database by using the assigned service IP. There is no need to expose it through a Kubernetes resource like a service. The database is ready to use out of the box. <br /><br />
69-
**The `scaleway-k8s-vpc` is not an officially supported by Scaleway.**
70-
</Message>
71-
7266
## How to detach a Database Instance from a Private Network
7367

7468
1. Click **PostgreSQL and MySQL** under **Managed Databases** on the side menu. A list of your Database Instances displays.

pages/managed-databases-for-postgresql-and-mysql/how-to/create-a-database.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Compared to traditional database management, which requires customers to provide
5050
- Choose your storage type and capacity. Two types of storage are available:
5151
- **Block Storage Legacy** - With this type, your storage is decoupled from your compute resources. You can increase your storage space without changing your node type. You can define your storage capacity by entering the desired amount in the box. The volume can be increased up to 10 TB.
5252

53-
- **Block Storage**: This storage type works like the [Block Storage Legacy](/managed-databases-for-postgresql-and-mysql/concepts/#block-storage-legacy), while providing lower latency and high resiliency through 5k IOPS. You can increase your volume to up to 10 TB.
53+
- **Block Storage**: This storage type works like Block Storage Legacy, while providing lower latency and high resiliency through 5k or 15k IOPS. You can increase your volume to up to 10 TB.
5454

5555
- **Local SSD Storage** - Your storage is fixed and tied to your compute resource.
5656
<Message type="note">

pages/managed-databases-for-redis/how-to/create-a-database-for-redis.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Managed Database for Redis™<sup>*</sup> is a low-latency caching solution base
3838
- Select a node type.
3939
4. Configure your Network. You can select either:
4040

41-
- **Private Network**: allows your databases to communicate in an isolated and secure network without requiring a public IP address. If you select this option, you must either:
41+
- **Attach to Private Network**: allows your databases to communicate in an isolated and secure network without requiring a public IP address. If you select this option, you must either:
4242
- select an existing Private Network to attach from the drop-down or
4343
- create and attach a new one
4444

@@ -48,7 +48,7 @@ Managed Database for Redis™<sup>*</sup> is a low-latency caching solution base
4848
- Managed Databases for Redis™ are currently not compatible with the [Static NAT](/public-gateways/concepts/#nat) feature of [Scaleway Public Gateways](/public-gateways/concepts/#public-gateway). You can use Public Gateways in [SSH bastion](/public-gateways/how-to/use-ssh-bastion/) mode and perform port forwarding.
4949
</Message>
5050

51-
- **Public network**: resources in a public network are publicly visible by default. The default Access Control List (ACL) settings allow anybody to connect to resources in your network. You can restrict access by configuring ACLs.
51+
- **Set up public connectivity**: resources in a public network are publicly visible by default. The default Access Control List (ACL) settings allow anybody to connect to resources in your network. You can restrict access by configuring ACLs.
5252
5. Complete the remaining steps in the creation wizard:
5353
- Create credentials: add a name and set a password for your user.
5454
<Message type="important">

pages/managed-mongodb-databases/how-to/connect-database-instance.mdx

Lines changed: 107 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
meta:
3-
title: How to connect to a MongoDB® Database Instance
3+
title: How to connect to a Managed MongoDB® Database Instance
44
description: This page explains how to connect to a MongoDB® Database Instance
55
content:
6-
h1: How to connect to a MongoDB® Database Instance
6+
h1: How to connect to a Managed MongoDB® Database Instance
77
paragraph: This page explains how to connect to a MongoDB® Database Instance
88
tags: mongodb mongodb document database-instance managed-database database
99
dates:
10-
validation: 2024-09-18
10+
validation: 2024-02-17
1111
posted: 2024-09-18
1212
categories:
1313
- managed-databases
@@ -19,7 +19,6 @@ Managed MongoDB® is currently in public beta. Click [here](https://www.scaleway
1919

2020
<Macro id="requirements" />
2121

22-
2322
- A Scaleway account logged into the [console](https://console.scaleway.com)
2423
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
2524
- A [MongoDB® Database Instance](/managed-mongodb-databases/quickstart/)
@@ -38,7 +37,7 @@ Managed MongoDB® is currently in public beta. Click [here](https://www.scaleway
3837

3938
Find below a detailed description of each connection mode:
4039

41-
### mongosh
40+
### With mongosh
4241

4342
To connect to a public endpoint using the MongoDB® shell:
4443

@@ -67,19 +66,11 @@ If the connection is successful, you should see the following message display on
6766
rs-{db-instance-id} [primary] test>
6867
```
6968

70-
Follow the same procedure to connect to a private endpoint for one node, replacing `{privateNetworkId}` with the `ID` of your Private Network:
71-
72-
```sh
73-
mongosh "mongodb://{db-instance-id}-0.{privateNetworkId}" -u {username} --tlsCAFile {your_certificate.pem}
74-
```
75-
76-
For multiple nodes, replace `{db-instance-id}` with the Database Instance UUID of each respective Instance, and `{privateNetworkId}` with the `ID` of your Private Network:
77-
78-
```sh
79-
"mongodb://{db-instance-id}-0.{privateNetworkId},{db-instance-id}-1.{privateNetworkId},{db-instance-id}-2.{privateNetworkId}" -u {username} --tlsCAFile {your_certificate.pem}
80-
```
69+
<Message type="important">
70+
To connect to a MongoDB® via a Private Network, use the following command: `mongosh "mongodb+srv://{username}:{password}@{instance_id}.{private_network_id}.internal/?tls=true&tlsCAFile={tls_certificate}"`. Make sure you replace the variables indicated within the `{}` with their corresponding values.
71+
</Message>
8172

82-
### Python
73+
### With Pymongo
8374

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

@@ -115,8 +106,11 @@ documents = collection.find({})
115106
for doc in documents:
116107
print(doc)
117108
```
109+
<Message type="important">
110+
To connect to a MongoDB® via a Private Network, use the following connection string: `connection_string = f"mongodb+srv://{username}:{password}@{instance_id}.{private_network_id}.internal/?tls=true&tlsCAFile={tls_certificate}"`. Make sure you replace the variables indicated within the `{}` with their corresponding values.
111+
</Message>
118112

119-
### Node.js
113+
### With Node.js
120114

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

@@ -134,8 +128,10 @@ const databaseName = 'databaseName'
134128
// Path to your TLS certificate file
135129
const tlsCertificatePath = path.resolve(__dirname, 'path/to/your_tls_certificate.pem');
136130

137-
// Construct the connection string
138-
const connectionString = `mongodb+srv://${username}:${password}@${instanceId}.mgdb.${region}.scw.cloud`;
131+
// Construct the public connection string
132+
const connectionString = `mongodb+srv://${username}:${password}@${instanceId}.mgdb.${region}.scw.cloud;`
133+
// Construct the private connection string
134+
const connectionString = `mongodb+srv://${username}:${password}@${instanceId}.${privateNetworkId}.internal;`
139135

140136
// Create a new MongoClient
141137
const client = new MongoClient(connectionString, {
@@ -170,91 +166,140 @@ async function run() {
170166
run().catch(console.dir);
171167
```
172168

173-
### Go
169+
### With Go
174170

175171
The following code shows you how to use the `mongo` driver to connect using TLS.
176172
```go
177173
package main
178174

179175
import (
180176
"context"
177+
"crypto/tls"
178+
"crypto/x509"
181179
"fmt"
180+
"io/ioutil"
181+
"log"
182+
182183
"go.mongodb.org/mongo-driver/mongo"
183184
"go.mongodb.org/mongo-driver/mongo/options"
184185
)
185186

186187
func main() {
188+
// Replace with your MongoDB connection details
189+
username := "your_username"
190+
password := "your_password"
191+
instanceID := "your_instance_id" // your instance_id
192+
privateNetworkID := "your_private_network_id" // Id of your Private Network
193+
region := "your_region" // the region of your database instance. "fr-par" for Paris.
194+
tlsCertificate := "path/to/your_tls_certificate.pem" // path to your TLS certificate
195+
databaseName = "databaseName"
196+
197+
// Read CA certificate
198+
caCert, err := ioutil.ReadFile(tlsCertificate)
199+
if err != nil {
200+
log.Fatalf("Erreur lors de la lecture du fichier CA: %v", err)
201+
}
187202

188-
username := "<replace_with_user_name>"
189-
password := "<replace_with_password>"
190-
host := "<endpoint ip>"
191-
port := 1234 // replace with endpoint port number
192-
caCertPath := "<instance_certificate.crt>"
193-
// prepare the uri for the connection
194-
uri := fmt.Sprintf(
195-
"mongodb://%s:%s@%s:%d/rdb?tls=true&tlsCACert=%s&authMechanism=PLAIN",
196-
username,
197-
password,
198-
host,
199-
port,
200-
caCertPath,
201-
)
202-
ctx := context.Background()
203-
// connect to the database
204-
client, _ := mongo.Connect(ctx, options.Client().ApplyURI(uri))
205-
// get the database
206-
db := client.Database("rdb")
207-
// get the collection
208-
cars := db.Collection("cars")
209-
// insert a document
210-
carToInsert := Car{Name: "Supercar", Year: 2020}
211-
cars.InsertOne(ctx, carToInsert)
212-
213-
// read the document
214-
carToRead := Car{}
215-
cars.FindOne(ctx, map[string]interface{}{"name": "Supercar"}).Decode(&carToRead)
216-
217-
// print the document
218-
fmt.Println(carToRead)
203+
// Create certificate pool
204+
caCertPool := x509.NewCertPool()
205+
caCertPool.AppendCertsFromPEM(caCert)
219206

220-
}
207+
tlsConfig := &tls.Config{
208+
RootCAs: caCertPool,
209+
}
210+
211+
// Construct the public connection string
212+
connectionString := fmt.Sprintf("mongodb+srv://%s:%s@%s.mgdb.%s.scw.cloud", username, password, instanceID, region)
213+
// Construct the private connection string
214+
connectionString := fmt.Sprintf("mongodb+srv://%s:%s@%s.%s.internal", username, password, instanceID, privateNetworkID)
215+
216+
// Create a new client and connect to the server
217+
clientOptions := options.Client().
218+
ApplyURI(connectionString).
219+
SetTLSConfig(tlsConfig)
220+
client, err := mongo.Connect(context.TODO(), clientOptions)
221221

222-
type Car struct {
223-
Name string
224-
Year int
222+
if err != nil {
223+
log.Fatal(err)
224+
}
225+
226+
// Check the connection
227+
err = client.Ping(context.TODO(), nil)
228+
229+
if err != nil {
230+
log.Fatal(err)
231+
}
232+
233+
fmt.Println("Connected to MongoDB!")
234+
235+
// Access a specific collection
236+
collection := client.Database(databaseName).Collection("your_collection_name")
237+
238+
// Example: Find documents in the collection
239+
cursor, err := collection.Find(context.TODO(), map[string]interface{}{})
240+
241+
if err != nil {
242+
log.Fatal(err)
243+
}
244+
245+
defer cursor.Close(context.TODO())
246+
247+
for cursor.Next(context.TODO()) {
248+
var result map[string]interface{}
249+
err := cursor.Decode(&result)
250+
if err != nil {
251+
log.Fatal(err)
252+
}
253+
fmt.Println(result)
254+
}
255+
256+
if err := cursor.Err(); err != nil {
257+
log.Fatal(err)
258+
}
259+
260+
// Close the connection once no longer needed
261+
err = client.Disconnect(context.TODO())
262+
if err != nil {
263+
log.Fatal(err)
264+
}
265+
266+
fmt.Println("Connection to MongoDB closed.")
225267
}
226268
```
227269

228-
### Mongoose
270+
### With Mongoose
229271

230272
The following code shows you how to use the `Mongoose` schema to connect using TLS.
231273

232274
```js
233275
const mongoose = require('mongoose');
234276
const path = require('path');
235277

236-
// Replace with your MongoDB® connection details
278+
// Replace with your MongoDB connection details
237279
const username = encodeURIComponent('your_username');
238280
const password = encodeURIComponent('your_password');
239-
const region = "your_region" // "fr-par" for Paris.
281+
const region = "your_region"; // "fr-par" for Paris.
240282
const instanceId = 'your_instance_id'; // your instance id
283+
const privateNetworkId = 'your_private_network_id'; // your private network id
241284
const databaseName = 'databaseName'
242285

243286
// Path to your TLS certificate file
244287
const tlsCertificatePath = path.resolve(__dirname, 'path/to/your_tls_certificate.pem');
245288

246-
// Construct the connection string
289+
// Construct the public connection string
247290
const connectionString = `mongodb+srv://${username}:${password}@${instanceId}.mgdb.${region}.scw.cloud`;
291+
// Construct the private connection string
292+
const connectionString = `mongodb+srv://${username}:${password}@${instanceId}.${privateNetworkId}.internal`;
248293

249-
// Connect to MongoDB® using Mongoose
294+
// Connect to MongoDB using Mongoose
250295
mongoose.connect(connectionString, {
251296
useNewUrlParser: true,
252297
useUnifiedTopology: true,
253298
tls: true, // Enable TLS/SSL
254299
tlsCAFile: tlsCertificatePath, // Path to the CA certificate file
255300
})
256301
.then(() => {
257-
console.log('Connected to MongoDB® with Mongoose!');
302+
console.log('Connected to MongoDB with Mongoose!');
258303
})
259304
.catch(err => {
260305
console.error('Connection error', err);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
meta:
3+
title: How to connect an existing MongoDB® Database Instance to a Private Network
4+
description: Instructions for connecting your MongoDB® Database Instance over a Private Network.
5+
content:
6+
h1: How to connect an existing MongoDB® Database Instance to a Private Network
7+
paragraph: Instructions for connecting your MongoDB® Database Instance over a Private Network.
8+
tags: managed-mongodb mongodb private-network database-instance
9+
dates:
10+
validation: 2025-02-17
11+
posted: 2025-02-17
12+
categories:
13+
- managed-databases
14+
- mongodb
15+
---
16+
17+
[Private Networks](/vpc/concepts/#private-networks) allows you to enhance the security of your system's architecture by isolating it from the internet.
18+
19+
Using Private Networks improves performance by reducing the latency between your application and your database nodes. It also increases the security of your databases, as Instances in your Private Network can directly communicate with your Database Instance, without passing through the public internet.
20+
21+
You can [create new Database Instances](/managed-mongodb-databases/how-to/create-a-database-instance) to attach to your Private Network, or attach existing ones.
22+
23+
<Macro id="requirements" />
24+
25+
- A Scaleway account logged into the [console](https://console.scaleway.com)
26+
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
27+
- A valid [API key](/iam/how-to/create-api-keys/)
28+
- A [Managed MongoDB® Database Instance](/managed-mongodb-databases/how-to/create-a-database-instance)
29+
30+
## How to attach a Database Instance to a Private Network
31+
32+
<Message type="note">
33+
You can only attach your Database Instance to one Private Network at a time.
34+
</Message>
35+
36+
1. Click **MongoDB®** under **Managed Databases** on the side menu. A list of your Database Instances displays.
37+
2. Click the database name or <Icon name="more" />, then click **More info** to access the Database Instance information page.
38+
3. Scroll to the **Network** section.
39+
4. Click **Attach Private Network** next to **Private endpoint**. A pop-up appears.
40+
5. Choose one or both of the options below:
41+
- Select and attach an existing Private Network and select the network from the drop-down list.
42+
<Message type="important">
43+
The Database Instance must be attached to a Private Network in the same region.
44+
</Message>
45+
- Create and attach a new Private Network and enter the name of the network in the form.
46+
<Message type="important">
47+
If you select this option, a Private Network with default settings will be created in the same region as your Database Instance. Do not use a TLD (e.g. `dev` or `cloud`) as a name, to avoid conflicts.
48+
</Message>
49+
6. Click **Attach to Private Network** to conclude.
50+
51+
<Message type="note">
52+
Managed MongoDB® is currently not compatible with the [Static NAT](/public-gateways/concepts/#nat) feature of [Scaleway Public Gateways](/public-gateways/concepts/#public-gateway). You can use Public Gateways in [SSH bastion](/public-gateways/how-to/use-ssh-bastion/) mode and perform port forwarding.
53+
</Message>
54+
55+
<Message type="tip">
56+
Once you have attached your Database Instance to a Private Network, you can [connect to the Database Instance](/managed-mongodb-databases/how-to/connect-database-instance) using the private endpoint connection string.
57+
</Message>
58+
59+
## How to detach a Database Instance from a Private Network
60+
61+
1. Click **MongoDB®** under **Managed Databases** on the side menu. A list of your Database Instances displays.
62+
2. Click the database name or <Icon name="more" />, then click **More info** to access the Database Instance information page.
63+
3. Scroll to the **Network** section.
64+
4. Click **Detach Private Network**. A pop-up appears.
65+
5. Click **Detach resource** to confirm.
66+
67+
<Message type="note">
68+
This action takes a few moments to complete. During this time, your Database Instance:
69+
- remains available,
70+
- goes into **Configuring** mode, and
71+
- network configuration actions become unavailable
72+
</Message>

0 commit comments

Comments
 (0)