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