<p>Hi I have followed some k8s tutorials on how to get going with setting up a local db + WordPress installation, but user can't connect to mysql within my cluster.
(everything else seems ok - in Kubernetes Dashboard Web UI)</p>

<blockquote>
  <p>Error: [15:40:55][~]#kubectl logs -f website-56677747c7-c7lb6
  [21-Nov-2019
  11:07:17 UTC] PHP Warning:  mysqli::__construct():
  php_network_getaddresses: getaddrinfo failed: Name or service not
  known in Standard input code on line 22 [21-Nov-2019 11:07:17 UTC] PHP
  Warning:  mysqli::__construct(): (HY000/2002):
  php_network_getaddresses: getaddrinfo failed: Name or service not
  known in Standard input code on line 22</p>
  
  <p>MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo
  failed: Name or service not known [21-Nov-2019 11:07:20 UTC] PHP
  Warning:  mysqli::__construct(): (HY000/1045): Access denied for user
  'websiteu5er'@'10.1.0.35' (using password: YES) in Standard input code
  on line 22</p>
  
  <p>MySQL Connection Error: (1045) Access denied for user
  'websiteu5er'@'10.1.0.35' (using password: YES)</p>
  
  <p>MySQL Connection Error: (1045) Access denied for user
  'websiteu5er'@'10.1.0.35' (using password: YES)</p>
  
  <p>MySQL Connection Error: (1045) Access denied for user
  'websiteu5er'@'10.1.0.35' (using password: YES)</p>
</blockquote>

<p>My Dockerfile (which I used to create the image pushed to docker hub then pulled into k8s service + deployment):</p>

<pre><code>FROM mysql:5.7

# This should create the following default root + user?
ENV MYSQL_ROOT_PASSWORD=hello123
ENV MYSQL_DATABASE=website
ENV MYSQL_USER=websiteu5er
ENV MYSQL_PASSWORD=hello123

RUN /etc/init.d/mysql start \
    &amp;&amp; mysql -u root --password='hello123' -e "GRANT ALL PRIVILEGES ON *.* TO 'websiteu5er'@'%' IDENTIFIED BY 'hello123';"


FROM wordpress:5.2.4-php7.3-apache

# Copy wp-config file over
COPY configs/wp-config.php .

RUN chown -R www-data:www-data *

COPY ./src/wp-content/themes/bam /var/www/html/wp-content/themes/bam
</code></pre>

gvanto
<p>The standard Docker Hub <a href="https://hub.docker.com/_/mysql" rel="nofollow noreferrer">mysql</a> image has the ability to run arbitrary SQL scripts <em>on the very first startup of the database only</em>.  It can also set up an initial database user with a known password, again on the first startup only.  Details are in the linked Docker Hub page.</p>

<p>In a Kubernetes context I’d use just the environment variables, and specify them in my pod spec.</p>

<pre><code>containers:
  - name: mysql
    image: mysql:5.7 # not a custom image
    env:
      - name: MYSQL_USER
        value: websiteu5er
      - name: MYSQL_PASSWORD
        value: hello123
</code></pre>

<p>If you did need more involved setup, I’d create a ConfigMap that contained SQL scripts, and then mount that into the container in <code>/docker-entrypoint-initdb.d</code>.</p>

<hr>

<p>There’s two things going on in your Dockerfile.  One is that, when you have multiple <code>FROM</code> lines, you’re actually executing a <em>multi-stage build</em>; the image you get out at the end is only the Wordpress image, and the MySQL parts before it get skipped.  The second is that you can’t actually create an image <code>FROM mysql</code> that contains any database-level configuration or content, so the image that comes out of the first stage has the environment variables set but won’t actually have executed your <code>GRANT PRIVILEGES</code> statement.</p>

<p>I’d just delete everything before the last <code>FROM</code> line and not try to build a derived MySQL image; use the <code>/docker-entrypoint-initdb.d</code> mechanism at startup time instead.</p>
