Jump to content

Link to database cannot be established: SQLSTATE[HY000] [2002] Connection refused


Knajcior

Recommended Posts

Hello,

Im trying to setup prestashop with mysql database using docker-compose on CENTOS74 host.

Im new both to prestashop and docker.

My docker-compose.yml file is as follows:

version: '2'

services:
    mysql:
        container_name: SQL
        image: mysql:5.7
        ports:
            - "3306:3306"
        volumes:
            - /path_to_backed_database/prestashop.sql:/docker-entrypoint-initdb.d/init.sql
        environment:
            MYSQL_DATABASE: prestashop
            MYSQL_ROOT_PASSWORD: admin
        restart: always
    apache:
        container_name: prestashop
        image: 'prestashop/prestashop:1.7.4.3'
        links:
            - mysql
        environment:
            DB_SERVER: mysql
        ports:
            - "80:80"
        volumes:
            - /path_to_my_prestashop_sources/prestashop:/var/www/html:rw,delegated
        restart: always

 

So after containers get up, i get 500ed while connecting to server with the following output:

Link to database cannot be established: SQLSTATE[HY000] [2002] Connection refused

 

However when i try to log in to the container using  "docker exec -it containerID /bin/bash"  I can execute mysql commands with no problems. The database is there and has successfully imported data from /path_to_backed_database/prestashop.sql at boot.

My prestashop sources are from a local server i was testing it recently. I thought maybe something is wrong with them so I tried to run containers without specifying volume for prestashop.

It got me to the prestashop installation GUI and it cannot connect to database either (which again, while checking manually is fine in the container). At the setting up database screen after typing the credentials i get :

Database Server is not found. Please verify the login, password and server fields (DbPDO)

 

I honestly doubt that this is about wrong password because  according to https://hub.docker.com/r/prestashop/prestashop/ my configuration seems fine (admin is the default password).

Is there a difficulty in connecting between the containers? Is it about some php version mismatch? I have tried multiple solutions in the past 24h but cannot figure this out. Thank you for your help :)

Link to comment
Share on other sites

I know absolutely nothing about docker - but i do have one thought. Are both systems on the same IP address? We use store manager to directly access our sql database and we have to allow our IP address through. In Cpanel we have to add the IP address in Remote MySql

 

So that is quite possibly way off the mark, but you never know

Link to comment
Share on other sites

7 hours ago, haylau said:

I know absolutely nothing about docker - but i do have one thought. Are both systems on the same IP address? We use store manager to directly access our sql database and we have to allow our IP address through. In Cpanel we have to add the IP address in Remote MySql

 

So that is quite possibly way off the mark, but you never know

 

Both containers are hosted from the same server and from what ive read docker creates a bridge to which docker deamon connects containers by default.

In the yml file that ive mentioned you have port mappings specified. I can connect to the prestashop server on IP_OF_HOST:80 and to the databse on IP_OF_HOST:3306 (both are not on the host but on the containers that are forwarding their exposed ports to the host).

The problem is the fact that it seems that the prestashop container cannot communicate with the mysql container.

 

Link to comment
Share on other sites

I found the configuration of the internal docker network. You can either use that (ip adress of the container in that local network) as well as the container name (in my case host adress: SQL was valid).

 

It works now, I hope this helps someone in the future :)

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...