You are looking at the documentation of a prior release. To read the documentation of the latest release, please visit here.

New to KubeDB? Please start here.

Initialize MySQL using Script

This tutorial will show you how to use KubeDB to initialize a MySQL database with *.sql, *.sh and/or *.sql.gz script. In this tutorial we will use .sql script stored in GitHub repository kubedb/mysql-init-scripts.

Note: The yaml files that are used in this tutorial are stored in docs/guides/mysql/initialization/yamls folder in GitHub repository kubedb/docs.

Before You Begin

  • At first, you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using kind.

  • Now, install KubeDB cli on your workstation and KubeDB operator in your cluster following the steps here.

  • To keep things isolated, this tutorial uses a separate namespace called demo throughout this tutorial. This tutorial will also use a phpMyAdmin to connect and test MySQL database, once it is running. Run the following command to prepare your cluster for this tutorial:

    $ kubectl create ns demo
    namespace/demo created
    $ kubectl create -f
    deployment.extensions/myadmin created
    service/myadmin created
    $ kubectl get pods -n demo 
    NAME                       READY   STATUS    RESTARTS   AGE
    myadmin-66cc8d4c77-wkwht   1/1     Running   0          5m20s
    $ kubectl get service -n demo
    NAME      TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    myadmin   LoadBalancer   <pending>     80:31529/TCP     3m14s

    Then, open your browser and go to the following URL: http://{node-ip}:{myadmin-svc-nodeport}. For kind cluster, you can get this URL by running the following command:

    $ kubectl get svc -n demo myadmin -o json | jq '.spec.ports[].nodePort'
    $ kubectl get node -o json | jq '.items[].status.addresses[].address'
    # expected url will be:

Prepare Initialization Scripts

MySQL supports initialization with .sh, .sql and .sql.gz files. In this tutorial, we will use init.sql script from mysql-init-scripts git repository to create a TABLE kubedb_table in mysql database.

We will use a ConfigMap as script source. You can use any Kubernetes supported volume as script source.

At first, we will create a ConfigMap from init.sql file. Then, we will provide this ConfigMap as script source in init.script of MySQL crd spec.

Let’s create a ConfigMap with initialization script,

$ kubectl create configmap -n demo my-init-script \
--from-literal=init.sql="$(curl -fsSL"
configmap/my-init-script created

Create a MySQL database with Init-Script

Below is the MySQL object created in this tutorial.

kind: MySQL
  name: mysql-init-script
  namespace: demo
  version: "8.0.27"
    storageClassName: "standard"
    - ReadWriteOnce
        storage: 1Gi
        name: my-init-script
$ kubectl create -f created


  • spec.init.script specifies a script source used to initialize the database before database server starts. The scripts will be executed alphabatically. In this tutorial, a sample .sql script from the git repository is used to create a test database. You can use other volume sources instead of ConfigMap. The *.sql, *sql.gz and/or *.sh sripts that are stored inside the root folder will be executed alphabatically. The scripts inside child folders will be skipped.

KubeDB operator watches for MySQL objects using Kubernetes api. When a MySQL object is created, KubeDB operator will create a new StatefulSet and a Service with the matching MySQL object name. KubeDB operator will also create a governing service for StatefulSets with the name kubedb, if one is not already present. No MySQL specific RBAC roles are required for RBAC enabled clusters.

$ kubectl dba describe my -n demo mysql-init-scrip
Name:               mysql-init-script
Namespace:          demo
CreationTimestamp:  Thu, 27 Aug 2020 12:42:04 +0600
Labels:             <none>
Replicas:           1  total
Status:             Running
StorageType:        Durable
  StorageClass:      standard
  Capacity:          1Gi
  Access Modes:      RWO
Halted:              false
Halted:              false
Termination Policy:  Delete

  Name:               mysql-init-script
  CreationTimestamp:  Thu, 27 Aug 2020 12:42:04 +0600
  Annotations:        <none>
  Replicas:           824637371096 desired | 1 total
  Pods Status:        1 Running / 0 Waiting / 0 Succeeded / 0 Failed

  Name:         mysql-init-script
  Annotations:  <none>
  Type:         ClusterIP
  Port:         db  3306/TCP
  TargetPort:   db/TCP

  Name:         mysql-init-script-gvr
  Type:         ClusterIP
  IP:           None
  Port:         db  3306/TCP
  TargetPort:   3306/TCP

