Alexandra Axt
Alexandra Axt

Reputation: 1346

Docker with Laravel 4.2, connecting to DB throws error: SQLSTATE[HY000] [2002] No such file or directory

I need to get an old version of a Laravel app working. It is using Laravel version 4.2.2.

I have a docker setup:

version: '3.5'

services:
  laravel:
    depends_on:
      - database
    ...

  database:
    image: mysql:5
    hostname: database
    environment:
      MYSQL_ROOT_PASSWORD: mypass
      MYSQL_DATABASE: mydb
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypass

I added database data to config (Laravel 4.2)

 'connections' => array (

            'mysql'  => array (
                'driver'    => 'mysql',
                'host'      => 'database', // as name of db container
                'database'  => 'mydb',  // this database exists, I can see in PhpMyAdmin
                'port'      => '3306',
                'username'  => 'root',  // I can login with these credentials in PhpMyAdmin and CLI
                'password'  => 'mypass',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),
...

I can login to a phpmyadmin container with these credentials, and I also can login from the CLI of laravel container with mysql -h database -u root -p.

If I dump the connection in Laravel Illuminate/Database/Connectors/Connector.php class, I see that the correct config is being used.

public function createConnection($dsn, array $config, array $options)
    {
        $username = array_get($config, 'username');

        $password = array_get($config, 'password');

        // dd($dsn);
        // dd($config);
        // dd($username); dd($password);


        return new PDO($dsn, $username, $password, $options);
    }

Gives:

Why do I still get the error?

Upvotes: 0

Views: 398

Answers (2)

SAMUEL
SAMUEL

Reputation: 8552

The possible reasons for SQLSTATE[HY000] [2002] is as follows

  1. MYSQL is not running

  2. Incorrect database settings

  3. Insufficient server resources

Based on the analysis of your scenario, the possible reasons is the incorrect database settings

Possible recommendation from my side is to find the right database settings in Laravel.

  • Make sure the host is correct
  • Make sure the username is correct
  • Make sure the password is correct

In most of the cases the problem lies on the host resolution in docker for that I recommend you to find the right hostname and port that the laravel container can able to connect with the mysql container

Following the steps described in this tutorial or verifying the steps with your existing system may solve your problem.

Upvotes: 0

Alexandra Axt
Alexandra Axt

Reputation: 1346

I found the problem. If you look at the $dsn-value in Illuminate/Database/Connectors/Connector.php createConnection:

string(59) "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=mydb"

You see that host=database is simply missing, even though I added it in laravel config.

Adding it manually solves this problem:

$dsn= "mysql:unix_socket=/var/run/mysqld/mysqld.sock;host=database;dbname=mydb";

Even though I added the host in the config, I found out that in another database-config there was a unix_socket-value added to mysql configuration. Laravel takes either the socket or the host information. My host-value has not been overriden, but the presence of unix_socket-value in the other configuration prevented connection via host.

So I simply removed the unix_socket in another config.

Upvotes: 1

Related Questions