Rreit
Rreit

Reputation: 75

WSL windows laravel SQLSTATE[HY000] [2002]

I recently started a laravel(8) project on my windows machine using WSL (Ubuntu 20.4) and docker. When reading data from the database I get the error 'SQLSTATE[HY000] [2002]No such file or directory' for DB_HOST=localhost or 'SQLSTATE[HY000] [2002] Connection refused' for DB_HOST=127.0.0.1. However, this problem appears only when I start the whole project using ./vendor/bin/sail up, when I boot the same project using php artisan serve the error does not appear for either DB_HOST configuration.

index function to show what is in DB

public function index()
{
        dd(Customer::all());
}

Customer object

class Customer extends Model
{
    use HasFactory;

    protected $table = 'customers';

    protected $primaryKey = 'id';

}

.env file

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:WtHehq3ZteXPe6xEIij2TBblW7Qo3VUd5obTao3Kg4U=
APP_DEBUG=true
APP_URL=http://thc.test

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=thc
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=memcached

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:7700

I already tried the solution with setting unix_socket to '/Applications/MAMP/tmp/mysql/mysql.sock' and also to the result of show variables like '%sock%'; of the 'socket' entry. I tried setting this value in both the '.env' file and in 'database.php'.

commands

php artisan key:generate
php artisan cache:clear
php artisan route:clear
php artisan config:clear 
php artisan view:clear 

also failed me

Upvotes: 2

Views: 1291

Answers (1)

matiaslauriti
matiaslauriti

Reputation: 8082

If you are using docker, you don't have to use any host as localhost, but rather use the service name as the host, for example, if you docker-compose.yaml is like this:

version: '3.9'

services:

  superNiceDb:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    environment:
        MYSQL_DATABASE: 'laravel'
        MYSQL_ROOT_PASSWORD: '123'
    volumes:
      - ./docker/mysql_data:/var/lib/mysql
      - ./docker/mysql:/docker-entrypoint-initdb.d
    ports:
      - ${DB_PORT:-3306}:3306

You have to use DB_HOST=superNiceDb in your .env or wherever you are reading that value from.

This applies to any system, if you are using docker, this is the only way of referring to services, never use localhost.

Upvotes: 1

Related Questions