Error establishing a database connection Error Kubernates wordpress & mysql

mysql-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 80
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: DEVOPS1
        - name: MYSQL_USER
          value: wpuser
        - name: MYSQL_PASSWORD
          value: DEVOPS12345
        - name: MYSQL_DATABASE
          value: wpdb

mysql-service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-service
    spec:
      selector:
        app: mysql
      ports:
        - protocol: TCP
          port: 3306
          targetPort: 3306

wordpress-deployment.yaml

                apiVersion: apps/v1
                kind: Deployment
                metadata:
                  name: wordpress-deployment
                  labels:
                    app: wordpress
                spec:
                  replicas: 3
                  selector:
                    matchLabels:
                      app: wordpress
                  template:
                    metadata:
                      labels:
                        app: wordpress
                    spec:
                      containers:
                      - name: wordpress
                        image: wordpress
                        ports:
                        - containerPort: 80
                        env:
                        - name: WORDPRESS_DB_HOST
                          value: mysql-service
                        - name: WORDPRESS_DB_USER
                          value: wpuser
                        - name: WORDPRESS_DB_PASSWORD
                          value: DEVOPS12345
                        - name: WORDPRESS_DB_NAME
                          value: wpdb
                        - name: WORDPRESS_DEBUG
                          value: "1"

wordpress-service.yaml

 apiVersion: v1
    kind: Service
    metadata:
      name: wordpress-service
    spec:
      type: NodePort
      selector:
        app: wordpress
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80

Please note : I used valid mysql credintials. (username,passwords) I double checked.

Result

Hey @Sameera_Wijerathna I just took a peak at the config, and the service name in the WORDPRESS_DB_HOST is using an _ instead of a -. I did a test with that corrected WP came up without the warning.

env:
  - name: WORDPRESS_DB_HOST
    value: mysql-service

@macintoshprime I modified. it . but same error has been showed me

Can you run kubectl get services to see what is listed? Also is there any useful information being displayed in the logs (kubectl logs -l app=wordpress)?

Here’s the deployment YAML I used and a screenshot showing what I get after applying it.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deployment
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        ports:
        - containerPort: 80
        env:
          - name: WORDPRESS_DB_HOST
            value: mysql-service
          - name: WORDPRESS_DB_USER
            value: wpuser
          - name: WORDPRESS_DB_PASSWORD
            value: DEVOPS12345
          - name: WORDPRESS_DB_NAME
            value: wpdb
          - name: WORDPRESS_DEBUG
            value: “1”
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-service
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Thanks @macintoshprime . Please note , i m using AWS environment. here are the logs and others

Here is the online link, you can check it
http://13.250.30.67:32067/

Service Lists

Wordpress logs

root@ip-172-31-34-220:~/wordpress# kubectl logs -l app=wordpress
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
No 'wp-config.php' found in /var/www/html, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_DB_HOST WORDPRESS_DB_PASSWORD WORDPRESS_DB_USER WORDPRESS_SERVICE_PORT WORDPRESS_SERVICE_PORT_80_TCP WORDPRESS_SERVICE_PORT_80_TCP_ADDR WORDPRESS_SERVICE_PORT_80_TCP_PORT WORDPRESS_SERVICE_PORT_80_TCP_PROTO WORDPRESS_SERVICE_SERVICE_HOST WORDPRESS_SERVICE_SERVICE_PORT)
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.2.14. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.2.14. Set the 'ServerName' directive globally to suppress this message
[Tue Apr 06 11:28:23.823529 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.16 configured -- resuming normal operations
[Tue Apr 06 11:28:23.824749 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
10.244.2.1 - - [06/Apr/2021:11:29:15 +0000] "-" 408 0 "-" "-"
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
No 'wp-config.php' found in /var/www/html, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_DB_HOST WORDPRESS_DB_PASSWORD WORDPRESS_DB_USER WORDPRESS_SERVICE_PORT WORDPRESS_SERVICE_PORT_80_TCP WORDPRESS_SERVICE_PORT_80_TCP_ADDR WORDPRESS_SERVICE_PORT_80_TCP_PORT WORDPRESS_SERVICE_PORT_80_TCP_PROTO WORDPRESS_SERVICE_SERVICE_HOST WORDPRESS_SERVICE_SERVICE_PORT)
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.1.15. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.1.15. Set the 'ServerName' directive globally to suppress this message
[Tue Apr 06 11:28:27.341294 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.16 configured -- resuming normal operations
[Tue Apr 06 11:28:27.341490 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
No 'wp-config.php' found in /var/www/html, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_DB_HOST WORDPRESS_DB_PASSWORD WORDPRESS_DB_USER WORDPRESS_SERVICE_PORT WORDPRESS_SERVICE_PORT_80_TCP WORDPRESS_SERVICE_PORT_80_TCP_ADDR WORDPRESS_SERVICE_PORT_80_TCP_PORT WORDPRESS_SERVICE_PORT_80_TCP_PROTO WORDPRESS_SERVICE_SERVICE_HOST WORDPRESS_SERVICE_SERVICE_PORT)
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.2.13. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.2.13. Set the 'ServerName' directive globally to suppress this message
[Tue Apr 06 11:28:20.244677 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.16 configured -- resuming normal operations
[Tue Apr 06 11:28:20.244862 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
10.244.2.1 - - [06/Apr/2021:11:28:23 +0000] "GET / HTTP/1.1" 500 2834 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
10.244.2.1 - - [06/Apr/2021:11:28:43 +0000] "GET /favicon.ico HTTP/1.1" 500 2834 "http://54.254.159.162:32067/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"

Please help me

Hey Sameera,

Been thinking about this and just haven’t had a chance to get back to you. Sorry for the delay. Is the mysql pod throwing any errors or miss behaving in anyway?

Have you used any other service to see if you can connect to the mysql-service endpoint? Here’s a guide for checking DNS resolution in K8s. Would you be using network policies by any chance? I’m assuming everything is in the same namespace.

Please help me to resolve this . I m novice person to this . I found the issue. i cannot see AVAILABILITY status in DEPLOYMENTS. check this screenshot . then i run kubectl describe all command

Here is the result

Here are the all YAML files

mysql-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 80
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: DEVOPS1
        - name: MYSQL_USER
          value: wpuser
        - name: MYSQL_PASSWORD
          value: DEVOPS12345
        - name: MYSQL_DATABASE
          value: wpdb

mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306

wordpress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deployment
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql-service
        - name: WORDPRESS_DB_USER
          value: wpuser
        - name: WORDPRESS_DB_PASSWORD
          value: wpdb


wp-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-service
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

For the flannel network issue are you using EKS to provision your cluster or is it from EC2 instances with kubeadm or other tool?

If you have access to the nodes you might want to check to see what errors the kubelet and kube-proxy logs are showing. Reference → Troubleshoot Clusters | Kubernetes

It also looks like some other users bumped into a similar issue and the solution was tracked in this github issue

Thanks @macintoshprime , I fixed it. now i got DB Connectivity error .

AWS Firewall

mysql-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 80
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: DEVOPS1
        - name: MYSQL_USER
          value: wpuser
        - name: MYSQL_PASSWORD
          value: DEVOPS12345
        - name: MYSQL_DATABASE
          value: wpdb

mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306

wordpress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deployment
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql-service
        - name: WORDPRESS_DB_USER
          value: wpuser
        - name: WORDPRESS_DB_PASSWORD
          value: wpdb

wp-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-service
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

New errors = Progress :slight_smile: What do the wordpress logs say?

Try switching the WORDPRESS_DB_PASSWORD to DEVOPS12345 .

@macintoshprime , Done. No luck

http://18.140.61.142:30617/
http://13.229.248.221:30617/

Also i modified my code like this . it also error

last night I modified my files, I posed it to stackoverflow.

Please if there is any genius. Help me. I need to complete my Assignment ASAP :frowning:

Hey Sameera. Took another look and I was able to get things up and running but not in an AWS cluster (I don’t have access).

I put the working yaml in a code share so you could take a look here . Based the changes off of the example from two post ago, only had to change the DB_HOST and DB_PASSWORD vars to get it working.

If that’s still giving you issues it might be something on the EKS side that’s giving some issues. Did you apply any network policies that might be interfering with communication (kubectl get networkpolicies)?

1 Like

@macintoshprime This is not EKS related thing . i m trying to build up Kubernetes + docker + wordpress+mysql hosting by manually. simply says this is on premise cluster

Sadly I don’t have much relevant AWS experience. If the provided YAML doesn’t work then it might be something in the configuration of the cluster.

You might want to check the required ports to see if they are open, checking the kubelet and kube-proxy logs on the hosts should help with that.

Were you able to test the DNS to see if you could access the service from another pod? That would help isolate the issue to either being in the deployment or a cluster issue.

What tool did you use to provision the cluster, kops, kubeadm?

Hey, did you find a solution i’m facing same problem here

I found this very helpful.
Also used this php - Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) - Stack Overflow

hi Sameera_Wijerathna, is this issue resolved? i am also facing the same problem