I have Laravel application with MySQL and I'm trying to run it in Docker using docker-compose. But when app trying to connect DB, it throws:
PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
My docker-compose.yml:
load_balancer:
image: tutum/haproxy
links:
- web
ports:
- "80:80"
cache:
image: redis
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: Q1w2e3r4t5
MYSQL_DATABASE: regappbase
web:
image: andrewmclagan/nginx-hhvm
links:
- db
- cache
volumes:
- ./:/var/www
environment:
- APP_ENV=local
- DB_DATABASE=regappbase
- DB_PASSWORD=Q1w2e3r4t5
- VIRTUAL_HOST=laravel.local
My .env:
APP_ENV=production
APP_DEBUG=true
APP_KEY=SomeRandomString
APP_URL=http://laravel.local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=regappbase
DB_USERNAME=root
DB_PASSWORD=Q1w2e3r4t5
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=laravel.local
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
Mysql connections to "localhost" by default attempt to connect over a socket present on the local machine. Your DB_HOST should be the linked container name, "db".
From the MYSQL docs:
You can change DB_HOST localhost to ip in .env. To receive ip mysql docker container need write command: