How to start a mysql container in Kubernetes with sample data?

7/13/2019

I need to start a MySQL container in Kubernetes with a database and a schema and sample data.

I tried to use the parameter "command" in the Kubernetes yaml, but at the time of execution, the db is still not started.

        - image: mysql:5.7.24
          name: database
          command:
            [
              '/usr/bin/mysql -u root -e "CREATE DATABASE IF NOT EXISTS mydbname"',
            ]
          env:
            - name: MYSQL_ALLOW_EMPTY_PASSWORD
              value: "1"
-- Luca Carducci
dump
google-kubernetes-engine
kubernetes
mysql

2 Answers

7/13/2019

you can first create the container of mysql and later import the data of mysql it will that way.

you can create the pvc volume and start the container black without any database. you can use the command exec to import the sql while and data to the database which will create the database and sample data inside the container.

start the container and go inside the container using exec mode and create a database and after that run this command

kubectl exec -i <container name> -- mysql -h <hostname> -u <username> -p<password> <databasename> > databasefile.sql
-- Harsh Manvar
Source: StackOverflow

7/31/2019

Solved adding

      volumeMounts:
        - name: initdb
          mountPath: /docker-entrypoint-initdb.d

...
  volumes:
    - name: initdb
      configMap:
        name: initdb-config

...
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: initdb-config
data:
  initdb.sql: |
      mysqlquery
-- Luca Carducci
Source: StackOverflow