Skip to main content
Version: release-0.9

Create and connect to a MySQL cluster

This tutorial shows how to create and connect to a MySQL cluster.

Create a MySQL cluster

Before you start

  • Install kbcli.

  • Install KubeBlocks by kbcli or by Helm.

  • Make sure the MySQL Addon is enabled. The MySQL Addon is installed and enabled by KubeBlocks by default. If you disable it when installing KubeBlocks, enable it first.

    kbcli addon list
    >
    NAME VERSION PROVIDER STATUS AUTO-INSTALL
    ...
    mysql 0.9.1 community Enabled true
    ...
  • View all the database types and versions available for creating a cluster.

    kbcli clusterdefinition list
    kbcli clusterversion list
  • To keep things isolated, create a separate namespace called demo throughout this tutorial.

    kubectl create namespace demo

Create a cluster

KubeBlocks supports creating two types of MySQL clusters: Standalone and Replication Cluster. Standalone only supports one replica and can be used in scenarios with lower requirements for availability. For scenarios with high availability requirements, it is recommended to create a Replication Cluster, which creates a cluster with two replicas. To ensure high availability, all replicas are distributed on different nodes by default.

  1. Create a MySQL cluster.

    kbcli cluster create mycluster --cluster-definition mysql -n demo

    If you want to customize your cluster specifications, kbcli provides various options, such as setting cluster version, termination policy, CPU, and memory. You can view these options by adding --help or -h flag.

    kbcli cluster create mysql --help
    kbcli cluster create mysql -h

    If you only have one node for deploying a Replication Cluster, set the --topology-keys as null when creating a Cluster. But you should note that for a production environment, it is not recommended to deploy all replicas on one node, which may decrease the cluster availability.

    kbcli cluster create mycluster --cluster-definition mysql --topology-keys null -n demo
  2. Verify whether this cluster is created successfully.

    kbcli cluster list -n demo
    >
    NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
    mycluster demo mysql mysql-8.0.30 Delete Running Jul 05,2024 18:46 UTC+0800

Connect to a MySQL Cluster

kbcli cluster connect mycluster  --namespace demo

For the detailed database connection guide, refer to Connect database.