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

Posted 2 years ago by DukeCastino

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 '[email protected]' 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 '@ [email protected] 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 :(

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.