9

I'm going through getting-started tutorial (https://www.docker.com/101-tutorial - Docker Desktop) from docker and they have this docker-compose here:

version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

The problem is that MySQL is not creating the "todos" database. And then my application can't connect to it giving me this error:

app_1    | Error: ER_HOST_NOT_PRIVILEGED: Host '172.26.0.2' is not allowed to connect to this MySQL server
app_1    |     at Handshake.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
app_1    |     at Handshake.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
app_1    |     at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
app_1    |     at Parser._parsePacket (/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
app_1    |     at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
app_1    |     at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
app_1    |     at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:91:28)
app_1    |     at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:525:10)
app_1    |     at Socket.emit (events.js:310:20)
app_1    |     at addChunk (_stream_readable.js:286:12)
app_1    |     --------------------
app_1    |     at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
app_1    |     at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
app_1    |     at PoolConnection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
app_1    |     at Pool.getConnection (/app/node_modules/mysql/lib/Pool.js:48:16)
app_1    |     at Pool.query (/app/node_modules/mysql/lib/Pool.js:202:8)
app_1    |     at /app/src/persistence/mysql.js:35:14
app_1    |     at new Promise (<anonymous>)
app_1    |     at Object.init (/app/src/persistence/mysql.js:34:12)
app_1    |     at processTicksAndRejections (internal/process/task_queues.js:97:5) {
app_1    |   code: 'ER_HOST_NOT_PRIVILEGED',
app_1    |   errno: 1130,
app_1    |   sqlMessage: "Host '172.26.0.2' is not allowed to connect to this MySQL server",
app_1    |   sqlState: undefined,
app_1    |   fatal: true
app_1    | }

If I run this command alone to spin MySQL, the "todos" database is created:

docker run -d --network todo-app --network-alias mysql -v todo-mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=todos mysql:5.7

Is there any command that was updated or that doesn't work properly on windows with docker-compose?

Igor
  • 1,397
  • 3
  • 24
  • 56
  • Does this answer your question? [Mysql adding user for remote access](https://stackoverflow.com/questions/16287559/mysql-adding-user-for-remote-access) – maio290 May 21 '20 at 00:45
  • Two different docker containers are linked together in an own bridged network by default, thus mysql and your application have a different ip address. See: https://docs.docker.com/network/ – maio290 May 21 '20 at 00:46
  • It doesn't asnwer my question. It's not a mysql issue and the docker containers are linked in the same network as they are bundled together in the same docker-compose. Here (https://docs.docker.com/compose/networking/) it says: "By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name." – Igor May 21 '20 at 16:11
  • And about the mysql, it doesn't have anyhthing to do with the problem. I'm trying to connect with root, and as mentioned in the question it works when I run just the command line separately. I'm able to connect to mySQL using the root user and secret password. Also I'm able to see the databases and the "todos" database it not showing up, which means it's not being created when running the mysql container – Igor May 21 '20 at 16:18
  • `docker-compose up -d ` will you provide this commands output ? – Dupinder Singh May 25 '20 at 18:06
  • One way to resolve this quickly, remove the `volume` from docker-compose – Dupinder Singh May 25 '20 at 18:14
  • Which tutorial are you going through? Please include the link. – BMitch May 25 '20 at 18:35
  • This tutorial: https://www.docker.com/101-tutorial (Docker Desktop) – Igor May 25 '20 at 18:39
  • Include the logs from `docker-compose logs mysql`. You should see a line showing `[Note] [Entrypoint]: Creating database todos` – BMitch May 25 '20 at 19:10

2 Answers2

10

TL;DR;

Run the command

docker-compose down --volumes

to remove any problematic volume created during the tutorial early phases, then, resume your tutorial at the step Running our Application Stack.


I suppose that the tutorial you are following is this one.

If you did follow it piece by piece and tried some docker-compose up -d in the step 1 or 2, then you've probably created a volume without your todos database.

Just going docker-compose down with your existing docker-compose.yml won't suffice because volumes is exactly made for this, the volume is the permanent storage layer of Docker.

By default all files created inside a container are stored on a writable container layer. This means that:

  • The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.
  • A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.
  • Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.

Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. If you’re running Docker on Linux you can also use a tmpfs mount. If you’re running Docker on Windows you can also use a named pipe.

Source: https://docs.docker.com/storage/

In order to remove that volume, you probably created without your database there is an extra flag you can add to docker-compose down: the flag --volumes or, in short -v

-v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.

Source: https://docs.docker.com/compose/reference/down/

So your fix should be as simple as:

  1. docker-compose down --volumes
  2. docker-compose up -d, so back in your tutorial at the step Running our Application Stack
  3. docker-compose logs -f as prompted in the rest of the tutorial
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
0

Currently you're database todo is created inside your mysql container when you launch docker-compose start.

In fact, your issue come from mysql user permission.

Add the line below, at the end of your file which initialize todo database

  CREATE USER 'newuser'@'%' IDENTIFIED BY 'user_password';

That line will create a user : newuser and give it access from any host (%) with the password user_password

Follow by this line

GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%';

It'll grant all permissions on every database and every table you have to newuser from any host

Finally, change your mysql environment variable MYSQL_USER and MYSQL_PASSWORD with the new one you just create

version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: newuser
      MYSQL_PASSWORD: user_password
      MYSQL_DB: todos

  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:
Wonkledge
  • 1,732
  • 7
  • 16
  • 1
    The image of MySQL is a really elaborated one and does that for you already when you boot your container for the very first time https://github.com/mysql/mysql-docker/blob/26296a305c53a5ec21698d9273d2048995ab2c13/5.7/docker-entrypoint.sh#L115 – β.εηοιτ.βε May 25 '20 at 20:02