Managing user permissions
You can manage user permissions for individual databases by changing user privileges.
Warning
To change user permissions at the cluster or database level, use the Yandex Cloud interfaces. Changes made using SQL commands are not saved.
For more information, see User permissions.
Changing user privileges
-
Navigate to the folder dashboard
and select Managed Service for MySQL. -
Click the name of your cluster and open the
Users tab. -
Click
and select Configure. -
Optionally, add the databases required for the user:
- Click Add database.
- Select a database from the drop-down list.
- Repeat these two steps to select all required databases.
- To revoke access to a specific database, delete it from the list by clicking
to the right of the database name.
-
Set up user privileges for each of the user’s databases:
- In the Roles column, click
. - In the drop-down list, select the privilege you want to grant the user.
- Repeat these two steps to add all required privileges.
- In the Roles column, click
-
To revoke a privilege, click
to the right of its name. -
Configure the administrative privileges for the user, if required.
-
Click Save.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
Granting privileges to a user:
yc managed-mysql user grant-permission <username> \ --cluster-name <cluster_name> \ --database <DB_name> \ --permissions <privileges_separated_by_commas>You can get the cluster name with the list of clusters in the folder, the database name, with the list of databases in the cluster, and the username, with the list of users in the cluster.
-
Revoking privileges from a user:
yc managed-mysql user revoke-permission <username> \ --cluster-name <cluster_name> \ --database <DB_name> \ --permissions <privileges_separated_by_commas>To grant or revoke
ALL_PRIVILEGES, specify theALLalias as the privilege name.
-
Open the current Terraform configuration file describing your infrastructure.
For more information on how to create this file, see this guide.
-
Find the relevant
yandex_mdb_mysql_userresource and change the list of user’s privileges for the appropriate database in therolesparameter:resource "yandex_mdb_mysql_user" "<username>" { cluster_id = "<cluster_ID>" name = "<username>" permission { database_name = "<DB_name>" roles = [<list_of_privileges>] } ... }Where:
database_name: Name of the database the user will have access to.roles: List of user privileges for the database.
-
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
For more information, see this Terraform provider article.
-
Get an IAM token for API authentication and set it as an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the User.update method, e.g., via the following cURL
request:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
updateMaskparameter as a single comma-separated string.curl \ --request PATCH \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-mysql/v1/clusters/<cluster_ID>/users/<username>' \ --data '{ "updateMask": "permissions", "permissions": [ { "databaseName": "<DB_name>", "roles": [ "<privilege_1>", "<privilege_2>", ..., "<privilege_N>" ] } ] }'Where:
-
updateMask: Comma-separated list of settings you want to update.Here, we provide only one setting.
-
permissions: User permission settings:databaseName: Name of the database to which the user will have access.roles: Array of user privileges, each provided as a separate string in the array. For the list of possible values, see User privileges in a cluster.
For each database, add a separate element with permission settings to the
permissionsarray.
You can get the cluster ID with the list of clusters in the folder, and the username, with the list of users in the cluster.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and set it as an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Call the UserService/Update method, e.g., via the following gRPCurl
request:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
update_maskparameter as an array ofpaths[]strings.Format for listing settings
"update_mask": { "paths": [ "<setting_1>", "<setting_2>", ... "<setting_N>" ] }grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/mysql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "user_name": "<username>", "update_mask": { "paths": [ "permissions" ] }, "permissions": [ { "database_name": "<DB_name>", "roles": [ "<privilege_1>", "<privilege_2>", ..., "<privilege_N>" ] } ] }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.mysql.v1.UserService.UpdateWhere:
-
update_mask: List of settings you want to update as an array of strings (paths[]).Here, we provide only one setting.
-
permissions: User permission settings:database_name: Name of the database to which the user will have access.roles: Array of user privileges, each provided as a separate string in the array. For the list of possible values, see User privileges in a cluster.
For each database, add a separate element with permission settings to the
permissionsarray.
You can get the cluster ID with the list of clusters in the folder, and the username, with the list of users in the cluster.
-
-
Check the server response to make sure your request was successful.
Examples
Creating a user with read-only permissions
To create a new user named user2 with the SecretPassword password and read-only access to the db1 database in the existing cluster1:
Create a user named user2. When creating the user:
- Add
db1to the database list. - Add the
SELECTrole fordb1.
-
Create a user named
user2:yc managed-mysql user create "user2" \ --cluster-name "cluster1" \ --password "SecretPassword" -
Add the
SELECTrole fordb1:yc managed-mysql users grant-permission "user2" \ --cluster-name "cluster1" \ --database "db1" \ --permissions "SELECT"
-
Open the current Terraform configuration file describing your infrastructure.
For more information on how to create this file, see this guide.
-
Add the
yandex_mdb_mysql_userresource:resource "yandex_mdb_mysql_user" "user2" { cluster_id = yandex_mdb_mysql_cluster.cluster1.id name = "user2" password = "SecretPassword" permission { database_name = "db1" roles = ["SELECT"] ... } } -
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
For more information, see this Terraform provider article.