Can't access mysql root or user after kubernetes deployment

12/26/2020

I have a nodejs app running as a Service that requires access to a mysql database running as another service (same namespace).

I also have a mysql file that I will be importing to the database.

here is my workflow :

  1. Set up a Secret that contain the root password alongside new database credentials (db_name, db_user, db_password).

  2. Set up a ConfigMap with SQL script to create the db structure.

  3. Finally, deploy mysql with pv/pvc, here is the yaml file content :

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---      
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: DB_ROOT
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: DB_NAME
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: DB_USER
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: DB_PASS
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
        - name: mysql-initdb
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim        
      - name: mysql-initdb
        configMap:
          name: mysql-initdb-config

Because my nodejs app doesn't access the database, I wanted to verify if the new db has been created and the sql file imported, running kubectl exec followed by mysql -u root -p is giving me this error :

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Within the pod, running echo $MYSQL_ROOT_PASSWORD or any other env variables returns the correct value.

What am I doing wrong ?

-- jaquemus
kubernetes
mysql

1 Answer

12/26/2020

Solved the issue by editing the line : mountPath: /var/lib/mysql

I changed it to /mnt/data and the deployment works like a charm.

-- jaquemus
Source: StackOverflow