> 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/database-creation.md).

# Create a database

> [!IMPORTANT]
> To create a database, your database user must have the [CREATE](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_create) permission. You can use one of the system users to do so or create a special user with such permissions. For more information about the database users, see the [Create a database user](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/users-creation.md) guide.

After you deploy the MySQL cluster using the `DBInstance` Custom Resource, you can create logical databases on this cluster. The Cennso Database extension lets you provision a logical database either declaratively, with the [`DBSchema` Custom Resource](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-db/xdbschema_definition.md), or manually. Both methods are described below.

## Provision a database using `DBSchema` CR

To create a database using the `DBSchema` Custom Resource, create a claim with the database name you want to create and apply it to the Management Cluster. For example:

```yaml
apiVersion: db.contrib.cennso.com/v1alpha1
kind: DBSchema
metadata:
  name: {DB-SCHEMA-CLAIM-NAME}
  namespace: cennso-infra
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: cennso-infra
  namespace: {NAMESPACE-WITH-DATABASE-CLUSTER}
  dbInstanceName: {DBINSTANCE-CLAIM-NAME}
  databaseName: {DATABASE-NAME}
  characterSet: utf8mb4
  collate: utf8mb4_unicode_ci
  imagePullSecretRef:
    name: custom-pull-secret
  image:
    registry: custom.registry.example.com/mysql-client
    tag: v8.0.1
```

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

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

After you apply the `DBSchema` manifest, a Kubernetes Job is created on the Core Cluster specified by `{CORE-CLUSTER-NAME}`. This Job creates a new logical database named `{DATABASE-NAME}` in the `{NAMESPACE-WITH-DATABASE-CLUSTER}` namespace.

To create a database 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.

> [!NOTE]
> For now, the Cennso Database extension only creates the logical database. It does not update or delete it. Deleting a `DBSchema` Custom Resource from your Management Cluster does not drop the provisioned database from MySQL.

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**

Use **spec.characterSet** to set the [character set](https://dev.mysql.com/doc/refman/8.0/en/charset.html) and **spec.collate** to set the collation for the new database. For more information about the supported character sets and collation, see the [Cennso Database Schema](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-schema.md) document.

## Provision a database manually

To create a database, execute the following steps:

1. Find the primary MySQL instance. The primary MySQL instance Pod must have the `mysql.percona.com/primary` label set to `true`. You can find the Pod with the primary MySQL instance using the following command:

   ```bash
   kubectl get pods --selector mysql.percona.com/primary=true -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

2. Get a shell to the primary MySQL instance container using the following command:

   ```bash
   kubectl exec -it {PRIMARY-MYSQL-INSTANCE-POD} -c mysql -n {NAMESPACE-WITH-DATABASE-CLUSTER} -- bash 
   ```

3. Connect to the primary MySQL instance using the following command:

   ```bash
   mysql -u {USER-NAME} -p {USER-PASSWORD}
   ```

4. Create a database using the `CREATE DATABASE` SQL statement. For example, you can use the following SQL statement to create a database with the `cennso_database` name:

   ```SQL
   CREATE DATABASE cennso_database;
   ```

For more information, see the [official MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/create-database.html).
