<p>I have created a persistent MySQL database on a Kubernetes Cluster following this <a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/" rel="nofollow noreferrer">tutorial</a>. I am trying to create the tables on initialization but cant get it to work.</p>
<p>I tried two different approaches.</p>
<p>First creating a new Config map:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-initdb-config
data:
  initdb.sql: |
    #SQL GOES HERE
</code></pre>
<p>And I added it to the volumes here with the stateful service (the commented out lines are what I added):</p>
<pre><code>      volumes:
        - name: conf
          emptyDir: {}
        - name: config-map
          configMap:
            name: mysql
#        - name: mysql-initdb
#          configMap:
#            name: mysql-initdb-config
</code></pre>
<p>I also tried add the SQL data into the current config map in use:</p>
<pre><code>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
</code></pre>
<p>Once again referencing it in the stateful set:</p>
<pre><code>      volumes:
        - name: conf
          emptyDir: {}
        - name: config-map
          configMap:
            name: mysql
#        - name: mysql-initdb
#          configMap:
#            name: mysql;
</code></pre>
<p>Am I taking the complete wrong approach or is there just a small mistake?</p>

EoinHanan
<p>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 <strong>/docker-entrypoint-initdb.d</strong> after creating a new database. So we have to mount the initialization script on that specific directory.</p>
<p>First create a new configmap/secret that will be used as volume source.</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-initdb-config
data:
  initdb.sql: |
    #SQL GOES HERE
</code></pre>
<p>Then create a volume that will be mounted in  <strong>/docker-entrypoint-initdb.d</strong> directory.</p>
<pre><code>  volumes:
    - name: init-script
      configMap:
        name: mysql-initdb-config
</code></pre>
<p>Now mount this volume on  the specific directory.</p>
<pre><code>  volumeMounts:
    - name: init-script
      mountPath: /docker-entrypoint-initdb.d
</code></pre>
<p>Further read: <a href="https://hub.docker.com/_/mysql" rel="nofollow noreferrer">How to use MySQL image</a></p>
