Connection refused when running Laravel artisan command with Docker
Solution 1
First edit your docker-compose.yml
.
mysql:
image: mariadb:latest
ports:
- 8889:3306
After that set the correct DB port in .env
.
Your DB port is wrong. You are trying to connect the exposed port inside the docker. In this case you should use DB_PORT=3306
in your .env
.
Solution 2
this worked for me: put the name of my mysql container instead of 127.0.0.1
NAME CONTAINERS
project-db --> container mysql
project-app --> container laravel
DB_CONNECTION=mysql
DB_HOST=project-db
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=root
Solution 3
Try change port to 3306 and use DB_HOST=localhost to yourdomain.com(your IP)
And don't forget Sudo Clear cache and config cache
DB_HOST=My_ip_for_virtual_machine (yourdomain.com)
sudo docker-compose exec app php artisan config:clear
sudo docker-compose exec app php artisan cache:clear
Solution 4
After 5 hours researching I found this quote in comments:
Remove port-exposing, mariadb:latest sets it in its Dockerfile: This image exposes the standard MySQL port (3306)...
This mean you MUST use port 3306 in .env
of each Laravel projects, either you define port for Mysql(or mariaDB).
This mean in host only port 3306 connect your Laravel to database.
Comments
-
GluePear almost 2 years
I'm running Laravel 5.4 in Docker. This is my
docker-compose.yml
file:version: '2' services: app: container_name: laravel_app image: webdevops/php-apache-dev:ubuntu-16.04 links: - mysql depends_on: - mysql ports: - 8888:80 volumes: - .:/app environment: docker: 'true' WEB_DOCUMENT_ROOT: '/app/public' WEB_NO_CACHE_PATTERN: '\.(.*)$$' working_dir: '/app' mysql: image: mariadb:latest ports: - 8889:80 environment: MYSQL_ROOT_PASSWORD: 'dev' MYSQL_DATABASE: 'dev' MYSQL_USER: 'dev' MYSQL_PASSWORD: 'dev'
This is the relevant part of my
.env
file:DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=8889 DB_DATABASE=dev DB_USERNAME=dev DB_PASSWORD=dev
I am able to see the Laravel welcome page - that side of things works. But when I run
php artisan migrate
I get this error:SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = dev and table_name = migrations)
I have tried fiddling with the
host
andport
parameters in the.env
file. -
Kyslik almost 6 yearsThis is a typo question it should be removed, since OP failed to read hub.docker.com/_/mariadb.
-
GluePear almost 6 yearsIf that rule was universally applied, SO would be a third of its current size. I'm not saying that's necessarily a bad thing...
-
Dennis Schaffer almost 5 yearshow you can do that? we have the connection refused problem.
-
bstricks about 3 yearsThank you!!!!!!!!!!!!!!!! This worked for me. Opened up Docker and copied the name of my mysql container: my-ap_mysql_1 and put it as the DB_HOST.
-
Viet Nguyen about 3 yearsThat's weird, it works but cost me days of searching for solutions. Thanks for saving me more days of googling :D
-
Nick almost 3 yearsI would have never figured this out had I not seen this. Thank you!
-
Mike Harrison almost 3 yearsDo you know why this worked for you? It didn't work for me and I was wondering if there's any documentation
-
ITW almost 3 yearsthat's the issue we are facing! thanks for pointing it out!
-
FarhanShares almost 3 yearsI also found this one true for me while using mysql:latest. Despite setting a custom port, only 3306 works.
-
nik over 2 yearsThis may not be the actual solution of question which OP asked, but this is solution of one of the most common mistake