Connecting to a MongoDB cluster from applications
You can connect to a MongoDB cluster using command line tools, graphical IDEs, and Docker container. To learn how to connect from your application code, see Code examples.
The examples below assume that the root.crt
SSL certificate is located in this directory:
/home/<home_directory>/.mongodb/
for Ubuntu$HOME\.mongodb
for Windows
If the connection to the cluster is successful and the test query is executed, the name of the database to which the connection was made will be displayed.
Command line tools
To see code examples with the host FQDN filled in, open the cluster page in the management console
The setup method depends on whether cluster sharding is enabled:
Linux (Bash)
Before connecting, install the MongoDB Shell utility
For a non-sharded cluster:
mongosh --norc \
--tls \
--tlsCAFile /home/<home_directory>/.mongodb/root.crt \
--host '<FQDN_of_MongoDB_host_1>:27018,...,<FQDN_of_MongoDB_host_N>:27018' \
--username <DB_username> \
--password <DB_user_password> \
<DB_name>
For a sharded cluster:
mongosh --norc \
--tls \
--tlsCAFile /home/<home_directory>/.mongodb/root.crt \
--host '<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...,<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017' \
--username <DB_username> \
--password <DB_user_password> \
<DB_name>
For a non-sharded cluster:
mongosh --norc \
--ssl \
--sslCAFile /home/<home_directory>/.mongodb/root.crt \
--host '<FQDN_of_MongoDB_host_1>:27018,...,<FQDN_of_MongoDB_host_N>:27018' \
--username <DB_username> \
--password <DB_user_password> \
<DB_name>
For a sharded cluster:
mongosh --norc \
--ssl \
--sslCAFile /home/<home_directory>/.mongodb/root.crt \
--host '<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...,<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017' \
--username <DB_username> \
--password <DB_user_password> \
<DB_name>
For a non-sharded cluster:
mongosh --norc \
--host '<FQDN_of_MongoDB_host_1>:27018,...,<FQDN_of_MongoDB_host_N>:27018' \
--username <DB_username> \
--password <DB_user_password> \
<DB_name>
For a sharded cluster:
mongosh --norc \
--host '<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...,<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017' \
--username <DB_username> \
--password <DB_user_password> \
<DB_name>
To learn how to get a host FQDN, see this guide.
After connecting, run the db
command.
Windows (PowerShell)
Before connecting, install the MongoDB Shell utility
For a non-sharded cluster:
mongosh.exe --norc `
--host '<FQDN_of_MongoDB_host_1>:27018,...,<FQDN_of_MongoDB_host_N>:27018' `
--tls `
--tlsCAFile $HOME\.mongodb\root.crt `
--username <DB_username> `
--password <DB_user_password> `
<DB_name>
For a sharded cluster:
mongosh.exe --norc `
--host '<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...,<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017' `
--tls `
--tlsCAFile $HOME\.mongodb\root.crt `
--username <DB_username> `
--password <DB_user_password> `
<DB_name>
For a non-sharded cluster:
mongosh.exe --norc `
--host '<FQDN_of_MongoDB_host_1>:27018,...,<FQDN_of_MongoDB_host_N>:27018' `
--username <DB_username> `
--password <DB_user_password> `
<DB_name>
For a sharded cluster:
mongosh.exe --norc `
--host '<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...,<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017' `
--username <DB_username> `
--password <DB_user_password> `
<DB_name>
To learn how to get a host FQDN, see this guide.
After connecting, run the db
command.
Connecting from graphical IDEs
Connections were tested in the following environment:
- MacOS Big Sur 11.3
- JetBrains DataGrip:
2021.1
- DBeaver Enterprise:
21.0
You can only use graphical IDEs to connect to public cluster hosts using an SSL certificate.
To avoid connection errors, save the certificate
DataGrip
- Create a data source:
- Select File → New → Data Source → MongoDB.
- On the General tab:
-
Specify the connection parameters:
-
User, Password: DB user's name and password.
-
URL: Connection string.
For a non-sharded cluster:
mongodb://<FQDN_of_MongoDB_host_1>:27018,..,<FQDN_of_MongoDB_host_N>:27018/<DB_name>
For a sharded cluster:
mongodb://<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017/<DB_name>
To learn how to get a host FQDN, see this guide.
-
-
Click Download to download the connection driver.
-
- On the SSH/SSL tab:
- Enable the Use SSL setting.
- In the CA file field, specify the path to the file with an SSL certificate for the connection.
- Click Test Connection to test the connection. If the connection is successful, you will see the connection status and information about the DBMS and driver.
- Click OK to save the data source.
DBeaver
Connections to MongoDB clusters are only available in DBeaver business editions
To connect to a cluster:
- Create a new DB connection:
- In the Database menu, select New connection.
- Select MongoDB from the DB list.
- Click Next.
- Specify the connection parameters on the Main tab:
-
Under Address, change Type to
URL
and specify the connection string.For a non-sharded cluster:
mongodb://<FQDN_of_MongoDB_host_1>:27018,..,<FQDN_of_MongoDB_host_N>:27018/<DB_name>
For a sharded cluster:
mongodb://<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017/<DB_name>
To learn how to get a host FQDN, see this guide.
-
In the Device list, select
SCRAM-SHA-256
(type of password encryption when connecting to the DB). -
Under Authentication, specify the DB username and password.
-
- On the SSL tab:
- Enable Use SSL.
- In the Root certificate field, specify the path to the saved SSL certificate file.
- Under Settings, select Skip hostname validation.
- Click Test connection ... to test the connection. If the connection is successful, you will see the connection status and information about the DBMS and driver.
- Click Ready to save the database connection settings.
Before you connect from a Docker container
To connect to a Managed Service for MongoDB cluster from a Docker container using SSL, add the following lines to the Dockerfile:
RUN apt-get update && \
apt-get install wget --yes && \
mkdir --parents ~/.mongodb && \
wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
--output-document ~/.mongodb/root.crt && \
chmod 0644 ~/.mongodb/root.crt
To connect without SSL, no additional Dockerfile settings are required.
After running the Docker container, switch to it and installmongosh
utility. You will need it to connect to the cluster.