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

hansje's avatar

SQLSTATE[HY000] [2002] Connection refused

Im following the laravel from scratch series, i'm at the point where ive setup a db and try to show the information on my views. When I start the homestead server, everything works fine. I can access the data from the db etc.

After 1 minute the connection to the db seems to be failing and I get the connection refused error.

What i've tried:

  • setting the host to localhost (and back)
  • looking in the my.cnf file on the machine to verify the settings
  • Search this forum and the interwebs for people with the same problem.

Anyone that can help me out?

0 likes
14 replies
SaeedPrez's avatar

@hansje are you showing the whole error message?

setting the host to localhost (and back)

What is back ?

Also what port are you using in your .env file? Or even better, show the whole .env file

hansje's avatar

@SaeedPrez this is the full error message: QueryException in Connection.php line 647: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from tasks order by created_at desc)

with back I ment back to 127.0.0.1

APP_ENV=local APP_KEY=base64:SR+Oqr9UpaSDAfasfaAq5aYVfASpq6nKATd92MTM0A= APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=http://homestead.dev

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=homestead DB_PASSWORD=secret

BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379

MAIL_DRIVER=smtp MAIL_HOST=mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null

PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET=

ps. sorry I dont know how to format this on this forum correctly D;

SaeedPrez's avatar

Just formatting so we can see what's going on..

APP_ENV=local 
APP_KEY=base64:SR+Oqr9UpV6HDS6MtiRVq5aYVpq6pIZnKATd92MTM0A= 
APP_DEBUG=true 
APP_LOG_LEVEL=debug 
APP_URL=http://homestead.dev

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=blog 
DB_USERNAME=homestead 
DB_PASSWORD=secret

BROADCAST_DRIVER=log 
CACHE_DRIVER=file 
SESSION_DRIVER=file 
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1 
REDIS_PASSWORD=null 
REDIS_PORT=6379

MAIL_DRIVER=smtp 
MAIL_HOST=mailtrap.io 
MAIL_PORT=2525 
MAIL_USERNAME=null 
MAIL_PASSWORD=null 
MAIL_ENCRYPTION=null

PUSHER_APP_ID= 
PUSHER_APP_KEY= 
PUSHER_APP_SECRET=
SaeedPrez's avatar

@hansje everything looks fine to me,..

  • after you changed the 127.0.0.1 to localhost, did you run php artisan config:clear ?
SaeedPrez's avatar

I'm running out of ideas,.. are you running everything from inside Homestead? If not, try port 33060..

If you're on Windows, I suggest to use Laragon instead and save yourself a lot of future headaches and head banging.

hansje's avatar

@SaeedPrez I'm running on windows yea. I was afraid that might be the problem... Ran everyhing from inside Homestead.

SaeedPrez's avatar

@hansje I have no idea if Homstead on Windows is the root of the problem,.. I was using Homestead on Windows successfully for some time but still it took quite some time to get things working and it was not always the way I wanted it.

In my opinion, the benefits that Homestead offers are not worth the trouble for the normal/beginner user.

I switched to Laragon a few weeks back and it just works, it's small, portable, fast, easy to configure and it allows me to focus on coding.

1 like
Mittensoff's avatar

I suggest 'wasting' a day or two and figuring out how to setup a web server with PHP and MySQL manually without using Homestead and similar stuff.

hansje's avatar

@SaeedPrez i noticed that the homstead server stops running mysql service. Restarting doesnt work. Maybe my vm doesnt have enough memory to handle the service.

Gonna try and fix this. Will report back if I find something that helps me

1 like
hansje's avatar

Well I didnt really fixed it but I used vagrant destroy && vagrant up to reset everything and that worked.

amzadkhan's avatar

Please update database name, username and password in your .env file.

DB_DATABASE=laravel_db DB_USERNAME=root DB_PASSWORD=null

If password not given put 'null' in front of 'DB_PASSWORD='. Hope above suggestion will help.

Please or to participate in this conversation.