Database Secret:
  Name:         mysql-init-script-auth
  Annotations:  <none>
  Type:         Opaque
    password:  16 bytes
    username:  4 bytes

  Script Source:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      my-init-script
    Optional:  false

    Annotations:  {"apiVersion":"","kind":"MySQL","metadata":{"annotations":{},"name":"mysql-init-script","namespace":"demo"},"spec":{"init":{"script":{"configMap":{"name":"my-init-script"}}},"storage":{"accessModes":["ReadWriteOnce"],"resources":{"requests":{"storage":"1Gi"}},"storageClassName":"standard"},"version":"8.0.27"}}

    Creation Timestamp:  2020-08-27T06:43:15Z
    Labels:   database    mysql-init-script        mysql     8.0.27               mysql-init-script
    Name:                            mysql-init-script
    Namespace:                       demo
    Client Config:
        Name:    mysql-init-script
        Path:    /
        Port:    3306
        Scheme:  mysql
      URL:       tcp(mysql-init-script:3306)/
      Name:   mysql-init-script-auth
    Version:  8.0.27

  Type    Reason      Age   From            Message
  ----    ------      ----  ----            -------
  Normal  Successful  1m    MySQL operator  Successfully created Service
  Normal  Successful  7s    MySQL operator  Successfully created StatefulSet
  Normal  Successful  7s    MySQL operator  Successfully created MySQL
  Normal  Successful  7s    MySQL operator  Successfully created appbinding

$ kubectl get statefulset -n demo
NAME                READY   AGE
mysql-init-script   1/1     2m24s

$ kubectl get pvc -n demo
NAME                       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-mysql-init-script-0   Bound    pvc-32a59975-2972-4122-9635-22fe19483145   1Gi        RWO            standard       3m

$ kubectl get pv -n demo
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                           STORAGECLASS   REASON   AGE
pvc-32a59975-2972-4122-9635-22fe19483145   1Gi        RWO            Delete           Bound    demo/data-mysql-init-script-0   standard                3m25s

$ kubectl get service -n demo
NAME                    TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
myadmin                 LoadBalancer   <pending>     80:31529/TCP   23m
mysql-init-script       ClusterIP   <none>        3306/TCP       3m49s
mysql-init-script-gvr   ClusterIP      None             <none>        3306/TCP       3m49s

KubeDB operator sets the status.phase to Running once the database is successfully created. Run the following command to see the modified MySQL object:

$ kubectl get my -n demo mysql-init-script -o yaml
kind: MySQL
  annotations: |
  creationTimestamp: "2020-08-27T06:42:04Z"
  generation: 2
    operation: Update
    time: "2020-08-27T06:43:15Z"
  name: mysql-init-script
  namespace: demo
  resourceVersion: "11901"
  selfLink: /apis/
  uid: 2903f636-09cb-4299-af5d-c7a2e799ec61
    name: mysql-init-script-auth
        name: my-init-script
    controller: {}
    metadata: {}
      resources: {}
      serviceAccountName: mysql-init-script
  replicas: 1
    - ReadWriteOnce
        storage: 1Gi
    storageClassName: standard
  storageType: Durable
  terminationPolicy: Delete
  version: 8.0.27
  observedGeneration: 2
  phase: Running

KubeDB operator has created a new Secret called mysql-init-script-auth (format: {mysql-object-name}-auth) for storing the password for MySQL superuser. This secret contains a username key which contains the username for MySQL superuser and a password key which contains the password for MySQL superuser. If you want to use an existing secret please specify that when creating the MySQL object using While creating this secret manually, make sure the secret contains these two keys containing data username and password and also make sure of using root as value of username.

Now, you can connect to this database from the phpMyAdmin dashboard using the database pod IP and and mysql user password.

$ kubectl get pods mysql-init-script-0 -n demo -o yaml | grep IP

$ kubectl get secrets -n demo mysql-init-script-auth -o jsonpath='{.data.\user}' | base64 -d

$ kubectl get secrets -n demo mysql-init-script-auth -o jsonpath='{.data.\password}' | base64 -d

Note: In MySQL: 8.0.14-v1 connection to phpMyAdmin may give error as it is using caching_sha2_password and sha256_password authentication plugins over mysql_native_password. If the error happens do the following for work around. But, It’s not recommended to change authentication plugins. See here for alternative solutions.

kubectl exec -it -n demo mysql-quickstart-0 -- mysql -u root --password=1Pc7bwSygrv1MX1Q -e "ALTER USER root IDENTIFIED WITH mysql_native_password BY '1Pc7bwSygrv1MX1Q';"

Now, open your browser and go to the following URL: http://{node-ip}:{myadmin-svc-nodeport}. To log into the phpMyAdmin, use host , username root and password 1Pc7bwSygrv1MX1Q.

As you can see here, the initial script has successfully created a table named kubedb_table in mysql database and inserted three rows of data into that table successfully.

Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run:

kubectl patch -n demo mysql/mysql-init-script -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
kubectl delete -n demo mysql/mysql-init-script

kubectl delete ns demo

Next Steps