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
- Aiven Console
- Aiven CLI
- Aiven API
- Terraform
- In your service, in the Connect section, click Users.
- Click Add service user or Create user.
- Enter a name for your service user.
- Set up all the other configuration options. If a password is required, a random password is generated automatically. You can change it later.
- 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.
Run the avn service user-create command:
avn service user-create SERVICE_NAME --username USERNAME
Replace the following:
SERVICE_NAME: the name of your Aiven for MySQL service.USERNAME: the name of the service user to create.
Use the ServiceUserCreate endpoint:
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": "USERNAME"}'
Replace the placeholders with your project name, service name, bearer token, and the username to create.
To restrict the privileges granted to the new user, add the optional mysql_grants
field. See Restrict privileges for a new user.
Use the
aiven_mysql_user resource
to create and manage service users.
To restrict the privileges granted to the new user, set the optional mysql_grants
argument. 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
- Aiven Console
- Aiven API
- Terraform
-
In your service, in the Connect section, click Users.
-
Click Add service user.
-
Enter a name for your service user.
-
For Privileges, select Custom privileges.
-
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.
-
Click Add service user.
Selecting Default privileges instead keeps the admin-level privileges.
Set mysql_grants to an array containing only the privileges to assign. Set it to an
empty array to create a user with no privileges beyond connecting to the service. Omit
the field to keep the default admin-level privileges.
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": "USERNAME", "mysql_grants": ["SELECT", "INSERT"]}'
Replace the placeholders with your project name, service name, bearer token, and the username to create.
Set mysql_grants to the privileges to assign. Omit the argument to keep the default
admin-level privileges. Changing mysql_grants replaces the user.
Loading...
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:
| Privilege | Applies to |
|---|---|
ALTER | Databases you create |
ALTER ROUTINE | Databases you create |
CREATE | The service and databases you create |
CREATE ROUTINE | Databases you create |
CREATE TEMPORARY TABLES | Databases you create |
CREATE USER | The service |
CREATE VIEW | Databases you create |
DELETE | Databases you create |
DROP | The service and databases you create |
EVENT | Databases you create |
EXECUTE | Databases you create |
INDEX | Databases you create |
INSERT | Databases you create |
LOCK TABLES | Databases you create |
PROCESS | The service |
REFERENCES | Databases you create |
RELOAD | The service |
REPLICATION_APPLIER | The service |
REPLICATION CLIENT | The service |
REPLICATION SLAVE | The service |
ROLE_ADMIN | The service |
SELECT | Databases you create, and read-only access to system databases |
SHOW DATABASES | The service |
SHOW VIEW | Databases you create |
TRIGGER | Databases you create |
UPDATE | Databases 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:
- Create a service user with the privileges you want. See Restrict privileges for a new user.
- Update your application's connection details to use the new username and password. See Connect to your service.
- Verify your application works with the new user.
- 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
passwordorpassword_wo. Without a fixed password, update your application with the new password after the change.
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