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. Creating clusters

Creating MongoDB clusters

  • Examples
    • Creating a single-host cluster

MongoDBclusters are one or more database hosts that replication can be configured between. Replication is enabled by default in any cluster consisting of more than one host (the primary host accepts write requests and asynchronously duplicates changes in the secondary hosts).

The number of hosts that can be created with a MongoDB cluster depends on the storage option selected:

  • When using network drives, you can request any number of hosts (from one to the current quota limit).
  • When using SSDs, you can create at least three replicas along with the cluster (a minimum of three replicas is required to ensure fault tolerance). If the available folder resources are still sufficient after creating a cluster, you can add extra replicas.
Management console
CLI
Terraform
  1. In the management console, select the folder where you want to create a DB cluster.
  2. Select Managed Service for MongoDB.
  3. Click Create cluster.
  4. Enter the cluster name in the Cluster name field. The cluster name must be unique within the folder.
  5. Select the environment where you want to create the cluster (you can't change the environment once the cluster is created):
    • PRODUCTION: For stable versions of your apps.
    • PRESTABLE: For testing, including the Managed Service for MongoDB service itself. The Prestable environment is first updated with new features, improvements, and bug fixes. However, not every update ensures backward compatibility.
  6. Select the DBMS version.
  7. Select the host class that defines the technical specifications of the VMs where the DB hosts will be deployed. When you change the host class for the cluster, the characteristics of all existing hosts change, too.
  8. Under Storage size:
    • Select the type of storage, either a more flexible network type (network-hdd or network-ssd) or faster local SSD storage (local-ssd). The size of the local storage can only be changed in 100 GB increments.
    • Select the size to be used for data and backups. For more information about how backups take up storage space, see Backups.
  9. Under Database, specify the DB attributes:
    • DB name.
    • Username.
    • User password. At least 8 characters.
  10. Under Hosts, select parameters for the database hosts created with the cluster (keep in mind that if you use SSDs when creating a MongoDB cluster, you can set at least three hosts). If you open Advanced settings, you can choose specific subnets for each host. By default, each host is created in a separate subnet.
  11. Click Create cluster.

If you don't have the Yandex.Cloud command line interface yet, install and initialize it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

To create a cluster:

  1. Check whether the folder has any subnets for the cluster hosts:

    $ yc vpc subnet list
    

    If there are no subnets in the folder, create the necessary subnets in VPC.

  2. View a description of the CLI's create cluster command:

    $ yc managed-mongodb cluster create --help
    
  3. Specify the cluster parameters in the create command (the example shows only mandatory flags):

    $ yc managed-mongodb cluster create \
       --cluster-name <cluster name>
       --environment=<prestable or production> \
       --network-name <network name> \
       --host zone-id=<availability zone>,subnet-id=<subnet ID> \
       --mongod-resource-preset <host class> \
       --user name=<user name>, password=<user password> \
       --database name=<database name>,owner=<database owner name> \
       --mongod-disk-type <network-hdd | network-ssd | local-ssd> \
       --mongod-disk-size <storage size in GB>
    

    The subnet ID subnet-id should be specified if the selected availability zone contains two or more subnets.

With Terraform, you can quickly create a cloud infrastructure in Yandex.Cloud. The infrastructure components are identified through configuration files that specify the required cloud resources and their parameters.

If you don't have Terraform yet, install it and configure the provider.

To create a cluster:

  1. In the configuration file, describe the parameters of resources that you want to create:

    • Database cluster: Description of the cluster and its hosts.
    • Network: Description of the cloud network where the cluster will be located. If you already have a suitable network, you don't need to describe it again.
    • Subnets: Description of the subnets to connect the cluster hosts to. If you already have suitable subnets, you don't need to describe them again.

    Example configuration file structure:

    resource "yandex_mdb_mongodb_cluster" "<cluster name>" {
      name        = "<cluster name>"
      environment = "<PRESTABLE or PRODUCTION>"
      network_id  = "<network ID>"
    
      cluster_config {
        version = "MongoDB version: 3.6, 4.0, or 4.2"
      }
    
      database {
        name = "<DB name>"
      }
    
      user {
        name     = "<username>"
        password = "<user password>"
        permission {
          database_name = "<database name>"
        }
      }
    
      resources {
        resource_preset_id = "<host class>"
        disk_type_id       = "<storage type>"    
        disk_size          = "<storage size in GB>"
      }
    
      host {
        zone_id   = "<availability zone>"
        subnet_id = "<subnet ID>"
      }
    }
    
    resource "yandex_vpc_network" "<network name>" { name = "<network name>" }
    
    resource "yandex_vpc_subnet" "<subnet name>" {
      name           = "<subnet name>" 
      zone           = "<availability zone>"
      network_id     = "<network ID>"
      v4_cidr_blocks = ["<range>"]
    }
    

    For more information about resources that you can create using Terraform, see the provider's documentation.

  2. Make sure that the configuration files are correct.

    1. In the command line, go to the folder where you created the configuration file.

    2. Run the check using the command:

      terraform plan
      

    If the configuration is described correctly, the terminal displays a list of created resources and their parameters. If there are errors in the configuration, Terraform points them out. This is a test step. No resources are created.

  3. Create a cluster.

    1. If the configuration doesn't contain any errors, run the command:

      terraform apply
      
    2. Confirm that you want to create the resources.

    After this, all the necessary resources will be created in the specified folder and the IP addresses of the VMs will be displayed in the terminal. You can check resource availability and their settings in management console.

Examples

Creating a single-host cluster

CLI
Terraform

To create a cluster with a single host, you should pass a single parameter, --host.

Let's say we need to create a MongoDB cluster with the following characteristics:

  • Named mymg.
  • In the production environment.
  • In the default network.
  • With one s2.micro class host in the b0rcctk2rvtr8efcch64 subnet in the ru-central1-c availability zone.
  • With 20 GB fast network storage (network-ssd).
  • With one user, user1, with the password user1user1.
  • With one database, db1.

Run the command:

$ yc managed-mongodb cluster create \
     --cluster-name mymg \
     --environment production \
     --network-name default \
     --mongod-resource-preset s2.micro \
     --host zone-id=ru-central1-c,subnet-id=b0rcctk2rvtr8efcch64 \
     --mongod-disk-size 20 \
     --mongod-disk-type network-ssd \
     --user name=user1,password=user1user1 \
     --database name=db1

Let's say we need to create a MongoDB cluster and a network for it with the following characteristics:

  • Named mymg.
  • Version 4.2.
  • In the PRODUCTION environment.
  • In the cloud with ID b1gq90dgh25bebiu75o.
  • In a folder named myfolder.
  • In a new network named mynet.
  • With 1 s2.micro class host in the new mysubnet subnet and ru-central1-c availability zone. The mysubnet subnet will have the range 10.5.0.0/24.
  • With 20 GB fast network storage (network-ssd).
  • With one user, user1, with the password user1user1.
  • With one database, db1.

The configuration file for the cluster looks like this:

provider "yandex" {
  token     = "<OAuth or static key of service account>"
  cloud_id  = "b1gq90dgh25bebiu75o"
  folder_id = "${data.yandex_resourcemanager_folder.myfolder.id}"
  zone      = "ru-central1-c"
}

resource "yandex_mdb_mongodb_cluster" "mymg" {
  name        = "mymg"
  environment = "PRODUCTION"
  network_id  = "${yandex_vpc_network.mynet.id}"

  cluster_config {
    version = "4.2"
  }

  database {
    name = "db1"
  }

  user {
    name     = "user1"
    password = "user1user1"
    permission {
      database_name = "db1"
    }
  }

  resources {
    resource_preset_id = "s2.micro"
    disk_type_id       = "network-ssd"    
    disk_size          = 20
  }

  host {
    zone_id   = "ru-central1-c"
    subnet_id = "${yandex_vpc_subnet.mysubnet.id}"
  }
}

resource "yandex_vpc_network" "mynet" { name = "mynet" }

resource "yandex_vpc_subnet" "mysubnet" {
  name           = "mysubnet"
  zone           = "ru-central1-c"
  network_id     = "${yandex_vpc_network.mynet.id}"
  v4_cidr_blocks = ["10.5.0.0/24"]
}
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC