Kubernetes - Create database in MySQL after pod start-up

2/1/2020

This is my Deployment YAML for launching a MySQL pod and then create a database and a user. The MySQL commands do not run. If I remove the -h localhost argument, it tries to connect over Unix socket and fails. If I provide the -h localhost or -h 127.0.0.1, then I get an error as server not found. Since this set-up is for a quick demo, I am not keen on running script after start-up or creating a new Dockerfile of off base mysql image.

How do I get the MySQL commands to run with heredoc?

kind: Deployment
apiVersion: apps/v1
metadata:
  name: mysql
  labels:
    run: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      run: mysql
  template:
    metadata:
      labels:
        run: mysql
    spec:
      containers:
        - name: mysql
          image: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: R00t.Passw0rd
            - name: MYSQL_PASSWORD
              value: Dem0.Passw0rd
          command:
            - mysql
          args:
            - -h 127.0.0.1 
            - -u root 
            - -p $(MYSQL_ROOT_PASSWORD)
            - << END
              create database demodb;
              create user demo identified with mysql_native_password by $(MYSQL_PASSWORD);
              grant all on demodb.* to demo;
              END
          resources:
            limits:
              memory: "512Mi"
              cpu: "200m"
          ports:
          - containerPort: 3306
            protocol: TCP          
          imagePullPolicy: Always
      restartPolicy: Always
-- cogitoergosum
containers
database-connection
ddl
kubernetes
mysql

0 Answers