Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DukeCastino's avatar

Docker + Laravel = SQLSTATE[HY000] [2002] Connection refused

Hi, i have a problem with mysql and my Laravel Project. I use the Docker Droplet from Digital Ocean and take the steps from this Tutorial: Laracasts

Installation works fine but when i run php artisan migrate it ends with this Error:

  [PDOException]                             
  SQLSTATE[HY000] [2002] Connection refused

when i change the DB_HOST from 127.0.0.1 to localhost in .env File I got this Error:

  [PDOException]                                    
  SQLSTATE[HY000] [2002] No such file or directory

.env:

AP_ENV=local
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_KEY=base64:S0FqsS6t7z0jXHieFiRGE2Omj/lgPs/cdG/f9vE/hGQ=
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_PREFIX=jf_

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Database.php:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'homestead'),
            'username' => env('DB_USERNAME', 'homestead'),
            'password' => env('DB_PASSWORD', 'secret'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => env('DB_PREFIX', ''),
            'strict' => false,
            'engine' => null,
        ],

docker-compose.yml:

### MySQL Container #########################################

    mysql:
        build: 
            context: ./mysql
            args:
                - MYSQL_DATABASE=homestead
                - MYSQL_USER=homestead
                - MYSQL_PASSWORD=secret
                - MYSQL_ROOT_PASSWORD=root
        volumes:
            - mysql:/var/lib/mysql
        ports:
            - "3306:3306"

log file from mysql Docker Container:

2016-11-21 23:20:26 1 [Note] Server hostname (bind-address): '*'; port: 3306
2016-11-21 23:20:26 1 [Note] IPv6 is available.
2016-11-21 23:20:26 1 [Note]   - '::' resolves to '::';
2016-11-21 23:20:26 1 [Note] Server socket created on IP: '::'.
2016-11-21 23:20:26 1 [Warning] 'user' entry 'root@xxxxxxxxxxx' ignored in --skip-name-resolve mode.
2016-11-21 23:20:26 1 [Warning] 'user' entry '@ xxxxxxxxxxx' ignored in --skip-name-resolve mode.
2016-11-21 23:20:26 1 [Warning] 'proxies_priv' entry '@ root@ xxxxxxxxxxx' ignored in --skip-name-resolve mode.
2016-11-21 23:20:26 1 [Note] Event Scheduler: Loaded 0 events
2016-11-21 23:20:26 1 [Note] Execution of init_file '/etc/mysql/startup' started.
ERROR: 1396  Operation DROP USER failed for 'homestead'@'%'
2016-11-21 23:20:26 1 [Note] Execution of init_file '/etc/mysql/startup' ended.
2016-11-21 23:20:26 1 [Note] mysqld: ready for connections.
Version: '5.6.34'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

Can anybody help me? I´m frustrated :(

0 likes
24 replies
Waldemar's avatar
DB_HOST=127.0.0.1

Change 127.0.0.1 to your IP host vitrual

For example

DB_HOST=192.168.56.10
7 likes
DukeCastino's avatar

@Daemonik sorry no

@Ishatanjeeb: i checked it several times :(

docker-compose build mysql

Building mysql
Step 1 : FROM mysql:5.6
 ---> 56e0e4e9ce5a
Step 2 : MAINTAINER Mahmoud Zalt <[email protected]>
 ---> Using cache
 ---> b68760d7d55d
Step 3 : ADD startup /etc/mysql/startup
 ---> Using cache
 ---> 3e594adb69e7
Step 4 : RUN chown -R mysql:root /var/lib/mysql/
 ---> Using cache
 ---> 126a33bb9ce6
Step 5 : ARG MYSQL_DATABASE=homestead
 ---> Using cache
 ---> 15e09784cf35
Step 6 : ARG MYSQL_USER=homestead
 ---> Using cache
 ---> 76874e3cc8ce
Step 7 : ARG MYSQL_PASSWORD=secret
 ---> Using cache
 ---> 1f231ab4af02
Step 8 : ARG MYSQL_ROOT_PASSWORD=root
 ---> Using cache
 ---> 0b921f3d8f17
Step 9 : ENV MYSQL_DATABASE $MYSQL_DATABASE
 ---> Using cache
 ---> eeddaae5c88c
Step 10 : ENV MYSQL_USER $MYSQL_USER
 ---> Using cache
 ---> e075184aa701
Step 11 : ENV MYSQL_PASSWORD $MYSQL_PASSWORD
 ---> Using cache
 ---> d78feebeb745
Step 12 : ENV MYSQL_ROOT_PASSWORD $MYSQL_ROOT_PASSWORD
 ---> Using cache
 ---> 4415d498dee9
Step 13 : RUN sed -i 's/MYSQL_DATABASE/'$MYSQL_DATABASE'/g' /etc/mysql/startup &&     sed -i 's/MYSQL_USER/'$MYSQL_USER'/g' /etc/mysql/startup &&     sed -i 's/MYSQL_PASSWORD/'$MYSQL_PASSWORD'/g' /etc/mysql/startup
 ---> Using cache
 ---> d78620c465f1
Step 14 : ADD my.cnf /etc/mysql/conf.d/my.cnf
 ---> Using cache
 ---> b0adaa91e1e0
Step 15 : CMD mysqld --init-file=/etc/mysql/startup
 ---> Using cache
 ---> 6e395f7d260a
Step 16 : EXPOSE 3306
 ---> Using cache
 ---> f898311c2975
Successfully built f898311c2975
Waldemar's avatar

@DukeCastino i have this problem too but when i change work is fine

DB_HOST=My_ip_for_virtual_machine

Try change port to 33060 and use DB_HOST=192.168.56.10(your ip)

And dont forgot sudo nano /etc/hosts (your PC not virtual)

192.168.56.10 yourdomain.com

And restart virtual

You can write me in skype i help you Skype([email protected])

1 like
pmall's avatar

With docker compose having a service named mysql, then you should be able to put mysql as hostname in your .env file.

10 likes
Narco's avatar

Are you logged in your docker environment through ssh and then trying to run php artisan migrate?

docker-compose exec workspace bash

1 like
DukeCastino's avatar

@Daemonik Do you use Vagrant with Virtualbox? That was my first develop machine and it works perfect. Now i looked for a productive Server an i landed at DigitalOcean with Docker Droplet. i think that works different to Vagrant. Change the Port to 33060 doesn´t work

@pmall when i added mysql as DB_HOST i run into this error:

SQLSTATE[HY000] [1130] Host '172.18.0.3' is not allowed to connect to this MySQL server

this IP is my Workspace Container IP

@Narco i run this command in the workspace Container

pmall's avatar
SQLSTATE[HY000] [1130] Host '172.18.0.3' is not allowed to connect to this MySQL server

Thats better. The mysql server is found but dont allow the connection.

DukeCastino's avatar

the solutions was that i remove the mysql database, add a new one and create a new user "homestead"

thanks all !

Sib's avatar

Use container name of mysql in DB_HOST. Like this DB_HOST=laravellocal_mysql_1

7 likes
oyepez003's avatar

Yes, you should use the container name as the host of the database.

And remember run docker-compose exec your-container php artisan config:cache

1 like
digitlimit's avatar

I had this challenge because I am running 3 different containers with different IP Addresses

db - 172.18.0.3 - container for MYSQL
app - 172.18.0.2 - container for Laravel app

so I fixed it by editing my Laravel .env file

DB_CONNECTION=mysql
DB_HOST=172.18.0.3
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
...

To get your containers Ip addresses. From your docker host

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
8 likes
sguzikov's avatar

Had the same issue. This helps. Thanks a lot!

mpak's avatar

Hi everyone! I have the same problem, but with Laravel 6. At 5.8 all works fine, but with same settings Laravel 6 doesn't connect to DB. I tried different settings, but without any result. I can connecting to DB through Sequel Pro or from mysql container. I use Nginx + MySQL 5.7 + php-fpm 7.3.3. This is my Dockerfile:

RUN apt-get update && apt-get install -y --no-install-recommends \
        software-properties-common \
        apt-utils \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libjpeg-dev \
        libpng-dev \
        libpq-dev \
        libicu-dev \
        libzip-dev \
        zip \
        libmosquitto-dev \
        git \
        build-essential \
        apt-transport-https \
        curl \
        ca-certificates \
        gnupg2 \
    && docker-php-ext-configure gd \
        --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install \
        intl \
        opcache \
        pgsql \
        pdo \
        pdo_mysql \
        mysqli \
        pdo_pgsql \
        gd \
        mbstring \
        zip \
    && docker-php-ext-enable \
        intl \
        opcache \
        mysqli \
        pdo \
        pdo_mysql \
        gd \
        mbstring \
        zip \
    && pecl install -o -f redis \
    && docker-php-ext-enable redis \
    && apt-get install -y procps

and docker-compose.yml

version: '3.3'
services:
    nginx:
        image: nginx:latest
        container_name: nginx
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./nginx/core:/etc/nginx/conf.d
            - ./../htdocs:/var/www/
            - ./nginx/logs:/var/log/nginx/
            - ./nginx/html:/usr/share/nginx/html/
        environment:
            APP_DIR: ${APP_DIR}
            DOLLAR: ${DOLLAR}
            DOMAIN: ${DOMAIN}
            PHP_PORT: ${PHP_PORT}
        links:
            - php
        depends_on:
            - php
            - mysql
    mysql:
        image: mysql:5.7
        container_name: mysql
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=root
        volumes:
            - ./mysql:/var/lib/mysql
    php:
        build:
            context: ./php
            args:
                - CURRENT_UID=${CURRENT_UID}
                - CURRENT_GID=${CURRENT_GID}
                - APP_DIR=${APP_DIR}

        container_name: php-fpm
        user: '${CURRENT_UID}:${CURRENT_GID}'
        environment:
            TIMEZONE: ${TIMEZONE}
            PHP_MEMORY_LIMIT: ${PHP_MEMORY_LIMIT}
        volumes:
            - ./../htdocs:/var/www

        links:
            - mysql
        depends_on:
            - mysql

and .env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel6
DB_USERNAME=root
DB_PASSWORD=root

May be someone can help me with this problem...

1 like
sujancse's avatar
php artisan config:clear
php artisan cache:clear

Clear your database volume and try again docker-compose down -v

And run your migration command inside php container like

docker-compose exec php php artisan migrate:fresh
1 like
mpak's avatar

@sujancse Thanks a lot!!!! I clear docker containers

docker-compose down -v

after that added to Dockerfile php extension bcmath, build new containers

docker-compose up --build -d

clear config and cache

php artisan config:clear
php artisan cache:clear

and it's started work!

aamiroon-dev's avatar

The mysql connection settings in the docker-compose.yaml file should look like this:

  mysql:  // choose any name you prefer
        image: mysql:8.0 // choose any tag you need
        ports:
            - 3306:3306
        environment:
            - MYSQL_DATABASE=${DB_DATABASE}  // from .env file
            - MYSQL_USER=${DB_USERNAME}  // from .env file
            - MYSQL_PASSWORD=${DB_PASSWORD}  // from .env file
            - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}  // from .env file
        volumes:
            - db-data:/var/lib/mysql

For the database host, you should type: DB_HOST=mysql which is in my case the name I chose in the docker-compose.yaml file.

1 like
Borovez's avatar

The answer for me was to change the DB_HOST=localhost to DB_HOST=127.0.0.1 in my .env file.

JohnnyBigodes's avatar

The best answer would be to change the DB_HOST in the env. to the name of the service in the docker-compose.yml.

With other words change it to DB_HOST=mysql because you cant handle the connection in a docker container easily through the IP-Address.

3 likes

Please or to participate in this conversation.