Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chilll's avatar

SQLSTATE[HY000] [2002] Connection refused. Feature tests work, but API calls fail

On OSX using: Docker, Laravel, PHP, MySQL, Nginx & PhpMyAdmin

I can access my DB with PMA, Sequel Pro and even though my project Feature tests.

However, I can't access the same API endpoints with my Vue application, Postman or even GET-ing the lists directly from browser.

I have verified that all requests do go though the same controller, but Postman/GET requestes just fail on the first line of my index() method:

$books = Book::all();

I am new to Laravel but... at the moment, I do not know what can possibly be wrong?

I don't think I have any security measures in my code. Maybe Laravel does something fancy internally?

Thanks,

0 likes
1 reply
chilll's avatar

Found it!

The value for DB_HOST in env.ini has to be the mysql container_name from docker-compose.yml which Docker then translates to the proper network address.

However, that did break my unit/feature tests tho'. And to make these function properly again - I had to make a copy of .env with a name .env.testing And in that file I changed APP_ENV=testing and DB_HOST=127.0.0.1

Duh :)

1 like

Please or to participate in this conversation.