1

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
  • 173
  • 1
  • 9
  • https://stackoverflow.com/questions/45681780/how-to-initialize-mysql-container-when-created-on-kubernetes – Ijaz Ahmad Jul 13 '19 at 20:19
  • 1
    Possible duplicate of [How to initialize mysql container when created on Kubernetes?](https://stackoverflow.com/questions/45681780/how-to-initialize-mysql-container-when-created-on-kubernetes) – helmbert Jul 14 '19 at 12:40

2 Answers2

3

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
  • 173
  • 1
  • 9
1

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
  • 27,020
  • 6
  • 48
  • 102