2

I'm learning to use docker to make my development easier but I'm still failing access mysql.

Here is my docker-compose.yaml:

version: '3.3'

services:
  # Database
  db:
    image: mysql:latest
    ports:
      - '3306:3306'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: dev1
      MYSQL_USER: root
      MYSQL_PASSWORD: password
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER:  root
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: dev1
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:

Wordpress is running without difficulties which means that mysql must be alright too. I'm on Linux and trying to connect database via mysql workbench. It appears that connection is also ok expect for, there is no schema and so no wordpress tables.

I tried to add also phpmyadmin into docker-compose.yaml:

phpmyadmin:
    depends_on:
  - db
image: phpmyadmin/phpmyadmin
restart: always
ports:
  - '8080:80'
environment:
  PMA_HOST: db
  MYSQL_ROOT_PASSWORD: password
networks:
  - wpsite

but here I get following error after attempt to access db: enter image description here

What I miss?

EDIT: here is overview of running containers: enter image description here

6
  • what does docker-compose ps show? Commented Mar 16, 2019 at 20:13
  • @Thomasleveil - I added screen with command result to the post Commented Mar 16, 2019 at 20:26
  • Why not map your MySQL port as 33060:3306 then you can be sure in that inside your container you use 3306 and outside it you use 33060? You do this for http and that works ok Commented Mar 16, 2019 at 20:27
  • @Caius I had tried but my problem persisted. Commented Mar 16, 2019 at 20:49
  • What does docker-compose exec db mysql -p -e "SHOW DATABASES" show? Commented Mar 16, 2019 at 20:51

1 Answer 1

3

So i modified your docker-compose , with 2 small changes , and i dont have a issue .

I created a user for wordpress ( userdev1 ) in mysql .

The root is already here and can have some restrictions for remote access .

Via phpmyadmin i can login with userdev1 or root

You want a network access with the root account you must set this variable
MYSQL_ROOT_HOST .

You can find more information on this page ( https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/docker-mysql-more-topics.html#docker_var_mysql-root-host )

version: '3.3'
services:
  # Database
  db:
    image: mysql:5.7
    ports:
      - '3306:3306'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password4root
      MYSQL_DATABASE: dev1
      MYSQL_USER: userdev1
      MYSQL_PASSWORD: password4dev1
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER:  userdev1
      WORDPRESS_DB_PASSWORD: password4dev1
      WORDPRESS_DB_NAME: dev1
    networks:
      - wpsite
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:

** UPDATED **

With the very last version of mysql docker image ( aka mysql 8.0 ), you must change the default-authentification to mysql_native_password to be comptatible with legacy mysql client

source : https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password

  image: mysql:latest
  command: --default-authentication-plugin=mysql_native_password
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is working. However the real cause why my code didn't work is in image mysql. For some reason is needed "image: mysql:5.7" because "image: mysql:latest" causes my issue. Thanks anyway.
@TomášVavřinka , i updated my response with a update for the last version of mysql ( aka 8.0 )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.