<p>First of all: your <code>docker-compose.yml</code> file is absolutely fine. I just tested it, created the necessary dir for bind mount, started the compose project (<code>docker-compose up -d</code>) and was able to login into mysql (<code>docker-compose exec db bash -c "mysql -u root -proot"</code> - worked with <code>wordpress</code> as well).</p>

<p>So now I'll take a guess (which is usually good...): you already have an initialized mysql filesystem in <code>./tmp/db</code> from a previous run (or copied from somewhere else). In this case, here is what you might have missed in the <a href="https://hub.docker.com/_/mysql/" rel="noreferrer">mysql docker image documentation</a> in the section on environment variables:</p>

<blockquote>
  <p>Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup</p>
</blockquote>

<p>If you want to have a look under the hood how this happens, you can study the <a href="https://github.com/docker-library/mysql/blob/6952c5d5a9889311157362c528d65dc2e37ff660/5.7/docker-entrypoint.sh" rel="noreferrer">image entrypoint script</a></p>

<p>So, as you may have now understood, your container starts but it is using the existing mysql filesystem with the previous settled dbs, data, users and passwords.</p>

<p>Simply stop your container, delete any files and directories in <code>./tmp/db</code> and restart from scratch: you should get your blank db install and be able to login in mysql with the users/pass defined in <code>docker-compose.yml</code></p>

CC BY-SA 4.0
2020-01-26T23:59:12.523Z
Genius! Thank you very much
Amanda Ferrari
CC BY-SA 4.0
2020-01-27T00:39:01.883Z
What you call genius is simply experience: I once did the same mistake and fixed it afterwards for a lot of my work mates. Now you know, you can pass it on.
Zeitounator
CC BY-SA 4.0
2020-01-26T21:10:34.360Z
2020-01-26T21:16:12.477Z
2020-01-26T21:16:12.477Z
Zeitounator
<p>I'm configuring a docker compose file, and the first step is to configure the MySQL service (so I can further use in my app service), but no matter what, I cannot enter MySQL within the container, even when I set all environments. That's how my docker-compose.yml file looks like:</p>

<pre><code>version: "3.3"

services:
  db:
    image: mysql:5.7
    volumes:
      - ./tmp/db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
</code></pre>

<p>I've tried SEVERAL (I literally lost count of how many) other configurations, and none of them work, it just doesn't work.</p>

<p>If I go inside the container with <code>docker exec it ${CONTAINER_NUMBER} bash</code>
and try to access MySQL, it will ALWAYS return:</p>

<pre><code>ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
</code></pre>

<p>Or if I try with the user wordpress:</p>

<pre><code>ERROR 1045 (28000): Access denied for user 'wordpress'@'localhost' (using password: YES)
</code></pre>

<p>I'm pretty confident that I spent more than 8 hours only changing the configuration, so the only thing I can think right now is: the image is broken.</p>

<p>I'm in a Ubuntu 19, not sure if this makes any difference.</p>

<docker><docker-compose><containers>
Can't make MySQL 5.7 work with Docker Compose, no matter what
Amanda Ferrari
# Can't make MySQL 5.7 work with Docker Compose, no matter what

(Asked by: username_0 on 2020-01-26)

I'm configuring a docker compose file, and the first step is to configure the MySQL service (so I can further use in my app service), but no matter what, I cannot enter MySQL within the container, even when I set all environments. That's how my docker-compose.yml file looks like:
version: "3.3"

services:
  db:
    image: mysql:5.7
    volumes:
      - ./tmp/db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

I've tried SEVERAL (I literally lost count of how many) other configurations, and none of them work, it just doesn't work.
If I go inside the container with docker exec it ${CONTAINER_NUMBER} bash
and try to access MySQL, it will ALWAYS return:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Or if I try with the user wordpress:
ERROR 1045 (28000): Access denied for user 'wordpress'@'localhost' (using password: YES)

I'm pretty confident that I spent more than 8 hours only changing the configuration, so the only thing I can think right now is: the image is broken.
I'm in a Ubuntu 19, not sure if this makes any difference.

**Tags:** docker, docker-compose, containers


---

## 1 answers

### Answer by username_1 on 2020-01-26. Score: 5 | Accepted answer

First of all: your `docker-compose.yml` file is absolutely fine. I just tested it, created the necessary dir for bind mount, started the compose project (`docker-compose up -d`) and was able to login into mysql (`docker-compose exec db bash -c "mysql -u root -proot"` - worked with `wordpress` as well).

So now I'll take a guess (which is usually good...): you already have an initialized mysql filesystem in `./tmp/db` from a previous run (or copied from somewhere else). In this case, here is what you might have missed in the [mysql docker image documentation](https://hub.docker.com/_/mysql/) in the section on environment variables:

> Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup

If you want to have a look under the hood how this happens, you can study the [image entrypoint script](https://github.com/docker-library/mysql/blob/6952c5d5a9889311157362c528d65dc2e37ff660/5.7/docker-entrypoint.sh)

So, as you may have now understood, your container starts but it is using the existing mysql filesystem with the previous settled dbs, data, users and passwords.

Simply stop your container, delete any files and directories in `./tmp/db` and restart from scratch: you should get your blank db install and be able to login in mysql with the users/pass defined in `docker-compose.yml`

> **Comments:**
> **username_0**: Genius! Thank you very much  
> **username_1**: What you call genius is simply experience: I once did the same mistake and fixed it afterwards for a lot of my work mates. Now you know, you can pass it on.  

---

