Connecting to a Yandex StoreDoc cluster from applications
You can connect to a Yandex StoreDoc cluster using command line tools, graphical IDEs, or Docker containers. To learn how to connect from the code of your application, see Code examples.
In the following examples, we assume that the root.crt SSL certificate is located in one of these directories:
~/.mongodb/for Ubuntu$HOME\.mongodbfor Windows
After you successfully connect to the cluster and run a test query, you will see the name of the database you connected to.
Warning
Queries with the writeConcern: 1 setting may lead to lagging data on secondary replicas. This increases the risk of write timeouts and data loss if the master host fails. Therefore, the SLAwriteConcern impacts high availability, see here.
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 sharding is enabled in the cluster:
Linux (Bash)
Before connecting, install MongoDB Shell.
For a non-sharded cluster:
mongosh --norc \
--tls \
--tlsCAFile ~/.mongodb/root.crt \
--host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' \
--username <DB_user_name> \
--password <DB_user_password> \
<DB_name>
For a sharded cluster:
mongosh --norc \
--tls \
--tlsCAFile ~/.mongodb/root.crt \
--host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' \
--username <DB_user_name> \
--password <DB_user_password> \
<DB_name>
To learn how to get a host FQDN, see this guide.
Once connected, run the db command.
For a non-sharded cluster:
mongosh --norc \
--host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' \
--username <DB_user_name> \
--password <DB_user_password> \
<DB_name>
For a sharded cluster:
mongosh --norc \
--host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' \
--username <DB_user_name> \
--password <DB_user_password> \
<DB_name>
To learn how to get a host FQDN, see this guide.
Once connected, run the db command.
mongosh "mongodb+srv://<DB_user_name>:<DB_user_password>\
@<cluster_ID>.mdb.yandexcloud.net/<DB_name>"
You can get the cluster ID with the list of clusters in the folder.
Once connected, run the db command.
You can add connection parameters to the command, if required, for example:
tls: Enables TLS encryption for the connection (trueorfalse). Encryption is enabled by default.tlsCAFile: Path to the.pemfile with root certificates from the CA.readPreference: Hosts to read from:primary: Only the primary replica (master).primaryPreferred: Master or secondary replicas if the master is unavailable.secondary: Only secondary replicas.secondaryPreferred: Secondary replicas or the master if there are no secondary replicas.nearest: Least lagging host.
authSource: Name of the database storing the user credentials. By default, it is the same as<DB_name>.appName: App name to display in logs.
For more information about available connection parameters, see this official guide
The parameter string begins with the ? character, and & is the separator.
Example of a command with connection properties:
mongosh "mongodb+srv://user********:qwe********@c9qng7jhsgtg********.mdb.yandexcloud.net\
/db********?authSource=admin&appName=debug"
Windows (PowerShell)
Before connecting, install MongoDB Shell.
For a non-sharded cluster:
mongosh.exe --norc `
--host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' `
--tls `
--tlsCAFile $HOME\.mongodb\root.crt `
--username <DB_user_name> `
--password <DB_user_password> `
<DB_name>
For a sharded cluster:
mongosh.exe --norc `
--host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' `
--tls `
--tlsCAFile $HOME\.mongodb\root.crt `
--username <DB_user_name> `
--password <DB_user_password> `
<DB_name>
To learn how to get a host FQDN, see this guide.
Once connected, run the db command.
For a non-sharded cluster:
mongosh.exe --norc `
--host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' `
--username <DB_user_name> `
--password <DB_user_password> `
<DB_name>
For a sharded cluster:
mongosh.exe --norc `
--host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' `
--username <DB_user_name> `
--password <DB_user_password> `
<DB_name>
To learn how to get a host FQDN, see this guide.
Once connected, run the db command.
mongosh.exe "mongodb+srv://<DB_user_name>:<DB_user_password>`
@<cluster_ID>.mdb.yandexcloud.net/<DB_name>"
You can get the cluster ID with the list of clusters in the folder.
Once connected, run the db command.
You can add connection parameters to the command, if required, for example:
tls: Enables TLS encryption for the connection (trueorfalse). Encryption is enabled by default.tlsCAFile: Path to the.pemfile with root certificates from the CA.readPreference: Hosts to read from:primary: Only the primary replica (master).primaryPreferred: Master or secondary replicas if the master is unavailable.secondary: Only secondary replicas.secondaryPreferred: Secondary replicas or the master if there are no secondary replicas.nearest: Least lagging host.
authSource: Name of the database storing the user credentials. By default, it is the same as<DB_name>.appName: App name to display in logs.
For more information about available connection parameters, see this official guide
The parameter string begins with the ? character, and & is the separator.
Example of a command with connection properties:
mongosh.exe "mongodb+srv://user********:qwe********@c9qng7jhsgtg********.mdb.yandexcloud.net`
/db********?authSource=admin&appName=debug"
Connecting with IAM authentication
You can connect to a Yandex StoreDoc cluster using IAM authentication. This authentication option is supported for Yandex accounts, federated accounts, and local accounts.
The connection is established via MongoDB Shell. For information on how to install MongoDB Shell, see this guide.
Before you proceed with connection, configure the security groups and enable public access to the cluster hosts if connecting over the internet.
To connect to a cluster using IAM authentication:
-
Set up authentication:
Management console-
Open the folder dashboard
. -
Navigate to Yandex StoreDoc.
-
Select the cluster.
-
Assign the Yandex Cloud account the
managed-mongodb.clusters.connectorrole for the cluster:-
Select the Access bindings tab.
-
Click Assign roles in the top-right corner of the page.
-
In the Who to grant access to field, select the account.
To find the account you need, enter its associated email address.
-
Click
Add role and select themanaged-mongodb.clusters.connectorrole. -
Click Save.
-
-
Create a user named Yandex StoreDoc:
-
Select
Users. -
Click Create user in the top-right corner of the page.
-
Choose the IAM authorization method.
-
Select the Yandex Cloud account with the
managed-mongodb.clusters.connectorrole. -
Select the databases and configure roles for them:
-
Click
Add database and select the database. -
Assign roles for the selected database.
To assign a role, click
and select the role you need.
-
-
Click Create.
-
-
-
Get an IAM token and place it in the
IAM_TOKENenvironment variable: -
Connect to the cluster by running this command:
For a non-sharded cluster:
mongosh --norc \ --tls \ --tlsCAFile ~/.mongodb/root.crt \ --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27018,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27018' \ --username "<DB_user_name>" \ --password "$IAM_TOKEN" \ --authenticationDatabase '$external' \ --authenticationMechanism PLAIN \ <DB_name>For a sharded cluster:
mongosh --norc \ --tls \ --tlsCAFile ~/.mongodb/root.crt \ --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' \ --username "<DB_user_name>" \ --password "$IAM_TOKEN" \ --authenticationDatabase '$external' \ --authenticationMechanism PLAIN \ <DB_name>Where
--usernameis the database user name, which matches the Yandex Cloud account email address.To learn how to get a host FQDN, see this guide.
Connecting from graphical IDEs
Connections were tested in the following environment:
- MacOS Big Sur 11.3
- JetBrains DataGrip:
2021.1 - DBeaver Enterprise:
21.0
From graphical IDEs, you can only 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:
-
Configure the connection as follows:
-
User, Password: Database user name and password.
-
URL: Connection string.
For a non-sharded cluster:
mongodb://<Yandex_StoreDoc_host_1_FQDN>:27018,..,<Yandex_StoreDoc_host_N_FQDN>:27018/<DB_name>For a sharded cluster:
mongodb://<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...<MONGOINFRA_or_MONGOS_host_N_FQDN>: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 Use SSL.
- In the CA file field, specify the path to the SSL certificate for your connection.
- Click Test 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 Yandex StoreDoc clusters are only available in DBeaver commercial editions
To connect to your cluster:
- Create a new database connection:
- In the Database menu, select New connection.
- Select MongoDB from the database list.
- Click Next.
- On the Main tab, specify the connection settings:
-
Under Address, change Type to
URLand specify the connection string.For a non-sharded cluster:
mongodb://<Yandex_StoreDoc_host_1_FQDN>:27018,..,<Yandex_StoreDoc_host_N_FQDN>:27018/<DB_name>For a sharded cluster:
mongodb://<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017/<DB_name>To learn how to get a host FQDN, see this guide.
-
In the Device list, select the
SCRAM-SHA-256password encryption type for the connection. -
Under Authentication, specify the database user name and password.
-
- On the SSL tab:
- Enable Use SSL.
- In the Root certificate field, specify the path to your saved SSL certificate file.
- Under Settings, check Skip hostname validation.
- Click Test connection .... If the connection is successful, you will see the connection status, DBMS information, and driver details.
- Click Ready to save the database connection settings.
Before you connect from a Docker container
To connect to a Yandex StoreDoc cluster from a Docker container over SSL, add the following lines to your 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 configuration is required.
After starting the Docker container, access it and install mongosh, which you will need to connect to the cluster.