Managing databases in Managed Service for MySQL
You can add and remove databases, view information about them, and manage some database settings using Managed Service for MySQL interfaces.
Warning
You can't manage databases using SQL commands. However, you can perform the ALTER DATABASE
operation.
Getting a list of cluster databases
- Go to the folder page
and select Managed Service for MySQL. - Click the cluster name and select the Databases tab.
If you do not 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 get a list of databases in a cluster, run the command:
yc managed-mysql database list \
--cluster-name=<cluster_name>
You can request the cluster name with a list of clusters in the folder.
To get a list of databases in a cluster, use the list REST API method for the Database resource or the DatabaseService/List gRPC API call and provide the cluster ID in the clusterId
request parameter.
You can fetch the cluster ID with a list of clusters in the folder.
Creating a database
Note
You can create a maximum of 1000 databases in each cluster.
-
Go to the folder page
and select Managed Service for MySQL. -
Click the cluster name.
-
If the new database does not have an owner among its current users, add such a user.
-
Select the Databases tab.
-
Click Add.
-
Enter the database name and click Add.
A database name may contain letters, numbers, and underscores. The name may be up to 63 characters long. Such names as
mysql
,sys
,information_schema
, andperformance_schema
are reserved for Managed Service for MySQL. You cannot create DBs with these names. -
Grant privileges for access to the database created to the appropriate cluster users.
If you do not 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 database in a cluster:
-
View a description of the CLI create database command:
yc managed-mysql database create --help
-
Run the create database command:
yc managed-mysql database create <DB_name> --cluster-name=<cluster_name>
A database name may contain letters, numbers, and underscores. The name may be up to 63 characters long. Such names as
mysql
,sys
,information_schema
, andperformance_schema
are reserved for Managed Service for MySQL. You cannot create DBs with these names.You can request the cluster name with a list of clusters in the folder.
Managed Service for MySQL runs the create database operation.
-
Open the current Terraform configuration file with an infrastructure plan.
For more information about creating this file, see Creating a MySQL cluster.
-
Add the
yandex_mdb_mysql_database
resource:resource "yandex_mdb_mysql_database" "<DB_name>" { cluster_id = "<cluster_ID>" name = "<DB_name>" }
A database name may contain letters, numbers, and underscores. The name may be up to 63 characters long. Such names as
mysql
,sys
,information_schema
, andperformance_schema
are reserved for Managed Service for MySQL. You cannot create DBs with these names. -
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
For more information, see the Terraform provider documentation
To create a database, use the create REST API method for the Database resource or the DatabaseService/Create gRPC API call and provide the following in the request:
-
ID of the cluster in which you want to create a database, in the
clusterId
parameter. To find out the cluster ID, get a list of clusters in the folder. -
Database name, in the
databaseSpec.name
parameter.A database name may contain letters, numbers, and underscores. The name may be up to 63 characters long. Such names as
mysql
,sys
,information_schema
, andperformance_schema
are reserved for Managed Service for MySQL. You cannot create DBs with these names.
Deleting a database
- Go to the folder page
and select Managed Service for MySQL. - Click the cluster name and open the Databases tab.
- Click the
icon in the same row as the DB and select Delete.
If you do not 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 delete a database, run the command:
yc managed-mysql database delete <DB_name> --cluster-name=<cluster_name>
You can request the cluster name with a list of clusters in the folder.
-
Open the current Terraform configuration file with an infrastructure plan.
For more information about creating this file, see Creating a MySQL cluster.
-
Delete the
yandex_mdb_mysql_database
resource with the name of the database to delete. -
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
For more information, see the Terraform provider documentation
To delete a database, use the delete REST API method for the Database resource or the DatabaseService/Delete gRPC API call and provide the following in the request:
- ID of the cluster where the database is located, in the
clusterId
parameter. To find out the cluster ID, get a list of clusters in the folder. - Database name, in the
databaseName
parameter. To find out the database name, retrieve a list of databases in the cluster.
Warning
Before creating a new database with the same name, wait for the delete operation to complete, otherwise the database being deleted will be restored. Operation status can be obtained with a list of cluster operations.
Setting SQL mode
You can set or change the value of the sql_mode setting that defines the SQL mode for the database. This operation restarts the cluster hosts.
- Go to the folder page
and select Managed Service for MySQL. - Select the cluster and click Edit cluster in the top panel.
- Under DBMS settings, click Settings.
- In the list of the settings, locate the sql_mode parameter.
- Configure a set of SQL modes in the drop-down list. To restore default settings, click Reset.
- Click Save in the DBMS settings dialog box.
- Click Save changes.
If you do not 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.
Set the SQL modes in the --set
parameter, e.g.:
yc managed-mysql cluster update-config \
--name <cluster_name> \
--set '"sql_mode=NO_KEY_OPTIONS,NO_TABLE_OPTIONS"'
Pay close attention to quotation marks: the entire string must constitute the parameter value, including sql_mode=
.
You can request the cluster name with a list of clusters in the folder.
-
Open the current Terraform configuration file with an infrastructure plan.
For more information about creating this file, see Creating a MySQL cluster.
-
Set the SQL modes in the
sql_mode
parameter undermysql_config
, e.g.:resource "yandex_mdb_mysql_cluster" "<cluster_name>" { ... mysql_config = { sql_mode = "<list_of_SQL_modes>" ... } }
-
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
For more information, see the Terraform provider documentation
Time limits
A Terraform provider sets the timeout for Managed Service for MySQL cluster operations:
- Creating a cluster, including by restoring one from a backup: 15 minutes.
- Editing a cluster, including the MySQL version update: 60 minutes.
- Deleting a cluster: 15 minutes.
Operations exceeding the set timeout are interrupted.
Add the timeouts
block to the cluster description, for example:
resource "yandex_mdb_mysql_cluster" "<cluster_name>" {
...
timeouts {
create = "1h30m" # 1 hour 30 minutes
update = "2h" # 2 hours
delete = "30m" # 30 minutes
}
}
To set the SQL modes you need, use the update REST API method for the Cluster resource or the ClusterService/Update gRPC API call and provide the following in the request:
-
Cluster ID in the
clusterId
parameter. To find out the cluster ID, get a list of clusters in the folder. -
An array with new MySQL settings in the following parameter:
configSpec.mysqlConfig_5_7.sqlMode
for MySQL 5.7.configSpec.mysqlConfig_8_0.sqlMode
for MySQL 8.0.
-
List of settings you want to update, in the
updateMask
parameter.
Warning
This API method overrides all parameters of the object being modified that were not explicitly passed in the request to the default values. To avoid this, list the settings you want to change in the updateMask
parameter (one line separated by commas).
Changing a character set and collation rules
To set the CHARACTER SET
and COLLATE
settings for the database:
-
Connect to the database under the account of the database owner or as a user with the
ALTER
privilege for this database. -
Run the ALTER DATABASE
query:ALTER DATABASE <DB_name> CHARACTER SET = 'utf8mb4' COLLATE = 'utf8mb4_unicode_ci';
-
To apply settings to the database tables along with the database, convert the tables with the same settings:
ALTER TABLE <DB_name>.<table_name> CONVERT TO CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';