WordPress + MySQL deployed in Kubernetes - MySQL Connection Error

11/6/2020

A Kubernetes scenario with Wordpress + Mysql in a local environment.

Wordpress Pod is unable to connect to Mysql database with the following error from Wordpress Pod logs:

MySQL Connection Error: (1045) Access denied for user 'root'@'10.44.0.5' (using password: YES)
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'root'@'10.44.0.5' (using password: YES) in - on line 22

Instruction taken from kubernetes.io at link. The only change i made was creating a Secret resource to store password and to be pointed from Mysql and Wordpress containers.

apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
data:
  password: cGFzc3dvcmQxMjMK     --> that is base64 of password123
type: Opaque

Pods are in default namespace both on node1 that is a worker node:

NAME                               READY   STATUS    RESTARTS   AGE   IP          NODE    
wordpress-554dfbbc47-hnr4n         0/1     Error     1          66s   10.44.0.5   node1   
wordpress-mysql-5477cbdfbf-29w2r   1/1     Running   0          74s   10.44.0.4   node1   

i've no skills about mysql but if i get bash shall in Mysql container and execute:

# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Here the Service output:

NAME              TYPE           CLUSTER-IP       EXTERNAL-IP    PORT(S)       
wordpress         LoadBalancer   10.107.114.255   192.168.1.83   80:32336/TCP   
wordpress-mysql   ClusterIP      None             <none>         3306/TCP       

Some env variables from MySql Pod:

....
HOSTNAME=wordpress-mysql-5477cbdfbf-29w2r
MYSQL_MAJOR=5.6
MYSQL_ROOT_PASSWORD=password123
MYSQL_VERSION=5.6.50-1debian9
....

PersistentVolume are working fine.

Quite stucked going ahead with troubleshooting. Help would appreciated.

-- Marco_81
kubernetes
mysql
wordpress

2 Answers

11/6/2020

Did you apply your secret? is your secret available in kube env?

-- Abhijit Gaikwad
Source: StackOverflow

11/7/2020

After testing different images for Mysql and Wordpress and reading useful links on hub.docker.com mysql & wordpress i got the web application stack working.

The configuration:

MySQL:

apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 1Gi

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.7
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: root-pass
              key: password

        - name: MYSQL_DATABASE
          value: mysql

        - name: MYSQL_USER
          value: mysql

        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password

        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      nodeSelector:
        storage: local
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

Wordpress:

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
  externalIPs:
  - 192.168.1.83

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 1Gi

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress
        name: wordpress
        imagePullPolicy: IfNotPresent

        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql

        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password

        - name: WORDPRESS_DB_USER
          value: mysql

        - name: WORDPRESS_DB_NAME
          value: mysql

        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      nodeSelector:
        storage: local
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

*Output PersitentVolume:*

NAME             STATUS   VOLUME                        CAPACITY   ACCESS MODES   STORAGECLASS    
mysql-pv-claim   Bound    persistent-volume-mysql       4Gi        RWO            local-storage   
wp-pv-claim      Bound    persistent-volume-wordpress   2Gi        RWO            local-storage   

Secrets:

apiVersion: v1
kind: Secret
metadata:
  name: root-pass
  namespace: default
data:
  password: cGFzc3dvcmQ=
type: Opaque

apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
data:
  password: cGFzc3dvcmQ=
type: Opaque

Notes for my example configuration:

  • on node1 created directory /mysql/data & /wordpress/data (mount point for mysql and wordpress containers).
  • image used for mysql -> mysql:5.7
  • image used for wordpress -> wordpress
  • added environment variables according to the documentation of mysql and wordpress.
-- Marco_81
Source: StackOverflow