Creating MongoDB clusters
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.
- In the management console, select the folder where you want to create a DB cluster.
- Select Managed Service for MongoDB.
- Click Create cluster.
- Enter the cluster name in the Cluster name field. The cluster name must be unique within the folder.
- 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.
- Select the DBMS version.
- 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.
- 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.
- Under Database, specify the DB attributes:
- DB name.
- Username.
- User password. At least 8 characters.
- 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.
- 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:
-
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.
-
View a description of the CLI's create cluster command:
$ yc managed-mongodb cluster create --help
-
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:
-
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.
-
Make sure that the configuration files are correct.
-
In the command line, go to the folder where you created the configuration file.
-
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.
-
-
Create a cluster.
-
If the configuration doesn't contain any errors, run the command:
terraform apply
-
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
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 theb0rcctk2rvtr8efcch64
subnet in theru-central1-c
availability zone. - With 20 GB fast network storage (
network-ssd
). - With one user,
user1
, with the passworduser1user1
. - 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 newmysubnet
subnet andru-central1-c
availability zone. Themysubnet
subnet will have the range10.5.0.0/24
. - With 20 GB fast network storage (
network-ssd
). - With one user,
user1
, with the passworduser1user1
. - 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"]
}