For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Connect to a MongoDB Resource from Inside Kubernetes

The following procedure describes how to connect to a MongoDB resource deployed by Kubernetes from inside the Kubernetes cluster.

You must be able to connect to the host and port where you deployed your Kubernetes resource. To learn more about connecting to your deployment, see Connect to a MongoDB Process.

Retrieve and run the connection command for your deployment. You can retrieve the connection command from the Ops Manager or Cloud Manager application, depending on where your clusters are hosted. You can also retrieve the connection command from the Kubernetes secret that the Kubernetes Operator creates automatically when you add a MongoDB user with SCRAM authentication or X509.

The procedure for connecting to a MongoDB Database resource varies based on how you want to retrieve your connection string:

Perform the following steps in the Ops Manager or Cloud Manager application, depending on where your clusters are hosted:

1

Click Deployment in the left navigation.

2
3
4
5

Example

To connect to a sharded cluster resource named shardedcluster, you might use the following connection string:

mongosh --host shardedcluster-mongos-0.shardedcluster-svc.mongodb.svc.cluster.local \
--port 27017

When connecting to a resource from inside of Kubernetes, the hostname to which you connect has the following form:

<k8s-pod-name>.<k8s-internal-service-name>.<k8s-namespace>.<cluster-name>
Rate this page