> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/users-creation.md).

# 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](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-db/xdbuser_definition.md) 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:

1. Create a Kubernetes [Secret](https://kubernetes.io/docs/concepts/configuration/secret/) on the Core Cluster that holds the password for the new user account. For example:

   ```yaml
   apiVersion: v1
   kind: Secret
   metadata:
     name: my-user-password
   data:
     password: ZXhhbXBsZS1wYXNzd29yZA==
   type: Opaque
   ```

   The Secret must be created in the same Kubernetes Namespace where the database instance is deployed. You can create the Secret using the following command:

   ```bash
   kubectl apply -f user-secret.yaml -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

2. Create the user account with the `DBUser` Custom Resource on the Management Cluster, referencing the Secret you just created. For example:

   ```yaml
   apiVersion: db.contrib.cennso.com/v1alpha1
   kind: DBUser
   metadata:
     name: {DB-USER-CLAIM-NAME}
     namespace: cennso-infra
   spec:
     connectionSecretRef:
       name: {CORE-CLUSTER-NAME}
       namespace: cennso-infra
     namespace: {NAMESPACE-WITH-DATABASE-CLUSTER}
     dbInstanceName: {DBINSTANCE-CLAIM-NAME}
     username: {USER-NAME}
     host: {USER-HOST}
     authPlugin: mysql_native_password
     passwordSecretRef:
       name: my-user-password
       key: password
     grants:
       - privileges: ["ALL PRIVILEGES"]
         target: "example.*"
       - privileges: ["ALL PRIVILEGES"]
         target: "mysql.*"
       - privileges:
           - REPLICATION CLIENT
           - REPLICATION SLAVE
           - REPLICATION_SLAVE_ADMIN
           - GROUP_REPLICATION_STREAM
           - PROCESS
           - RELOAD
           - SYSTEM_VARIABLES_ADMIN
           - BINLOG_ADMIN
           - USAGE
         target: "*.*"
   ```

   You can apply the `DBUser` Custom Resource to your Management Cluster using the following command:

   ```bash
   kubectl apply -f db-user.yaml
   ```

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 documentation](https://dev.mysql.com/doc/refman/8.0/en/account-names.html).

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 CLIENT`
- `REPLICATION SLAVE`
- `REPLICATION_SLAVE_ADMIN`
- `GROUP_REPLICATION_STREAM`
- `PROCESS`
- `RELOAD`
- `SYSTEM_VARIABLES_ADMIN`
- `BINLOG_ADMIN`
- `USAGE`

You can control the behaviour and lifetime of the Kubernetes Job generated by this CR using the standard configuration options of the [Kubernetes Job](https://kubernetes.io/docs/concepts/workloads/controllers/job/):

- **spec.backoffLimit**
- **spec.ttlSecondsAfterFinished**
- **spec.activeDeadlineSeconds**

## Provision a user manually

To provision a new user account manually, follow these steps:

1. Get the credentials for the `root` user by running the following command:

   ```bash
   kubectl get secret ps-db-{DBINSTANCE-NAME}-secrets -n {NAMESPACE} --template='{{.data.root | base64decode}}{{"\n"}}'
   ```

   `{NAMESPACE}` is the Kubernetes Namespace where the Cennso Database is deployed, and `{DBINSTANCE-NAME}` is the **metadata.name** of the `DBInstance` claim.

2. Connect to the MySQL primary instance using the `root` user account credentials:

   ```bash
   mysql -u root -p -h {MYSQL-PRIMARY-INSTANCE}
   ```

   Enter the password when prompted. `{MYSQL-PRIMARY-INSTANCE}` is the address of the primary MySQL instance, for example the `ps-db-{DBINSTANCE-NAME}-haproxy.{NAMESPACE}.svc` Kubernetes Service, which routes the port `3306` to the primary MySQL instance.

3. [Create a new database user](https://dev.mysql.com/doc/refman/8.0/en/create-user.html) by running the following SQL command:

   ```sql
   CREATE USER IF NOT EXISTS '{USERNAME}'@'%' IDENTIFIED WITH mysql_native_password BY '{PASSWORD}';
   ```

   Replace `{USERNAME}` with the desired username. Replace `{PASSWORD}` with the desired password.

4. [Grant the required privileges](https://dev.mysql.com/doc/refman/8.0/en/grant.html) to the user based on your application needs. For a list of available privileges, see [MySQL privileges documentation](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html).

   Example command to grant all privileges on a specific database:

   ```sql
   GRANT ALL PRIVILEGES ON {DATABASE}.* TO '{USERNAME}'@'%';
   ```

   Replace `{DATABASE}` with your database name and `{USERNAME}` with the username created in step 3.

5. Apply the permission changes:

   ```sql
   FLUSH PRIVILEGES;
   ```

For example, to create a user with all privileges on the `example` database, you can use the following SQL commands:

```sql
CREATE USER IF NOT EXISTS 'example'@'%' IDENTIFIED WITH mysql_native_password BY 'example';
GRANT ALL PRIVILEGES ON example.* TO 'example'@'%';
GRANT ALL PRIVILEGES ON mysql.* TO 'example'@'%';
GRANT REPLICATION CLIENT ON *.* TO 'example'@'%';
GRANT REPLICATION SLAVE ON *.* TO 'example'@'%';
GRANT REPLICATION_SLAVE_ADMIN ON *.* TO 'example'@'%';
GRANT GROUP_REPLICATION_STREAM ON *.* TO 'example'@'%';
GRANT PROCESS ON *.* TO 'example'@'%';
GRANT RELOAD ON *.* TO 'example'@'%';
GRANT SYSTEM_VARIABLES_ADMIN ON *.* TO 'example'@'%';
GRANT BINLOG_ADMIN ON *.* TO 'example'@'%';
GRANT USAGE ON *.* TO 'example'@'%';
FLUSH PRIVILEGES;
```
