Skip to main content

Manage Aiven for MySQL® service users

Create and manage service users in your Aiven for MySQL® service to control access to its databases and tables.

Service users only exist in the scope of the Aiven service. They are unique to the service and not shared with any other services. Every service has a default avnadmin user with full access to the service.

Add a service user​

  1. In your service, in the Connect section, click Users.
  2. Click Add service user or Create user.
  3. Enter a name for your service user.
  4. Set up all the other configuration options. If a password is required, a random password is generated automatically. You can change it later.
  5. Click Add service user or Save.

A new user gets admin-level privileges unless you select a narrower set. See Restrict privileges for a new user.

Restrict privileges for a new user​

By default, a new Aiven for MySQL service user gets admin-level privileges, including the ability to create other users. To limit what the user can do, select its privileges when you create it.

The Aiven Console, Aiven API, and Terraform assign privileges when the user is created. To change the privileges of an existing user, create another user with the privileges you need. See Change privileges for an existing service user.

Requirements​

Restricting privileges at user creation requires your Aiven for MySQL service to support granular grants. If your service doesn't support this capability, creating a user with selected privileges fails, and the Aiven API returns an HTTP 400 Bad Request status code. To add support, review and apply pending maintenance updates on your service.

Granular grants depend on this service capability rather than on your service plan, so every Aiven for MySQL plan supports them once the service has the required maintenance update.

Create a user with selected privileges​

  1. In your service, in the Connect section, click Users.

  2. Click Add service user.

  3. Enter a name for your service user.

  4. For Privileges, select Custom privileges.

  5. For Privileges to grant, select the privileges to assign to the user.

    Leave the selection empty to create a user that can connect to the service but can't access any data.

  6. Click Add service user.

Selecting Default privileges instead keeps the admin-level privileges.

If the assigned privileges include CREATE USER or ROLE_ADMIN, the created user can also grant every assigned privilege to other users, equivalent to MySQL's WITH GRANT OPTION.

Available privileges​

You can assign the following privileges:

PrivilegeApplies to
ALTERDatabases you create
ALTER ROUTINEDatabases you create
CREATEThe service and databases you create
CREATE ROUTINEDatabases you create
CREATE TEMPORARY TABLESDatabases you create
CREATE USERThe service
CREATE VIEWDatabases you create
DELETEDatabases you create
DROPThe service and databases you create
EVENTDatabases you create
EXECUTEDatabases you create
INDEXDatabases you create
INSERTDatabases you create
LOCK TABLESDatabases you create
PROCESSThe service
REFERENCESDatabases you create
RELOADThe service
REPLICATION_APPLIERThe service
REPLICATION CLIENTThe service
REPLICATION SLAVEThe service
ROLE_ADMINThe service
SELECTDatabases you create, and read-only access to system databases
SHOW DATABASESThe service
SHOW VIEWDatabases you create
TRIGGERDatabases you create
UPDATEDatabases you create

For privileges that apply to databases, Aiven revokes the privilege from the service's system databases, except SELECT. This keeps read access to system information on every user without allowing changes to it.

Change privileges for an existing service user​

You set a service user's privileges when you create the user. To change what an existing service user can do, create a service user with the privileges you need, point your application at it, and delete the user you replaced:

  1. Create a service user with the privileges you want. See Restrict privileges for a new user.
  2. Update your application's connection details to use the new username and password. See Connect to your service.
  3. Verify your application works with the new user.
  4. Delete the user you replaced. Run avn service user-delete, or call the ServiceUserDelete endpoint.

In Terraform, changing mysql_grants on an aiven_mysql_user resource replaces the user, because the field forces recreation of the resource. Terraform deletes the user and creates it again with the same name:

  • Deleting the user closes its open connections, so plan the change for a window where a short interruption for that user is acceptable.
  • The new user gets a new generated password, unless the resource sets password or password_wo. Without a fixed password, update your application with the new password after the change.
note

Deleting a service user closes its open connections. Switch your application to the new user before you delete the user you replaced.

View the privileges of a service user​

To see a service user's privileges, connect to your service as that user and run:

SHOW GRANTS;

To list the service users on your service, run avn service user-list. The same users are listed in Aiven Console, where you add a service user.

To view another user's privileges, run SHOW GRANTS FOR 'USERNAME'@'%';. This requires SELECT on the service's system databases, which a service user has when its assigned privileges include SELECT. Connect as avnadmin, or as a user that has SELECT, to read another user's privileges.

Use SQL GRANT statements​

Aiven sets a service user's privileges when it creates the user, and leaves them unchanged after that. To refine privileges further, connect as avnadmin with a SQL client and use MySQL's GRANT and REVOKE statements.

avnadmin holds the privileges listed in Available privileges, so those are the privileges it can grant.

Privileges you set this way exist only in MySQL. They are not part of the privileges Aiven assigned when it created the user, so keep a record of them alongside the rest of your service configuration, and reapply them if you replace the user.

Example: separate application and migration users​

A common setup is one user for everyday application traffic and a second, broader user used only while a migration runs.

Create the application user with the privileges your application needs at runtime:

curl --request POST \
--url https://api.aiven.io/v1/project/PROJECT_NAME/service/SERVICE_NAME/user \
--header 'Authorization: Bearer YOUR_BEARER_TOKEN' \
--header 'content-type: application/json' \
--data '{"username": "app_user", "mysql_grants": ["SELECT", "INSERT", "UPDATE", "DELETE"]}'

Create the migration user with the privileges your migration tooling needs, such as schema changes and temporary tables:

curl --request POST \
--url https://api.aiven.io/v1/project/PROJECT_NAME/service/SERVICE_NAME/user \
--header 'Authorization: Bearer YOUR_BEARER_TOKEN' \
--header 'content-type: application/json' \
--data '{
"username": "migration_user",
"mysql_grants": [
"SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "ALTER", "INDEX",
"DROP", "CREATE TEMPORARY TABLES", "LOCK TABLES"
]
}'

In Terraform, declare the two users as separate resources:

resource "aiven_mysql_user" "app_user" {
project = "PROJECT_NAME"
service_name = "SERVICE_NAME"
username = "app_user"
mysql_grants = ["SELECT", "INSERT", "UPDATE", "DELETE"]
}

resource "aiven_mysql_user" "migration_user" {
project = "PROJECT_NAME"
service_name = "SERVICE_NAME"
username = "migration_user"
mysql_grants = [
"SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "ALTER", "INDEX", "DROP",
"CREATE TEMPORARY TABLES", "LOCK TABLES",
]
}

Delete the migration user once the migration is complete.

The Aiven CLI creates service users with the default privileges. To select privileges, use the Aiven Console, the Aiven API, or Terraform.

Related pages