PrestaShop / docker

🐳
https://hub.docker.com/r/prestashop/prestashop/
MIT License
263 stars 180 forks source link

Access denied to MySQL container from Prestashop container #253

Closed maitre-hibou closed 3 years ago

maitre-hibou commented 3 years ago

Hello there,

I'm trying to setup a local dev environment for Prestashop on a Docker Compose stack. (edit : I setup this stack on a Windows WSL2 installation using Docker for Windows) When running my stack, auto install fails because Prestashop container is unable to connect to MySQL container using provided credentials.

Here is my docker-compose.yml config :

version: '3'

networks:
  default:

services:
  database:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=5ecr3t
      - MYSQL_DATABASE=prestashop
      - MYSQL_USER=xpressive
      - MYSQL_PASSWORD=5ecr3t
    networks:
      - default
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - database
      - shop
    environment:
      - PMA_HOST=database
    networks:
      - default
    ports:
      - 8081:80
  shop:
    build: ./docker/prestashop
    depends_on: 
      - database
    environment: 
      - ADMIN_MAIL=contact@xpressive.io
      - ADMIN_PASSWORD=5ecr3t
      - DB_SERVER=database
      - DB_DATABASE=prestashop
      - DB_USER=xpressive
      - DB_PASSWORD=5ecr3t
      - PS_COUNTRY=FR
      - PS_DEMO_MODE=1
      - PS_DEV_MODE=1
      - PS_DOMAIN=prestashop.xpressive.local
      - PS_INSTALL_AUTO=1
      - PS_LANGUAGE=fr
    networks:
      - default

And here is the error shown in MySQL container logs :

[Note] Access denied for user 'xpressive'@'192.168.224.3' (using password: YES)

However, when I run the following command, I'm able to connect to MySQL container :

docker-compose exec shop mysql -uxpressive -p5ecr3t prestashop

I've tried to override the docker_run.sh script to display the value of environment variable with the following :

    while [ $RET -ne 0 ]; do
        #I added this line ...
        echo "\n* Checking if $DB_USER:$DB_PASSWORD@$DB_SERVER:$DB_PORT/$DB_NAME is available..."
        #... which displays : Checking if xpressive:5ecr3t@database:3306/prestashop is available...

        # This is the command failing
        mysql -h$DB_SERVER -P$DB_PORT -u$DB_USER -p$DB_PASSWD -e "status" > /dev/null 2>&1
        RET=$?

        if [ $RET -ne 0 ]; then
            echo "\n* Waiting for confirmation of MySQL service startup";
            sleep 3
        fi
    done

I'm guessing that error is due to the fact that IP address is used instead of FQDN, but I don't know why...

Anyone has a clue on this ?

Thanks in advance !

matks commented 3 years ago

@maitre-hibou Did you look at the Troubleshooting section of the README https://github.com/PrestaShop/docker#troubleshooting ? Your issue might be one of the issues mentioned (and solved)

maitre-hibou commented 3 years ago

@matks Yeah I've seen this section and already tried to add --protocol=tcp to the mysql connection line (the only potential fix that correspond to my setup), but I still have the issue. I forgot to specify that I run this stack on Windows WSL 2 using Docker for Windows.

PierreRambaud commented 3 years ago

The problem maybe comes from Docker for Windows. Be sure when you started your MySQL container that everything is ok. Try to connect into this one and test if you can perform SQL queries.

maitre-hibou commented 3 years ago

@PierreRambaud I did and everything is OK. I can perform MySQL queries directly into the container without problem.

PierreRambaud commented 3 years ago

@maitre-hibou You're doing it inside the container or outside? It's weird that it's not working for you :sweat:

maitre-hibou commented 3 years ago

@PierreRambaud When both container are started, in MySQL (database) container logs, I see the error message Access denied ...

But at the same time I can run the following command with success and perform queries :

docker-compose exec database mysql -uxpressive -p5ecr3t prestashop

I just can't login to MySQL on database container from my shop container.

I forgot to mention that this issue happens only when I try to auto install shop. When I perform a manual installation and when I'm asked to provide database credentials and test connection, it works perfectly.

PierreRambaud commented 3 years ago

Be sure you're using the container name during the installation: https://github.com/PrestaShop/PrestaShop/blob/develop/docker-compose.yml#L26

maitre-hibou commented 3 years ago

I checked and I'm using the correct name (I tried both the name of the service - database - and the container name generated by docker compose - prestashop_dev_database_1)

PierreRambaud commented 3 years ago

I didn't manage to reproduce the issue :(

maitre-hibou commented 3 years ago

@PierreRambaud Did you run it on Docker WSL or on Docker on Linux ? Just to know where to search more infos

PierreRambaud commented 3 years ago

I'm running it on Linux ^^

maitre-hibou commented 3 years ago

Ok I set up a fresh dual boot Linux Mint install, performed Docker installation by following docs, and tried to make my docker-compose.yml file simpler :

version: "3"

services:
  shop:
    image: prestashop/prestashop:1.7
    depends_on: 
      - database
    environment: 
      - ADMIN_MAIL=contact@xpressive.io
      - ADMIN_PASSWORD=5ecr3t
      - DB_SERVER=database
      - DB_DATABASE=prestashop
      - DB_USER=prestashop
      - DB_PASSWORD=5ecr3t
      - PS_COUNTRY=FR
      - PS_DEMO_MODE=1
      - PS_DEV_MODE=1
      - PS_DOMAIN=prestashop.xpressive.local
      - PS_INSTALL_AUTO=1
      - PS_LANGUAGE=fr
  database:
    image: mysql:5.7
    environment: 
      - MYSQL_ROOT_PASSWORD=5ecr3t
      - MYSQL_DATABASE=prestashop
      - MYSQL_USER=prestashop
      - MYSQL_PASSWORD=5ecr3t

But the problem is still the same ...

PierreRambaud commented 3 years ago

Because you're using the wrong environement variable

version: "3"

services:
  shop:
    image: prestashop/prestashop:1.7
    depends_on: 
      - database
    environment: 
      - ADMIN_MAIL=contact@xpressive.io
      - ADMIN_PASSWORD=5ecr3t
      - DB_SERVER=database
      - DB_DATABASE=prestashop
      - DB_USER=prestashop
      - DB_PASSWD=5ecr3t
      - PS_COUNTRY=FR
      - PS_DEMO_MODE=1
      - PS_DEV_MODE=1
      - PS_DOMAIN=prestashop.xpressive.local
      - PS_INSTALL_AUTO=1
      - PS_LANGUAGE=fr
  database:
    image: mysql:5.7
    environment: 
      - MYSQL_ROOT_PASSWORD=5ecr3t
      - MYSQL_DATABASE=prestashop
      - MYSQL_USER=prestashop
      - MYSQL_PASSWORD=5ecr3t

Don't ask me why they choose PASSWD instead of PASSWORD :sweat:

maitre-hibou commented 3 years ago

Oh my 😅.... Thank you it works now ! Yeah that choice is very misleading, especially as it is spelled correctly on ADMIN_PASSWORD