> 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/deployment-guides/cennso-database-validation.md).

# Validate the MySQL cluster

After you applied the Cennso Database CRs to the Management Cluster, the MySQL cluster is deployed. It consists of multiple MySQL instances. If you use node-local volumes, the instances run on the nodes specified by **spec.instances[].storage.node**. Otherwise, they run on the nodes where the storage class provides their volumes.

One of the MySQL instances is the primary instance that accepts both reads and writes. The second is a read replica only. You can differentiate them by the `mysql.percona.com/primary` label. This label has the value `true` for the MySQL instance selected as the primary instance.

This document describes the validation procedures after your MySQL cluster is up and running.

## Connect to the MySQL cluster

Applications must use HAProxy instead of connecting directly to a pod or a service. HAProxy exposes the following ports:

- port `3306` for a read-write MySQL instance
- port `3307` for a read-only MySQL instance(s)

Connecting directly to a MySQL pod or a MySQL service doesn't provide the roles distinction and doesn't disconnect the application once a read-write MySQL instance becomes a read-only one.

The Cennso Database extension exposes two kinds of HAProxy endpoints:

- [In-cluster endpoint](#in-cluster-endpoint) - targets all HAProxy pods. Use it for workloads running inside the same Core Cluster as the MySQL cluster.
- [Per-instance external endpoint](#per-instance-external-endpoint) - targets a single HAProxy pod. Use it when you want to address a specific instance or expose it through an external IP.

### In-cluster endpoint

The Cennso Database extension creates a Kubernetes Service that fronts all HAProxy pods. Applications running in the Core Cluster must use this Service as the MySQL endpoint. The endpoint is reachable via `ps-db-{DBINSTANCE-NAME}-haproxy:{PORT}` or `ps-db-{DBINSTANCE-NAME}-haproxy.{DATABASE-CLUSTER-NAMESPACE}.svc.cluster.local:{PORT}`. The `DBINSTANCE-NAME` here is the value specified in the **metadata.name** of the `DBInstance` claim. The port can be either `3306` for the primary MySQL instance or `3307` for the read replica instance.

For example, to connect from a Pod in the same Core Cluster, run:

```bash
mysql -u root -p \
  -h ps-db-{DBINSTANCE-NAME}-haproxy.{DATABASE-CLUSTER-NAMESPACE}.svc.cluster.local \
  -P 3306
```

To use an encrypted connection, add the `--ssl-mode=REQUIRED` option:

```bash
mysql -u root -p \
  -h ps-db-{DBINSTANCE-NAME}-haproxy.{DATABASE-CLUSTER-NAMESPACE}.svc.cluster.local \
  -P 3306 \
  --ssl-mode=REQUIRED
```

The `root` password is stored in the `ps-db-{DBINSTANCE-NAME}-secrets` Kubernetes Secret in the same namespace as the MySQL cluster. You can retrieve it with:

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

### Per-instance external endpoint

> [!NOTE]
> Per-instance endpoint supports only port `3306`.

In addition to the in-cluster endpoint, the Cennso Database extension creates one Kubernetes Service per HAProxy pod. Each of these Services selects a single HAProxy pod, which is useful when you want to address a specific instance or expose it through an external IP. The endpoint is reachable via `db-{DBINSTANCE-NAME}-service-{INSTANCE-NUMBER}-ext:3306`. The `DBINSTANCE-NAME` here is the value specified in the **metadata.name** of the `DBInstance` claim. The `{INSTANCE-NUMBER}` is the key from **spec.instances** in the `DBInstance` CR (for example, `0` or `1`). If you set **spec.instances[].storage.externalIP**, the corresponding Service exposes the configured external IP, which lets you reach the MySQL cluster from the outside of the Core Cluster.

## Validate replication state

Once both primary and secondary instances are ready, you can check the replication status. To do that, execute the following steps:

1. Get credentials for the `root` user to access the database instances:

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

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

2. Connect to the secondary MySQL instance with the credentials obtained in the previous step. For information on how to connect to the MySQL replica, read the [Connect to the MySQL cluster](#connect-to-the-mysql-cluster) section.

3. Execute the following SQL query to get the status of your MySQL instance:

   ```sql
   SHOW REPLICA STATUS\G
   ```

   Among various fields in the output, you should see the following:

   ```
   Replica_IO_State: Waiting for source to send event
   Replica_IO_Running: Yes
   Replica_SQL_Running: Yes
   SQL_Delay: 0
   Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
   ```

   This means that you have a fully working and ready-to-use MySQL cluster.
