5

I'm creating a laravel project in a docker container, along with MySQL and phpmyadmin, when trying to migrate (or access the database from phpmyadmin) I get access denied error.

I've tried several SOF solutions but none of them worked, also tried ones in GitHub issues.

here is my docker-compose.yml

version: "3"

services:
  web:
    container_name: ${APP_NAME}_web
    build:
      context: ./docker/web
    ports:
      - 9000:80
    volumes:
      - ./:/var/www/app
    networks:
      - mynet
  db:
    image: mysql:5.7
    container_name: db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laracocodb
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - mysqldata:/var/lib/mysql/
    networks:
      - mynet

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpma
    links:
      - db:db
    ports:
      - 9191:80
    environment:
      MYSQL_USERNAME: root
      MYSQL_ROOT_PASSWORD: root
      PMA_HOST: db
    networks:
      - mynet
networks:
  mynet:
    driver: bridge
volumes:
  mysqldata:
    driver: local

no matter where I access the database (from db container bash, from phpmyadmin index page or from the web service when trying to migrate the database), the error is always access denied

joe_inz
  • 998
  • 2
  • 13
  • 30
  • I don't know much about docker, but sometimes (depending on hosting) you may need to add the caller's ip address to the Remote Mysql access list. – Arthur Hylton Jun 03 '19 at 16:01
  • can you explain more? – joe_inz Jun 03 '19 at 16:05
  • https://dev.mysql.com/doc/refman/8.0/en/adding-users.html – Lightness Races in Orbit Jun 03 '19 at 16:07
  • I don't think that is the solution, because to create an account or a user you should log to MySQL first but that is denied, I've read that when creating a service of MySQL in docker and specify the credentials like in environment variables in docker-compose file they will be configured with that service and can be used inside the container. – joe_inz Jun 03 '19 at 16:12
  • @joe_inz what i mean is, in the hosting account software (usually cPanel for me) there is a section called Remote MySQL [something...] that requires you to add (whitelist) the ip address of any server or machine that will be requesting login access to any mySQL database. – Arthur Hylton Jun 03 '19 at 17:56
  • How is that related to docker container, I'm not in any hosting service ... This is locally! – joe_inz Jun 04 '19 at 18:07

1 Answers1

3

I have also run into this problem many times, by default MySQL allows root to be accessed by localhost user that means even if you have opened the port 3306:3306, you will still need to add the user.

Follow these commands and the error will resolve!

https://stackoverflow.com/a/11225588

Geet Choubey
  • 1,069
  • 7
  • 23
  • when you were using MySQL inside a docker container? – joe_inz Jun 03 '19 at 16:21
  • Yes. I was using MySQL inside docker container. Had a separate Dockerfile for it if you want to automate it. – Geet Choubey Jun 04 '19 at 19:07
  • can provide the docker file for mysql container please, I'm a little bit new to docker actually – joe_inz Jun 05 '19 at 05:22
  • 1
    i've managed to solve the problem with a new docker compose configuration, it turns out that the old one didn't create the user root and the environment vars at the creation of mysql container that's why i coudn't even log into it with root. Anyway your answer did help me so i'll mark it as best, thank you – joe_inz Jun 05 '19 at 07:52