Create a database user
By default, after bootstrapping Cennso Database instances, only system user accounts are created. These accounts should be used only for tasks such as administration, monitoring, and database backups. To create additional database user accounts for your applications, you can either use the DBUser Custom Resource or create them manually. Both methods are described below.
Provision a user account using DBUser CR
The creation of a user account consists of two steps:
Create a Kubernetes SecretOpens in a new tab on the Core Cluster that holds the password for the new user account. For example:
yamlThe Secret must be created in the same Kubernetes Namespace where the database instance is deployed. You can create the Secret using the following command:
bashCreate the user account with the
DBUserCustom Resource on the Management Cluster, referencing the Secret you just created. For example:yamlYou can apply the
DBUserCustom Resource to your Management Cluster using the following command:bash
After you apply the DBUser manifest, a Kubernetes Job is created on the Core Cluster specified by {CORE-CLUSTER-NAME}. This Job creates a new user account with {USER-NAME} name in the {NAMESPACE-WITH-DATABASE-CLUSTER} namespace, using the password from the my-user-password Kubernetes Secret and privileges from the manifest.
To create a user account at the given database instance, the Job must be able to find the database cluster. The spec.dbInstanceName field is used for this purpose. Set it to the metadata.name of the DBInstance claim related to the target database cluster.
Besides the username and password, you can specify the host part of the MySQL account identity using the spec.host parameter. For example, % allows the user to connect from any host. For more information about the format of the host, see the official documentationOpens in a new tab.
You can also pick the MySQL authentication plugin via spec.authPlugin. Supported values are mysql_native_password (default) and caching_sha2_password.
According to the manifest above, the new user account is created with ALL PRIVILEGES on the mysql and example databases. In addition, it has the following grants on all other databases:
REPLICATION CLIENTREPLICATION SLAVEREPLICATION_SLAVE_ADMINGROUP_REPLICATION_STREAMPROCESSRELOADSYSTEM_VARIABLES_ADMINBINLOG_ADMINUSAGE
You can control the behaviour and lifetime of the Kubernetes Job generated by this CR using the standard configuration options of the Kubernetes JobOpens in a new tab:
- spec.backoffLimit
- spec.ttlSecondsAfterFinished
- spec.activeDeadlineSeconds
Provision a user manually
To provision a new user account manually, follow these steps:
Get the credentials for the
rootuser by running the following command:bash{NAMESPACE}is the Kubernetes Namespace where the Cennso Database is deployed, and{DBINSTANCE-NAME}is the metadata.name of theDBInstanceclaim.Connect to the MySQL primary instance using the
rootuser account credentials:bashEnter the password when prompted.
{MYSQL-PRIMARY-INSTANCE}is the address of the primary MySQL instance, for example theps-db-{DBINSTANCE-NAME}-haproxy.{NAMESPACE}.svcKubernetes Service, which routes the port3306to the primary MySQL instance.Create a new database userOpens in a new tab by running the following SQL command:
sqlReplace
{USERNAME}with the desired username. Replace{PASSWORD}with the desired password.Grant the required privilegesOpens in a new tab to the user based on your application needs. For a list of available privileges, see MySQL privileges documentationOpens in a new tab.
Example command to grant all privileges on a specific database:
sqlReplace
{DATABASE}with your database name and{USERNAME}with the username created in step 3.Apply the permission changes:
sql
For example, to create a user with all privileges on the example database, you can use the following SQL commands: