There are no less than 1,000 threads on the interwebs about Laravel Sail and getting the "SQLSTATE[HY000] [2002] Connection refused" error. I have tried just about every relevant suggestion and after spending about 45 hours on this, cannot get ANYWHERE with Sail/Docker.
I installed Sail on a pre-existing project using:
php artisan sail:install
php artisan sail:publish
In Ubuntu (running Windows WSL2): I’ve tried:
sail build --no-cache
sail up -d
But alas, still:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from…
At one point, I was able to access the mysql cli and verify there was a database with the correct name, but the HTTP requests just weren’t being accepted. However, after reinstalling sail a few times and changing environment variables, I can no longer do this either, as I get the mysql cli error:
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’
Here’s my .env:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=somedb
DB_USERNAME=someuser
DB_PASSWORD=somepass
Here’s my docker-compose:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
memcached:
image: 'memcached:alpine'
ports:
- '11211:11211'
networks:
- sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local
2
Answers
In file .env change
DB_HOST=mysql
toDB_HOST=host.docker.internal
just change image_name like:
image: "mysql:8.0" and change as DB_HOST=mysql
run sail up -d command it works