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

zaster's avatar

SQLSTATE[HY000] [2002] Connection refused

I am getting this error

SQLSTATE[HY000] [2002] Connection refused

database.php


        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', 33060),
            'database'  => env('DB_DATABASE', 'homestead'),
            'username'  => env('DB_USERNAME', 'homestead'),
            'password'  => env('DB_PASSWORD', 'secret'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=J6cc5Vpd1q5F76VGcCfMY3rx8dEeCOfE

DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

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

Homestead.yaml

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: D:\Project\PHP
      to: /home/vagrant/Code

sites:
    - map: laravel.dev
      to: /home/vagrant/Code/Laravel/public
    - map: quickstart.dev
      to: /home/vagrant/Code/quickstart/public

databases:
    - homestead

variables:
    - key: APP_ENV
      value: local
    - key: DB_PORT
      value: 33060

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 93000
#       to: 9300
#     - send: 7777
#       to: 777
#       protocol: udp
0 likes
10 replies
vtalbot's avatar
vtalbot
Best Answer
Level 26

The port should be 3306 and you should let the hostname to localhost.

2 likes
delmas's avatar

@vtalbot thanks it helps me fix a part of my database connection on server!

zaster's avatar

Worked like magic. I just changed the below files as you instructed

database.php
.env
Homestead.yaml

and problem solved THANK YOU VERY MUCH vtalbot your 13 words did the trick

zaster's avatar

@vtalbot later on i realized that i am unable to run

php artisan migrate

to run the above command it is required to change the port to 33060

zaster's avatar

@vtalbot

The port should be 33060 and you should let the hostname to localhost.

It seems like this is the perfect answer

vtalbot's avatar

It is recommended to run the command inside the homestead instead of the local machine.

fahadkhan1740's avatar

I could be because you might have not restarted PHP artisan since long

So After making DB changes and config:clear Tinker works fine

But to make browser refect the new DB connection you need to re-run

php artisan serve

knightLaravel's avatar

I got the same issue when following this tutorial: laracasts: series laravel-6-from-scratch episodes 9 at 4:45 (was not allowed to have link first day)

I changed the code to (Didn´t work with . first in Laravel8): $post = \DB::table('posts')->first();

I use the laradock instead of homestead. Since it looked more complicated then a docker solution. But for me this solution didn´t work. Not sure if its Laravel 8 or laradock that is the issue. Any ideas?

Please or to participate in this conversation.