Using Docker and Laravel I have this error with MySQL: "SQLSTATE [HY000] [2002] No such file or directory"

1

I have an app in laravel 5.2 that connects to my mysql local database, without problems. But, when placing the app inside a docker container, it can not connect to the database and I get this error:

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

If I change the DB_HOST constant in the .env file to 127.0.0.1 instead of localhost, then I get this one:

SQLSTATE[HY000] [2002] Connection refused

I read that the problem could be due to not having a default route for the socket in the php.ini

$ ls -l /var/run/mysqld/mysqld.sock
$ srwxrwxrwx 1 mysql mysql 0 nov 27 08:31 /var/run/mysqld/mysqld.sock

So I added it only in the case of the pdo:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Part of my .env file

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=developer
DB_PASSWORD=123456

My database.php

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

Use docker-compose to mount the container, as I plan to create other microservices later:

version: "3.3"
services:
   api_test:
      build: .
      ports:
         - "8181:8181"
      env_file:
         - ./.env
      environment:
         - PORT:8181

My Dockerfile:

FROM php:7.1.5-apache

RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql mbstring
WORKDIR /api_test
COPY . /api_test
RUN composer install
RUN a2enmod rewrite
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181

Usage:

  • Debian 9.6, kernel 4.9.0-8-amd64

  • Apache / 2.4.25

  • PHP 7.0.30-0 + deb9u1

  • MariaDB 10.1.37 database server

Any suggestions?

    
asked by PulidoVPE 29.11.2018 в 20:10
source

1 answer

1

I found the solution. Or a solution that serves me. I found them here and here

The problem was remote access in my local mysql database. My container's network was different from my localhost's. And I did not have my mysql configured to receive external connections.

In the my.cnf

[mysqld]
bind-address = 0.0.0.0

And with the permissions

mysql> grant all privileges on *.* to 'developer'@'%' identified by '123456';
mysql> flush privileges;

EDITED

I forgot to add that changing my DB_HOST for my local IP (from the IP assigned by my router) to my .env file was also necessary.

    
answered by 03.12.2018 в 14:52