How to Manage MySQL Users and Databases in a Database Cluster
Validated on 1 Mar 2021 • Last edited on 23 May 2024
MySQL is an open source, object-relational database built with speed and reliability in mind. Its large and active developer community has created many third-party applications, tools, and libraries that expand MySQL’s functionality.
MySQL database clusters come configured with a default database (defaultdb) and a default administrative user (doadmin). These defaults are necessary for cluster replication and administration, so you can’t delete them, but you can add additional users and databases.
If you connect to the database cluster with preconfigured connection details from the cluster’s Overview page, you can edit them to use a different user and password or a different database.
Add or Delete a Database User Using the CLI
How to create a database user using the DigitalOcean CLI
To create a database user via the command-line, follow these steps:
Use the token to grant doctl access to your
DigitalOcean account.
doctl auth init
Finally, create a database user with
doctl databases user create. The basic usage looks
like this, but you'll want to read the usage docs for more details:
doctl databases user create <database-cluster-id> <user-name> [flags]
The following example creates a new user with the username example-user for a database cluster with the ID ca9f591d-f38h-5555-a0ef-1c02d1d1e35
doctl databases user create ca9f591d-f38h-5555-a0ef-1c02d1d1e35 example-user
How to delete a database user using the DigitalOcean CLI
To delete a database user via the command-line, follow these steps:
Use the token to grant doctl access to your
DigitalOcean account.
doctl auth init
Finally, delete a database user with
doctl databases user delete. The basic usage looks
like this, but you'll want to read the usage docs for more details:
doctl databases user delete <database-cluster-id> <user-id> [flags]
The following example deletes the user with the username example-user for a database cluster with the ID ca9f591d-f38h-5555-a0ef-1c02d1d1e35
doctl databases user delete ca9f591d-f38h-5555-a0ef-1c02d1d1e35 example-user
Add or Delete a Database User Using the API
How to create a database user using the DigitalOcean API
To create a database user using the DigitalOcean API, follow these steps:
import os
from pydo import Client
client = Client(token=os.environ.get("DIGITALOCEAN_TOKEN"))
delete_resp = client.databases.delete_user(database_cluster_uuid="aba134a", username="backend_user1")
Add or Delete a Database User Using the Control Panel
To add or delete users or databases to a database cluster, click the name of the database to go to its Overview page, then select the Users & Databases tab.
Create a new database in the Databases section by entering a name in the Add new database field and clicking Save. You can also delete a database here by opening its More menu, clicking Delete, and then confirming the deletion.
Create new users in the Users section by entering a name in the Add new user field, selecting a Password encryption option, and clicking Save. New users will receive the permissions of the doadmin user by default; these can be changed by following the instructions at How to Modify User Privileges in MySQL Databases.
You can also delete a user here by opening the user’s More menu, clicking Delete, and then confirming the deletion. Similarly, to reset a user’s password, open the user’s More menu and select Reset password.
Password Encryption
DigitalOcean Managed Databases using MySQL 8+ are automatically configured to use caching_sha2_password authentication by default. caching_sha2_password uses a stronger password encryption than prior versions of MySQL and some applications (such as PHP based applications using PHP 7.1 or older) have trouble connecting to MySQL 8+ databases. You can use the Password Encryption option to set a user’s password encryption to the legacy version (mysql_native_password) if your applications are experiencing authentication issues.
To change a user’s password encryption on a database using the control panel, click the name of the database to go to its Overview page, then select the Users & Databases tab. Beside the user you want to change, click the More menu and select Edit Password Encryption.
In the Password encryption menu, select the desired encryption type, then click Save. The database automatically updates with the new encryption preference.