Creating a MongoDB cluster
MongoDB cluster is one or more database hosts that can have replication configured between them. 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 together 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 limits of the current quota).
- 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.
- Click on Managed Service for MongoDB.
- Click Create cluster and select the necessary DBMS. Once a cluster is created, you cannot change the DBMS.
- 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 cannot change the environment after cluster creation):
production— for stable versions of your apps.
prestable— to perform testing, including that of the Managed Service for MongoDB service itself. The prestable environment is updated more often, which means that known problems are fixed sooner in it, but this may cause backward incompatible changes.
- Select the DBMS version.
- Select the host class that will define 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.
- In the Storage size section:
- Select the type of storage, either a more flexible network type (network-hdd or network-nvme) or faster local SSD storage (local-nvme). The size of the local storage can only be changed in increments of 100 GB.
- Select the size to be used for data and backups. For more information about how backups take up storage space, see Backups.
- In the Database section, specify DB attributes:
- DB name
- User name
- User password (a minimum of 8 characters).
- In the Hosts section, select parameters for database hosts created with the cluster (keep in mind that if you use SSDs when creating MongoDB clusters, you can set at least three hosts). If you open the Advanced settings section, 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 Yandex.Cloud CLI, install it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the
To create a cluster:
See the description of the CLI's create cluster command:
$ yc managed-mongodb cluster create --help
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 the VPC service.
Specify the cluster parameters in the create command (the example shows only mandatory flags):
$ yc managed-mongodb cluster create \ --name <cluster name> \ --environment=<prestable or production> \ --network-name <network name> \ --host zone-id=<availability zone>,subnet-id=<subnet ID> \ --resource-preset <host class> \ --user name=<username>,password=<user password> \ --database name=<database name>,owner=<database owner name> \ --mongod-disk-type <network-hdd | network-nvme | local-nvme> \ --mongod-disk-size <storage size in GB>
The subnet ID
subnet-idshould be specified if the selected availability zone contains two or more subnets.
Creating a single-host cluster
To create a cluster with a single host, you should pass a single parameter,
Let's say we need to create a MongoDB cluster with the following characteristics:
- In the
- In the
- With a single host of the
s1.nanoclass in the
b0rcctk2rvtr8efcch64subnet and the
- With SSD network storage of 20 GB.
- With one user (
user1) and the password
- With one
Run the command:
$ yc managed-mongodb cluster create \ --name mymg \ --environment production \ --network-name default \ --mongod-resource-preset s1.nano \ --host zone-id=ru-central1-c,subnet-id=b0rcctk2rvtr8efcch64 \ --mongod-disk-size 20 \ --mongod-disk-type network-nvme \ --user name=user1,password=user1user1 \ --database name=db1