Openshift oc patch not executing initdb.sql from /docker-entrypoint-initdb.d

11/21/2019

OpenShift:

I have the below MySQL Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-master
spec:
  selector:
    matchLabels:
      app: mysql-master
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql-master
    spec:
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: ro-mstr-nfs-datadir-claim
      containers:
      - image: mysql:5.7
        name: mysql-master
        env:
        - name: MYSQL_SERVER_CONTAINER
          value: mysql
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_USER
        - name: MYSQL_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_PASSWORD
        ports:
        - containerPort: 3306
          name: mysql-master
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql

I created a deployment using this yml file which created a deployment and pod which is successfully running.

And I have a configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: ro-mstr-mysqlinitcnfgmap
data:
  initdb.sql: |-
    CREATE TABLE aadhaar ( name varchar(255) NOT NULL,
    sex char NOT NULL, birth DATE  NOT NULL, death DATE  NULL,
    id int(255) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) );
    CREATE USER 'usera'@'%' IDENTIFIED BY 'usera';
    GRANT REPLICATION SLAVE ON *.* TO 'usera' IDENTIFIED BY 'usera';
    FLUSH PRIVILEGES;

Now I need to patch the above deployment using this configmap. I am using the below command

oc patch deployment mysql-master -p '{ "spec": { "template": { "spec": { "volumes": [ { "name": "ro-mysqlinitconf-vol", "configMap": { "name": "ro-mstr-mysqlinitcnfgmap" } } ], "containers": [ { "image": "mysql:5.7", "name": "mysql-master", "volumeMounts": [ { "name": "ro-mysqlinitconf-vol", "mountPath": "/docker-entrypoint-initdb.d" } ] } ] } } } }'

So the above command is successful, I validated the Deployment description and inside the container it placed the initdb.sql file successfully, and recreated the pod. But the issue is it has not created the aadhaar table. I think it has not executed the initdb.sql file from docker-entrypoint-initdb.d.

-- Abdul
docker
kubernetes
openshift

1 Answer

11/21/2019

If you dive into the entrypoint script in your image (https://github.com/docker-library/mysql/blob/75f81c8e20e5085422155c48a50d99321212bf6f/5.7/docker-entrypoint.sh#L341-L350) you can see it only runs the initdb.d files if it is also creating the database the first time. I think maybe you assumed it always ran them on startup?

-- coderanger
Source: StackOverflow