0

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
  • 2,309
  • 4
  • 38
  • 62
  • ...you need to create a ConfigMap with the SQL script, and mount it on `/docker-entrypoint-initdb.d`. This is almost the same as in plain Docker. – David Maze Feb 01 '20 at 12:15
  • @DavidMaze I will try that option. BTW, did you mean, copying a DDL file to `docker-entrypoint-initdb.d` in a `Dockerfile` is the same thing? – cogitoergosum Feb 01 '20 at 12:21
  • Yes, or mounting it as a volume at startup time. – David Maze Feb 01 '20 at 12:23
  • Ok, last question. What is wrong with the `command` and `args` way that I have in my `Deployment`? – cogitoergosum Feb 01 '20 at 12:24
  • 1
    You can't pass an arbitrary SQL command as a command-line parameter to the MySQL daemon. Even if you could, shell syntax (like heredocs) doesn't work unless you manually provide a shell to interpret it (`sh -c ...`), but a YAML [block scalar](https://yaml.org/spec/1.2/spec.html#id2793652) is functionally similar. – David Maze Feb 01 '20 at 12:30
  • So, [this YAML](https://pastebin.com/SvhkhcLq) invokes mysql but fails with any or no value of `-h`. Omitting `-h` causes a connection over Unix socket and fails. Using `localhost` or `127.0.0.1` for `-h` also fails with `Can't connect` error message. Using the `ConfigMap` option of saving the script will make the password visible. – cogitoergosum Feb 01 '20 at 14:11
  • @DavidMaze I have figured out the way to use YAML block header for creating database and user at start-up. Can you please reopen the question so that I can add my answer? – cogitoergosum Feb 01 '20 at 14:30

0 Answers0