Reputation: 85
I am trying to create a new item via postman to my laravel app ran in docker. The docker containers are auto-generated via ddev which provides a development environment and ran using WSL2 Ubuntu 18.04. So here is the catch, I can "php artisan migrate" just fine I can even "php artisan tinker" and create one just fine but when sending request via postman from my host machine I get this error.
Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (SQL: select count(*) as aggregate from
users
whereid
= 1) in file /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
I had solved this issue yesterday with this https://www.craigforrester.com/posts/windows-subsystem-for-linux-disable-ipv6-for-apt/ but tried it when I got this error again today and no luck.
Things I've Tried:
What I Think It Is: I am 99% certain this is not a credentials issue and more so a routing issue, cause like I said in the first paragraph I can migrate and create from the WSL Ubuntu. I think the routing issue is either from my host to the WSL or from WSL to mysql. But then again my request seems to be going through otherwise I wouldve got some kind of PHP connection error and not a MySQL connection error
Upvotes: 3
Views: 4038
Reputation: 1027
I had this issue a while back it turned out that I had to set DB_HOST to the name of the docker mysql container:-
docker-compose.yml:-
version: '3.1'
services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
image: larastock
ports:
- 8000:80
restart: always
volumes:
- .:/var/www/html
networks:
- larastock
mysql:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: larastock
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
networks:
- larastock
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- larastock
networks:
larastock:
volumes:
db_data:
.env:-
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=larastock
DB_USERNAME=root
DB_PASSWORD=password
Upvotes: 2