ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111) in OpenShift

12/6/2019

I have created a deployment with the below yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-slave
  namespace: mysql-dr2
spec:
  selector:
    matchLabels:
      app: mysql-slave
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql-slave
    spec:
      volumes:
        - name: ro-slave-nfs-pv-datadir-vol
          persistentVolumeClaim:
            claimName: ro-slave-nfs-pvc-datadir-claim
        - name: ro-slave-create-at-initcnfgmap-vol
          configMap:
            name: ro-slave-create-at-initcnfgmap
      containers:
      - image: mysql:5.7
        name: mysql-slave
        env:
        - name: MYSQL_SERVER_CONTAINER
          value: mysql-slave
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret2
                key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valueFrom:
              secretKeyRef:
                name: mysql-secret2
                key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
              secretKeyRef:
                name: mysql-secret2
                key: MYSQL_USER
        - name: MYSQL_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret2
                key: MYSQL_PASSWORD
        ports:
        - containerPort: 3309
          name: mysql-slave
        volumeMounts:
        - name: ro-slave-nfs-pv-datadir-vol
          mountPath: /var/lib/mysql
        - name: ro-slave-create-at-initcnfgmap-vol
          mountPath: /docker-entrypoint-initdb.d/initdb-create-aadhaarTbl.sql
          subPath: initdb-create-aadhaarTbl.sql

After that I placed file inside container by patching the deployment

    volumeMounts:
    - mountPath: /docker-entrypoint-initdb.d/ro_slave_setup_sp.sql
      name: ro-slave-repln-initcnfgmap-sp-vol
      subPath: ro_slave_setup_sp.sql

Now I need to execute this sql file again by patching the deployment for this I am injecting the below code into deployment

{
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "image": "mysql:5.7",
            "name": "mysql-slave",
            "lifecycle": {
              "postStart": {
                "exec": {
                  "command": [
                    "/bin/sh",
                    "-c",
                    "mysql -h localhost -P 3309 -uroot -ppassword < docker-entrypoint-initdb.d/ro_slave_setup_sp.sql"
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
}

After this patch container is crashing with the below error

[rouser@ldblncr mysql-slave]$ oc get pods
NAME                           READY   STATUS   RESTARTS   AGE
mysql-slave-565476d864-2slqc   0/1     PostStartHookError: command '/bin/sh -c mysql -h localhost -P 3309 -uroot -ppassword < docker-entrypoint-initdb.d/ro_slave_setup_sp.sql' exited with 1: mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111)
                               1       73s

But when I login to container and execute below its working fine.

mysql -uroot -ppassword -h localhost -P 3309 < docker-entrypoint-initdb.d/ro_slave_setup_sp.sql

Please help on this.

-- Abdul
docker
docker-compose
kubernetes
openshift

0 Answers