Charmed MongoDB

Channel Revision Published Runs on
6/stable 164 26 Mar 2024
Ubuntu 22.04
6/candidate 164 26 Mar 2024
Ubuntu 22.04
6/beta 164 26 Mar 2024
Ubuntu 22.04
6/edge 173 27 May 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


22.04 20.04 18.04

Charmed MongoDB Tutorials > Deploy a sharded cluster > 3. Access a sharded cluster

Access a sharded cluster

In sharded MongoDB clusters, the database is accessed via mongos. In Charmed MongoDB clusters, we have an operational mongos daemon running as the admin user within the config-server.

Connecting to the database requires a Uniform Resource Identifier (URI). MongoDB expects a MongoDB-specific URI that contains information used to authenticate us to the database.

In this part of the tutorial, we will generate a URI containing database credentials that will allow us to connect to the database via the mongos router.


Disclaimer: This part of the tutorial accesses the sharded MongoDB cluster via the operator user. This is the admin user in Charmed MongoDB.

Do not directly interface with the operator user in a production environment. Always connect to MongoDB via a separate user. We will cover this later in this tutorial when we learn how to integrate with other applications.

Retrieve URI Data

To access MongoDB via mongos, we need to build a URI of the format


Let’s start by retrieving the necessary information for these environment variables.

Username | $DB_USERNAME

In this case, we are using the admin user to connect to MongoDB. Use operator as the username:

export DB_USERNAME="operator"

Password | $DB_PASSWORD

The password can be retrieved by running the get-password action on the Charmed MongoDB application:

juju run config-server/leader get-password

Running the command above will output

Running operation 5 with 1 task
  - task 6 on unit-config-server-0

Waiting for task 6...
password: 9JLjd0tuGngW5xFKWWbo0Blxyef0oGec

You can manually copy the password displayed above into the DB_PASSWORD environment variable, or use the command below to retrieve and assign it in one go:

export DB_PASSWORD=$(juju run config-server/leader get-password  | grep password|  awk '{print $2}')

Generate a MongoDB URI

Now that you have the necessary fields to connect to the URI, you can connect to MongoDB with charmed-mongodb.mongosh via the URI.

You can create the URI with the environment variables you exported earlier:

export URI=mongodb://$DB_USERNAME:$DB_PASSWORD@localhost:27018/admin

Now echo the URI and save it, as you won’t be able to rely on this environment variable when you ssh into a unit in the next step:

echo $URI

Connect via MongoDB URI

The mongo shell is already installed in Charmed MongoDB as charmed-mongodb.mongosh. To access the unit hosting Charmed MongoDB, first ssh into the config-server unit:

juju ssh config-server/0

Then use your URI to to access the mongo shell:

charmed-mongodb.mongosh <saved URI>

Below is an example output of running this command inside the leader unit:

ubuntu@juju-573669-0:~$ charmed-mongodb.mongosh mongodb://operator:e4HufmNaNbIdr0ArNgRTWSe7LXmNID8c@

Current Mongosh Log ID:	65c68b16f117c8adb6cab710
Connecting to:		mongodb://<credentials>@localhost/admin?directConnection=true&appName=mongosh+2.0.1
Using MongoDB:		6.0.6-5
Using Mongosh:		2.0.1
mongosh 2.1.4 is available for download:

For mongosh info see:

config-server [direct: primary] admin>

You can verify which shards are in the sharded cluster by running sh.status()

The first few lines will output:

{ _id: 1, clusterId: ObjectId("65c67095d6c910a966c84b68") }
    _id: 'shard0',
    host: 'shard0/',
    state: 1,
    topologyTime: Timestamp({ t: 1707504367, i: 4 })
    _id: 'shard1',
    host: 'shard1/',
    state: 1,
    topologyTime: Timestamp({ t: 1707504376, i: 2 })


Notice how shard0 and shard1 are both present under shards

When you’re ready to leave the MongoDB shell, just type exit. You will be back in the host of Charmed MongoDB (mongodb/0).

Exit this host by once again typing exit. Now you will be in your original shell where you can interact with Juju and LXD.

Next Step: 4. Add and remove shards

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