Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Managed Service for MongoDB
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Information about existing clusters
    • Creating clusters
    • Database migration to Yandex.Cloud
    • Connecting to databases
    • Stop and start the cluster
    • Changing cluster and database settings
    • MongoDB version upgrade
    • Database management
    • Managing hosts in a cluster
    • Managing database users
    • Managing backups
    • How to manage shards
    • Deleting clusters
  • Solutions
    • Sharding collections
  • Concepts
    • Relationship between service resources
    • Host classes
    • Network in Yandex Managed Service for MongoDB
    • Quotas and limits
    • Storage types
    • Backups
    • Replication
    • Sharding
    • Users and roles
    • Supported clients
  • Access management
  • Pricing policy
    • Current pricing policy
    • Archive
      • Before January 1, 2019
      • From January 1 to March 1, 2019
      • From March 1, 2019 to February 1, 2020
  • API reference
    • Authentication in the API
    • gRPC
      • Обзор
      • BackupService
      • ClusterService
      • DatabaseService
      • ResourcePresetService
      • UserService
      • OperationService
    • REST
      • Overview
      • Backup
        • Overview
        • get
        • list
      • Cluster
        • Overview
        • addHosts
        • addShard
        • backup
        • create
        • delete
        • deleteHosts
        • deleteShard
        • enableSharding
        • get
        • getShard
        • list
        • listBackups
        • listHosts
        • listLogs
        • listOperations
        • listShards
        • move
        • rescheduleMaintenance
        • resetupHosts
        • restartHosts
        • restore
        • start
        • stop
        • streamLogs
        • update
      • Database
        • Overview
        • create
        • delete
        • get
        • list
      • ResourcePreset
        • Overview
        • get
        • list
      • User
        • Overview
        • create
        • delete
        • get
        • grantPermission
        • list
        • revokePermission
        • update
      • Operation
        • Overview
        • get
  • Questions and answers
    • General questions
    • Questions about MongoDB
    • All questions on the same page
  1. Step-by-step instructions
  2. Connecting to databases

Connecting to a database in a cluster MongoDB

  • Configuring an SSL certificate
  • Connection string

You can connect to Managed Service for MongoDB cluster hosts:

  • Over the internet, if you configured public access for the appropriate host. You can only connect to these hosts over an SSL connection.
  • From Yandex.Cloud VM instances hosted in the same virtual network. If the host isn't publicly available, you don't need to use an SSL connection to connect to such VMs.

To connect to Managed Service for MongoDB cluster hosts, specify port 27018.

Note

If public access is only configured for certain hosts in your cluster, automatic primary replica change may make the primary replica unavailable over the internet.

Configuring an SSL certificate

MongoDBhosts with public access only support connections with an SSL certificate. You can prepare a certificate as follows:

$ mkdir ~/.mongodb
$ wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" -O ~/.mongodb/CA.pem
$ chmod 0600 ~/.mongodb/CA.pem

Connection string

You can connect to the database using the command mongo by listing all the cluster hosts in the host parameter value.

To view an example of the command with the host FQDN filled in, open the cluster page in the management console and click Connect.

SSL for mongo 4.2
SSL for older versions of MongoDB
Without SSL

For hosts with public access, you can only connect over the internet with an SSL certificate.

$ mongo --norc \
        --tls \
        --tlsCAFile ~/.mongodb/CA.pem \
        --host 'rs01/<host 1 FQDN>:27018,<host 2 FQDN>:27018,<host N FQDN>:27018' \
        -u <user name> \
        -p <user password> \
        <DB name>

For hosts with public access, you can only connect over the internet with an SSL certificate.

$ mongo --norc \
        --ssl \
        --sslCAFile ~/.mongodb/CA.pem \
        --host 'rs01/<host 1 FQDN>:27018,<host 2 FQDN>:27018,<host N FQDN>:27018' \
        -u <user name> \
        -p <user password> \
        <DB name>

If you don't need to encrypt traffic within the virtual network when connecting to the database, you can connect from a Yandex.Cloud VM without SSL. Pass the sslmode parameter with the disable value:

$ mongo --norc \
        --host 'rs01/<host 1 FQDN>:27018,<host 2 FQDN>:27018,<host N FQDN>:27018' \
        -u <user name> \
        -p <user password> \
        <DB name>

Write requests will be automatically routed to the primary cluster replica.

In this article:
  • Configuring an SSL certificate
  • Connection string
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC