<p>A Kubernetes scenario with Wordpress + Mysql in a local environment.</p>
<p>Wordpress Pod is unable to connect to Mysql database with the following error from Wordpress Pod logs:</p>
<pre><code>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
</code></pre>
<p>Instruction taken from kubernetes.io at <a href="https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/" rel="nofollow noreferrer">link</a>. The only change i made was creating a Secret resource to store password and to be pointed from Mysql and Wordpress containers.</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
data:
  password: cGFzc3dvcmQxMjMK     --&gt; that is base64 of password123
type: Opaque
</code></pre>
<p>Pods are in default namespace both on node1 that is a worker node:</p>
<pre><code>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   
</code></pre>
<p>i've no skills about mysql but if i get bash shall in Mysql container and execute:</p>
<pre><code># mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
</code></pre>
<p>Here the <strong>Service</strong> output:</p>
<pre><code>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             &lt;none&gt;         3306/TCP       
</code></pre>
<p>Some env variables from MySql Pod:</p>
<pre><code>....
HOSTNAME=wordpress-mysql-5477cbdfbf-29w2r
MYSQL_MAJOR=5.6
MYSQL_ROOT_PASSWORD=password123
MYSQL_VERSION=5.6.50-1debian9
....
</code></pre>
<p>PersistentVolume are working fine.</p>
<p>Quite stucked going ahead with troubleshooting. Help would appreciated.</p>

Marco_81
<p>After testing different images for Mysql and Wordpress and reading useful links on hub.docker.com <a href="https://hub.docker.com/_/mysql" rel="nofollow noreferrer">mysql</a> &amp; <a href="https://hub.docker.com/_/wordpress" rel="nofollow noreferrer">wordpress</a> i got the web application stack working.</p>
<p>The configuration:</p>
<p><strong>MySQL</strong>:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
</code></pre>
<hr />
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 1Gi
</code></pre>
<hr />
<pre><code>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
</code></pre>
<p><strong>Wordpress</strong>:</p>
<pre><code>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
</code></pre>
<hr />
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 1Gi
</code></pre>
<hr />
<pre><code>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
</code></pre>
<p><em><strong>Output PersitentVolume</strong>:</em></p>
<pre><code>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   
</code></pre>
<p><strong>Secrets</strong>:</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: root-pass
  namespace: default
data:
  password: cGFzc3dvcmQ=
type: Opaque
</code></pre>
<hr />
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
data:
  password: cGFzc3dvcmQ=
type: Opaque
</code></pre>
<p><em>Notes</em> for my example configuration:</p>
<ul>
<li>on node1 created directory /mysql/data &amp; /wordpress/data (mount point for mysql and wordpress containers).</li>
<li>image used for mysql     -&gt; mysql:5.7</li>
<li>image used for wordpress -&gt; wordpress</li>
<li>added environment variables according to the documentation of mysql and wordpress.</li>
</ul>
