MongoDB

Channel Revision Published Runs on
6/stable 199 04 Oct 2024
Ubuntu 22.04
6/candidate 199 04 Oct 2024
Ubuntu 22.04
6/beta 199 04 Oct 2024
Ubuntu 22.04
6/edge 206 13 Dec 2024
Ubuntu 22.04
5/stable 117 20 Apr 2023
Ubuntu 22.04
5/candidate 117 20 Apr 2023
Ubuntu 22.04
5/edge 139 21 Nov 2023
Ubuntu 22.04 Ubuntu 20.04
5/edge 109 06 Mar 2023
Ubuntu 22.04 Ubuntu 20.04
3.6/stable 100 28 Apr 2023
Ubuntu 20.04 Ubuntu 18.04
3.6/candidate 100 13 Apr 2023
Ubuntu 20.04 Ubuntu 18.04
3.6/edge 100 03 Feb 2023
Ubuntu 20.04 Ubuntu 18.04
juju deploy mongodb --channel 6/stable
Show information

Platform:

Ubuntu
22.04 20.04 18.04

Charmed MongoDB Tutorials > Deploy a sharded cluster > 6. Integrate with other applications

Integrate MongoDB with other applications

While mongos exists inside our application config-server, its only purpose is to perform database admin operations. We have used this process throughout the tutorial so far to simplify our environment while learning about MongoDB operations, but doing so is not suitable for a production deployment.

In order to access the cluster safely, we will use Juju integrations to connect to Charmed Mongos - a charm that acts as a router to connect client applications to sharded clusters.

Integrations are the easiest way to create a user for MongoDB in Charmed MongoDB. An integration automatically creates a username, password, and database for the desired user/application. In this section of the tutorial, we will learn how to correctly access our MongoDB cluster by integrating with Charmed MongoDB. We will also see how user management works via integrations.

Summary


Deploy the mongos and data-integrator charms

Since Charmed Mongos is a subordinate charm, it requires a principal charm to host it. For this tutorial, we will use the data-integrator charm to host the mongos charm.

Deploy mongos and data-integrator

juju deploy data-integrator --config database-name=test-database
juju deploy mongos --channel 6/edge
Watch juju status --watch 1s until the data-integrator status is blocked and the mongos status is unknown
Model     Controller  Cloud/Region         Version  SLA          Timestamp
tutorial  overlord    localhost/localhost  3.1.7    unsupported  14:30:39+01:00

App              Version  Status   Scale  Charm            Channel  Rev  Exposed  Message
config-server             active       1  mongodb          6/beta   149  no       Primary
data-integrator           blocked      1  data-integrator  stable    19  no       Please specify either topic, index, or database name
mongos                    unknown      0  mongos           6/edge     5  no
shard0                    active       1  mongodb          6/beta   149  no       Primary
shard1                    active       1  mongodb          6/beta   149  no       Primary

Unit                Workload  Agent  Machine  Public address  Ports            Message
config-server/0*    active    idle   0        10.67.56.193    27017-27018/tcp  Primary
data-integrator/0*  blocked   idle   4        10.67.56.157                     Please specify either topic, index, or database name
shard0/0*           active    idle   1        10.67.56.197    27017/tcp        Primary
shard1/0*           active    idle   2        10.67.56.194    27017/tcp        Primary

Machine  State    Address       Inst id        Base          AZ  Message
0        started  10.67.56.193  juju-5646f0-0  ubuntu@22.04      Running
1        started  10.67.56.197  juju-5646f0-1  ubuntu@22.04      Running
2        started  10.67.56.194  juju-5646f0-2  ubuntu@22.04      Running
4        started  10.67.56.157  juju-5646f0-4  ubuntu@22.04      Running

Integration provider                   Requirer                               Interface              Type     Message
config-server:config-server            shard0:sharding                        shards                 regular
config-server:config-server            shard1:sharding                        shards                 regular
config-server:database-peers           config-server:database-peers           mongodb-peers          peer
data-integrator:data-integrator-peers  data-integrator:data-integrator-peers  data-integrator-peers  peer
mongos:router-peers                    mongos:router-peers                    mongos-peers           peer     joining
shard0:database-peers                  shard0:database-peers                  mongodb-peers          peer
shard1:database-peers                  shard1:database-peers                  mongodb-peers          peer

Integrate with MongoDB

Now we will connect our client application, data-integrator, to the cluster via our mongos router:

juju integrate mongos data-integrator
juju integrate mongos config-server

The data-integrator status will stay Please relate the data-integator with the desired product until mongos is integrated with the config-server.

This is because mongos acts as a proxy and data-integrator waits for the actual url to connect to the cluster.

Watch juju status --watch 1s --relations until the integrations are ready.
Model     Controller  Cloud/Region         Version  SLA          Timestamp
tutorial  overlord    localhost/localhost  3.1.7    unsupported  18:40:10+01:00

