Skip to main content
Version: 2.0.2

MySQL API Reference

This reference details the use of MySQL on Hikube, highlighting its deployment in a replicated cluster with a primary and replicas for high availability, as well as the ability to enable automatic backups to S3-compatible storage.


Base Structure​

MySQL Resource​

YAML Configuration Example​

apiVersion: apps.cozystack.io/v1alpha1
kind: MySQL
metadata:
name: example
namespace: default
spec:

Parameters​

Common Parameters​

ParameterTypeDescriptionDefaultRequired
replicasintNumber of MariaDB replicas in the cluster2Yes
resourcesobjectExplicit CPU and memory configuration for each replica. If empty, resourcesPreset is applied{}No
resources.cpuquantityCPU available for each replicanullNo
resources.memoryquantityMemory (RAM) available for each replicanullNo
resourcesPresetstringDefault resource profile (nano, micro, small, medium, large, xlarge, 2xlarge)nanoYes
sizequantityPersistent volume (PVC) size for storing data10GiYes
storageClassstringStorageClass used to store data""No
externalboolEnable external access to the cluster (LoadBalancer)falseNo

YAML Configuration Example​

mysql.yaml
apiVersion: apps.cozystack.io/v1alpha1
kind: MySQL
metadata:
name: example # Instance name
namespace: default # Target namespace
spec:
replicas: 3 # Number of replicas (1 primary + 2 replicas)

resources:
cpu: 1000m # CPU per replica
memory: 1Gi # RAM per replica

resourcesPreset: nano # Default profile if resources is empty
size: 10Gi # Persistent volume size
storageClass: "" # Storage class
external: false # External access (LoadBalancer)

Application-Specific Parameters​

ParameterTypeDescriptionDefaultRequired
usersmap[string]objectUser configuration{...}Yes
users[name].passwordstringUser password""Yes
users[name].maxUserConnectionsintMaximum number of connections for the user0No
databasesmap[string]objectDatabase configuration{...}Yes
databases[name].rolesobjectRoles associated with the databasenullNo
databases[name].roles.admin[]stringList of users with admin rights[]No
databases[name].roles.readonly[]stringList of users with read-only rights[]No

YAML Configuration Example​

mysql.yaml
apiVersion: apps.cozystack.io/v1alpha1
kind: MySQL
metadata:
name: example
namespace: default
spec:
replicas: 2
size: 10Gi
resourcesPreset: nano
# MySQL user definition
users:
appuser:
password: strongpassword # Application user password
maxUserConnections: 50 # Simultaneous connection limit
readonly:
password: readonlypass # User with restricted rights
maxUserConnections: 10

# Database definition
databases:
myapp:
roles:
admin:
- appuser # appuser = admin of "myapp" database
readonly:
- readonly # readonly = read-only access
analytics:
roles:
admin:
- appuser # appuser = admin of "analytics" database

Backup Parameters​

ParameterTypeDescriptionDefaultRequired
backupobjectBackup configuration{}No
backup.enabledboolEnable regular backupsfalseNo
backup.s3RegionstringAWS S3 region where backups are stored"us-east-1"Yes
backup.s3BucketstringS3 bucket used to store backups"s3.example.org/mysql-backups"Yes
backup.schedulestringBackup scheduling (cron)"0 2 * * *"No
backup.cleanupStrategystringRetention strategy for cleaning up old backups"--keep-last=3 --keep-daily=3 --keep-within-weekly=1m"No
backup.s3AccessKeystringS3 access key (authentication)"<your-access-key>"Yes
backup.s3SecretKeystringS3 secret key (authentication)"<your-secret-key>"Yes
backup.resticPasswordstringPassword used for Restic encryption"<password>"Yes

YAML Configuration Example​

mysql.yaml
apiVersion: apps.cozystack.io/v1alpha1
kind: MySQL
metadata:
name: example
namespace: default
spec:
replicas: 2
size: 10Gi
resourcesPreset: small

# Automatic backup configuration
backup:
enabled: true
s3Region: eu-central-1
s3Bucket: s3.hikube.cloud/mysql-backups
schedule: "0 3 * * *" # Backup every day at 3 AM
cleanupStrategy: "--keep-last=7 --keep-daily=7 --keep-weekly=4"
s3AccessKey: "HIKUBE123ACCESSKEY"
s3SecretKey: "HIKUBE456SECRETKEY"
resticPassword: "SuperStrongResticPassword!"

resources and resourcesPreset​

The resources field allows explicitly defining the CPU and memory configuration of each MySQL replica.
If this field is left empty, the value of the resourcesPreset parameter is used.

YAML Configuration Example​

mysql.yaml
resources:
cpu: 4000m
memory: 4Gi

⚠️ Attention: if resources is defined, the resourcesPreset value is ignored.

Preset nameCPUMemory
nano250m128Mi
micro500m256Mi
small1512Mi
medium11Gi
large22Gi
xlarge44Gi
2xlarge88Gi

How to?​

Switch Primary Role in a MySQL/MariaDB Cluster​

In a managed MySQL/MariaDB cluster, one node is defined as primary (handling writes) and the others as replicas (read).
It is sometimes necessary to change the primary role, for example during maintenance or to distribute load.

  1. Edit the MariaDB resource
kubectl edit mariadb  mysql-example

Modify the following section to designate a new primary:

spec:
replication:
primary:
podIndex: 1 # Indicates the pod index to promote to primary
  1. Check cluster status
➜  ~ kubectl get mariadb
NAME READY STATUS PRIMARY UPDATES AGE
mysql-example True Running mysql-example-1 ReplicasFirstPrimaryLast 84m
➜ ~

♻️ Restore a MariaDB/MySQL Backup​

Backups are managed with Restic and stored in an S3-compatible bucket.
Restoration allows recovering a database from an existing snapshot.

1. List available snapshots​

To display all stored backups:

restic -r s3:s3.example.org/mariadb-backups/database_name snapshots
2. Restore the latest snapshot​
restic -r s3:s3.example.org/mariadb-backups/database_name restore latest --target /tmp/

Known Issues​

  • Replication can't be finished with various errors
  • Replication can't be finished in case if binlog purged Until mariadbbackup is not used to bootstrap a node by mariadb-operator (this feature is not inmplemented yet), follow these manual steps to fix it: https://github.com/mariadb-operator/mariadb-operator/issues/141#issuecomment-1804760231
  • Corrupted indicies Sometimes some indecies can be corrupted on master replica, you can recover them from slave
mysqldump -h <slave> -P 3306 -u<user> -p<password> --column-statistics=0 <database> <table> ~/tmp/fix-table.sql
mysql -h <master> -P 3306 -u<user> -p<password> <database> < ~/tmp/fix-table.sql