Tomáš Vavřinka
Tomáš Vavřinka

Reputation: 633

Access mysql db inside docker container from outside

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

Upvotes: 2

Views: 8236

Answers (1)

EchoMike444
EchoMike444

Reputation: 1692

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

Upvotes: 3

Related Questions