Reputation: 77
I have created a persistent MySQL database on a Kubernetes Cluster following this tutorial. I am trying to create the tables on initialization but cant get it to work.
I tried two different approaches.
First creating a new Config map:
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-initdb-config
data:
initdb.sql: |
#SQL GOES HERE
And I added it to the volumes here with the stateful service (the commented out lines are what I added):
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
# - name: mysql-initdb
# configMap:
# name: mysql-initdb-config
I also tried add the SQL data into the current config map in use:
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
namespace: thesis
labels:
app: mysql
data:
primary.cnf: |
[mysqld]
log-bin
replica.cnf: |
[mysqld]
super-read-only
initdb.sql: |
# SQL goes here
Once again referencing it in the stateful set:
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
# - name: mysql-initdb
# configMap:
# name: mysql;
Am I taking the complete wrong approach or is there just a small mistake?
Upvotes: 0
Views: 1024
Reputation: 408
When a mysql container is started for the first time, it will execute files with extensions .sh, .sql and sql.sz that are found in /docker-entrypoint-initdb.d after creating a new database. So we have to mount the initialization script on that specific directory.
First create a new configmap/secret that will be used as volume source.
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-initdb-config
data:
initdb.sql: |
#SQL GOES HERE
Then create a volume that will be mounted in /docker-entrypoint-initdb.d directory.
volumes:
- name: init-script
configMap:
name: mysql-initdb-config
Now mount this volume on the specific directory.
volumeMounts:
- name: init-script
mountPath: /docker-entrypoint-initdb.d
Further read: How to use MySQL image
Upvotes: 1