App              Version  Status  Scale  Charm            Channel  Rev  Exposed  Message     
config-server             active      1  mongodb          6/beta   149  no       Primary     
data-integrator           active      1  data-integrator  edge      19  no                                                                      
mongos                    active      1  mongos           6/edge     5  no                                                  
shard0                    active      1  mongodb          6/beta   149  no       Primary     
shard1                    active      1  mongodb          6/beta   149  no       Primary     
                                                                                          
Unit                Workload  Agent  Machine  Public address  Ports            Message       
config-server/0*    active    idle   0        10.67.56.193    27017-27018/tcp  Primary                
data-integrator/0*  active    idle   6        10.67.56.187                                                                                                    
  mongos/0*         active    idle            10.67.56.187    27018/tcp                                                                        
shard0/0*           active    idle   1        10.67.56.197    27017/tcp        Primary       
shard1/0*           active    idle   2        10.67.56.194    27017/tcp        Primary       
                                                                        
Machine  State    Address       Inst id        Base          AZ  Message
0        started  10.67.56.193  juju-5646f0-0  ubuntu@22.04      Running                   
1        started  10.67.56.197  juju-5646f0-1  ubuntu@22.04      Running                   
2        started  10.67.56.194  juju-5646f0-2  ubuntu@22.04      Running          
6        started  10.67.56.187  juju-5646f0-6  ubuntu@22.04      Running                                             
                                                                                                                     
Integration provider                   Requirer                               Interface              Type         Message
config-server:cluster                  mongos:cluster                         config-server          regular
config-server:config-server            shard0:sharding                        shards                 regular         
config-server:config-server            shard1:sharding                        shards                 regular         
config-server:database-peers           config-server:database-peers           mongodb-peers          peer            
data-integrator:data-integrator-peers  data-integrator:data-integrator-peers  data-integrator-peers  peer       
data-integrator:mongos                 mongos:mongos_proxy                    mongos_client          subordinate         
mongos:router-peers                    mongos:router-peers                    mongos-peers           peer
shard0:database-peers                  shard0:database-peers                  mongodb-peers          peer
shard1:database-peers                  shard1:database-peers                  mongodb-peers          peer

Retrieve the mongos URI

juju run data-integrator/leader get-credentials

This should output something like:

Running operation 19 with 1 task
  - task 20 on unit-data-integrator-2

Waiting for task 20...
mongos:
  data: '{"database": "test-database", "external-node-connectivity": "true", "requested-secrets":
    "[\"username\", \"password\", \"tls\", \"tls-ca\", \"uris\"]"}'
  database: test-database
  password: gRBRV3SAVV9hKWHxocI7KyZgkQgP184W
  uris: mongodb://relation-16:gRBRV3SAVV9hKWHxocI7KyZgkQgP184W@10.67.56.187:27018/test-database?&authSource=admin
  username: relation-16
ok: "True"

Save the value listed under uris. We will use this later to access the database through mongos.

Access the integrated database

Instead of connecting to mongos via the config-server application like we did in 3. Access a sharded cluster, we will access mongos directly via the host application. This is because each application related to the sharded mongodb cluster will have their own mongos router.

Additionally, the sharded cluster from Charmed MongoDB will automatically create a special username and password for the data-integrator charm. This is the best way to access the sharded Charmed MongoDB cluster.

Access the unit hosting mongos:

juju ssh mongos/0

Then, access the MongoDB shell with the URI you saved in the previous step. Make sure to wrap the URI in " with no trailing whitespace:

charmed-mongodb.mongosh "<URI>"

You are now in the MongoDB shell as a new user created specifically for the data-integrator charm.

When you relate two applications, Charmed MongoDB automatically sets up a user and database for you. Enter db.getName() into the MongoDB shell, this will output:

test-database

This is the name of the database we specified when we first deployed the data-integrator charm.

To create a collection in test-database and then show the collection, enter:

db.createCollection("test-collection")
show collections

Now insert a document into this database:

db.test_collection.insertOne(
{
First_Name: "Jammy",
Last_Name: "Jellyfish",
})

You can verify this document was inserted by running:

db.test_collection.find()

Leave the MongoDB shell by typing exit.

You will be back in the host of Charmed MongoDB (mongodb/0). Exit this host by typing exit again.

You should now be at the original shell where you can interact with Juju and LXD.

Remove the user

Removing the integration automatically removes the user that was created with it, and stops the mongos router.

To remove the relation, run:

juju remove-relation mongos config-server

Now try again to connect to the same URI you just used to access the integrated database:

juju ssh mongos/0
charmed-mongodb.mongosh "<URI>"

You will see an error like the one shown below, since the user no longer exists.

MongoNetworkError: connect ECONNREFUSED 10.67.56.187:27018

Now run exit once to leave the unit.

You should now be in the shell you started in where you can interact with Juju and LXD.

Recreate the user

If you wanted to recreate this user all you would need to do is relate the the two applications again:

juju integrate config-server mongos

Re-relating generates a new password for this user, and therefore a new URI you can see the new URI with:

juju run data-integrator/leader get-credentials

Next step: 7. Enable security


Help improve this document in the forum (guidelines). Last updated 2 months ago.