2. Connect to Percona Server for MongoDB¶
In this tutorial, you will connect to the Percona Server for MongoDB cluster you deployed previously.
To connect to Percona Server for MongoDB you need to construct the MongoDB connection URI string. It includes the credentials of the admin user, which are stored in the Secrets object.
Here’s how to do it:
-
List the Secrets objects
$ kubectl get secrets -n <namespace>
The Secrets object we target is named as
<cluster_name>-secrets
. The<cluster_name>
value is the name of your Percona Distribution for MongoDB. The default variant is:my-cluster-name-secrets
cluster1-psmdb-db-secrets
-
Retrieve the admin user credentials. Replace the
secret-name
andnamespace
with your values in the following commands:- Retrieve the login
$ kubectl get secret <secret-name> -n <namespace> -o yaml -o jsonpath='{.data.MONGODB_DATABASE_ADMIN_USER}' | base64 --decode | tr '\n' ' ' && echo " "
The default value is
databaseAdmin
- Retrieve the password
$ kubectl get secret <secret-name> -n <namespace> -o yaml -o jsonpath='{.data.MONGODB_DATABASE_ADMIN_PASSWORD}' | base64 --decode | tr '\n' ' ' && echo " "
-
Run a container with a MongoDB client and connect its console output to your terminal. The following command does this, naming the new Pod
percona-client
:$ kubectl -n <namespace> run -i --rm --tty percona-client --image=percona/percona-server-mongodb:6.0.21-18 --restart=Never -- bash -il
-
Connect to Percona Server for MongoDB. The format of the MongoDB connection URI string is the following:
mongosh "mongodb://databaseAdmin:<databaseAdminPassword>@<cluster-name>-mongos.<namespace>.svc.cluster.local/admin?ssl=false"
mongosh "mongodb://databaseAdmin:<databaseAdminPassword>@<cluster-name>-rs0.<namespace>.svc.cluster.local/admin?replicaSet=rs0&ssl=false"
If you run MongoDB 5.0 and earlier, use the old
mongo
client instead ofmongosh
.Example
The following example connects to the
admin
database of Percona Server for MongoDB 6.0 sharded cluster with the namemy-cluster-name
. The cluster runs in the namespacemongodb-operator
:mongosh "mongodb://databaseAdmin:databaseAdminPassword@my-cluster-name-mongos.mongodb-operator.svc.cluster.local/admin?ssl=false"
Congratulations! You have connected to Percona Server for MongoDB.