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 that the yaml files that are used in this tutorial, stored in docs/examples folder in GitHub repository kubedb/cli.

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 Minikube.

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 -f
namespace "demo" created

$ kubectl create -f
deployment "myadmin" created
service "myadmin" created

$ kubectl get pods -n demo --watch
NAME                      READY     STATUS              RESTARTS   AGE
myadmin-c4db4df95-4tgkx   0/1       ContainerCreating   0          27s
myadmin-c4db4df95-4tgkx   1/1       Running             0          1m

$ kubectl get service -n demo
NAME                TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
myadmin             LoadBalancer   <pending>     80:32673/TCP   50s

$ minikube ip

Now, open your browser and go to the following URL: http://{minikube-ip}:{myadmin-svc-nodeport}. You can also get this URl by running the following command:

$ minikube service myadmin -n demo --url

According to the above example, this URL will be The login informations to phpMyAdmin (host, username and password) will be retrieved later in this tutorial.

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
  doNotPause: true
    storageClassName: "standard"
    - ReadWriteOnce
        storage: 50Mi
        repository: ""
        directory: .

$ kubedb create -f
validating ""
mysql "mysql-init-script" created


  • spec.version is the version of MySQL database. In this tutorial, a MySQL 8.0 database is going to be created.

  • specifies the StorageClass of PVC dynamically allocated to store data for this database. This storage spec will be passed to the StatefulSet created by KubeDB operator to run database pods. You can specify any StorageClass available in your cluster with appropriate resource requests. If no storage spec is given, an emptyDir is used.

  • spec.init.scriptSource specifies a script source used to initialize the database before database server starts. The scripts will be executed alphabatically. In this tutorial, a sample .js script from the git repository is used to create a test database. You can use other volume sources instead of gitrepo. 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 ClusterIP 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.

$ kubedb describe my -n demo mysql-init-script
Name:		mysql-init-script
Namespace:	demo
StartTimestamp:	Fri, 09 Feb 2018 17:18:14 +0600
Status:		Running
  StorageClass:	standard
  Capacity:	50Mi
  Access Modes:	RWO

  Name:			mysql-init-script
  Replicas:		1 current / 1 desired
  CreationTimestamp:	Fri, 09 Feb 2018 17:18:15 +0600
  Pods Status:		1 Running / 0 Waiting / 0 Succeeded / 0 Failed

  Name:		mysql-init-script
  Type:		ClusterIP
  Port:		db	3306/TCP

Database Secret:
  Name:	mysql-init-script-auth
  Type:	Opaque
  password:	16 bytes
  user:		4 bytes

No Snapshots.

  FirstSeen   LastSeen   Count     From             Type       Reason       Message
  ---------   --------   -----     ----             --------   ------       -------
  9m          9m         1         MySQL operator   Normal     Successful   Successfully patched StatefulSet
  9m          9m         1         MySQL operator   Normal     Successful   Successfully patched MySQL
  9m          9m         1         MySQL operator   Normal     Successful   Successfully created StatefulSet
  9m          9m         1         MySQL operator   Normal     Successful   Successfully created MySQL
  9m          9m         1         MySQL operator   Normal     Successful   Successfully created Service

$ kubectl get statefulset -n demo
NAME                DESIRED   CURRENT   AGE
mysql-init-script   1         1         10m

$ kubectl get pvc -n demo
NAME                       STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-mysql-init-script-0   Bound     pvc-ec9fd7b2-0d8a-11e8-9091-08002751ae8c   50Mi       RWO            standard       12m

$ kubectl get pv -n demo
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                           STORAGECLASS   REASON    AGE
pvc-ec9fd7b2-0d8a-11e8-9091-08002751ae8c   50Mi       RWO            Delete           Bound     demo/data-mysql-init-script-0   standard                 12m

$ kubectl get service -n demo
NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubedb              ClusterIP   None            <none>        <none>     13m
mysql-init-script   ClusterIP   <none>        3306/TCP   13m

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

$ kubedb get my -n demo mysql-init-script -o yaml
kind: MySQL
  clusterName: ""
  creationTimestamp: 2018-02-09T11:18:14Z
  generation: 0
  name: mysql-init-script
  namespace: demo
  resourceVersion: "28624"
  selfLink: /apis/
  uid: ebbcc002-0d8a-11e8-9091-08002751ae8c
    secretName: mysql-init-script-auth
  doNotPause: true
        directory: .
    - ReadWriteOnce
        storage: 50Mi
    storageClassName: standard
  version: 8
  creationTime: 2018-02-09T11:18:14Z
  phase: Running

Please note that 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 user 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 spec.databaseSecret.secretName. While creating this secret manually, make sure the secret contains these two keys containing data user and password and also make sure of using root as value of user.

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

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

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:

$ kubedb delete my,drmn,snap -n demo --all --force

$ kubectl delete ns demo
namespace "demo" deleted

Next Steps