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

Quaap's avatar

Homestead MySQL connection error: SQLSTATE[HY000] [2002]

I had a working Homestead environment for a few weeks, but it seems that the mysql connection suddenly stopped working. This is the Laravel QueryException I get:

SQLSTATE[42S02]: Base table or view not found

It seems that Laravel can connect to MySQL and the Homestead database, but can't find the table. This part is correct, the table does indeed not exist, so I tried to use artisan migrate to create the different tables, with the following result:

SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

I can ssh into vagrant, connect to MySQL and show the different databases and tables.

I tried vagrant destroy -> vagrant up, even downloaded the homestead box again trying to create a fresh environment, but nothing seems to fix this problem.

My database.php configuration:

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost:33060',
            'database'  => 'homestead',
            'username'  => 'homestead',
            'password'  => 'secret',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

anyone have an idea or advice?

0 likes
37 replies
bashy's avatar

Best to use the IP so it's connectible from both remote CLI and your local machine

'host'      => '192.168.10.10',
3 likes
Quaap's avatar

@bashy : Thanks for the reply. I changed the host setting, but it doesn't fix the issue. I noticed that when I use vagrant up, I get a lot of red ==> default: stdin: is not a tty errors. I'm not sure if these are related.

bashy's avatar

Not related, what error do you get now or is it the same connection issue?

Quaap's avatar

@bashy : I still get the same connection error when trying migrating the database tables.

$ php artisan migrate

[PDOException]
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
bashy's avatar

Works on my normal Vagrant install but Homestead could be different. Try with the port (33060) on that IP.

You can always test the connection with

telnet 192.168.10.10 3306

Quaap's avatar

@bashy I can't use telnet from my windows git bash, so I had to install the windows telnet client first. But I can't connect to 192.168.10.10 using telnet (no clear reason). I also tried it with PuttY, but the connection was refused.

Could it be a VirtualBox-Host network problem? I'm all out of ideas :-/

bashy's avatar

Tried using different ports IPs?

127.0.0.1 33060

192.168.10.10 3306 and 33060?

Quaap's avatar

@bashy Thanks for your patience!

Yes I tried telnet using different IP's and ports, no luck.

I can ping 192.168.10.10, so the connection looks okay. Laravel can connect to the Database, so that also not seems to be the problem. When I ssh into the VM, I can connect to MySQL using the console.

Isn't Artisan using the same connection as Laravel to connect to MySQL? (Sorry still a newby to Laravel)

bashy's avatar

Yes it will use the same connection, although it depends what the environment is. local/production will use different files.

isaackearl's avatar

For me laravel uses the regular port to access the db (3306). I only have to use 33060 when I access homestead database remotely via mysql workbench or something like that.

Quaap's avatar

When I ssh into the VM, artisan migration works fine. It's a workaround for now, but it's not how it should be, or how it was before. The environment is running as local. I checked the local DB settings, they seem okay.

rleger's avatar

I like to run things outside the vm as its much faster (beaht, migrations...) I'm trying to be able to run commands using the db inside and outside the vm without changing host every time I go from one to the other.

I'm pulling my hairs out with this..

DB_HOST=127.0.0.1  works outside the vm (and not in !)
DB_HOST=localhost works inside the vm (and not out !)

I've also added DB_PORT=33060 and defaulted to 3306 on database config file

'port'      => env('DB_PORT', 3306),

Does anyone has a clue on how to work around that ? It seems like a recurring problem, I found many topics on the subject but no solution.

1 like
jrean's avatar

@rleger do you have the same problem using 192.168.10.10 instead of localhost and/or 127.0.0.1 ?

bashy's avatar

You tried telnet? It's much easier to check the connection rather than changing the port and checking via your app.

Try these combinations

192.168.10.10:3306
192.168.10.10:33060 // shouldn't work but could of been binded externally on the local port
127.0.0.1:3306 // shouldn't work but could of been binded locally
127.0.0.1:33060
localhost:3306 // shouldn't work but could of been binded locally
localhost:33060
rleger's avatar

I tested the addresses and port. Only 127.0.0.1:33060 and localhost:33060 worked

don't know what to make of it though Just for info I do use 192.168.10.10 to "map" the domains in my hosts file.

Thanks !

bashy's avatar

weird that the MySQL port doesn't forward... have you tested on telnet or similar?

rleger's avatar

Yes I've used "telnet ip_address port" on the terminal

rleger's avatar

I don't understand what I could possibly have done wrong. The hosts file is rather simple to get right.. I've followed homestead install steps.. weird.

I had a similar problem at some point with an earlier version of L5 but it was gone...

Thanks for your help anyway

bashy's avatar

There's loads of things to check, just hard since I'm not debugging it myself :P

If you login to your VM via SSH and run either of these, you'll be able to see if MySQL is binding to certain ports.

// Show all
sudo netstat -plnt

// To only show 3306
sudo netstat -plnt | grep ':3306'
enantiomer1's avatar

I'm having the same issue; I was really starting to like Homestead until this issue came up. Hope it's just a wrinkle with L5 roll-out. and the .env. Went back to using the PHP server + WAMP for now.

tomhorvat's avatar

I have the same problem since L5! In L4 running artisan migrate through main machine worked!

So far I figured out that artisan connects from local machine to homestead(vagrant) through port 3306 but should through 33060.

So, to address this issue, i created a new connection in config/database.php called "local", identical to "mysql" but I changed host to:

'host'      => 'localhost:33060'

and am using:

php artisan migrate --database=local

so artisan uses correct port...

It's not a problem to ssh into VM but it's slower on my machine...

njmata's avatar

Hi, I'm new using Laravel, I just installed yesterday.

I found this exact problem and I could resolved using the next configuration:


'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', '127.0.0.1'),
            '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,
        ],
I hope that can fix the problem for other users.
2 likes
RomainLanz's avatar

Why do you specify the port?

You can just set 'host' => 'localhost' and it's working. Your database is inside homestead and your Laravel's app too. You can't migrate outside the VM, because "localhost" is your computer and not the VM in this context.

If you want to access to your database with a software like HeidiSQL or SequelPRO you need to set the port to 33060.

1 like
willvincent's avatar

localhost on port 33060 should forward to the VM on port 3306 though.

Lots of hand waving going on in this thread. Cutting through the confusion, the underlying problem here is that people are trying to run migrations from OUTSIDE the VM, correct? That should be doable, but you would have to have different environment settings inside the vm vs outside the vm, so that when outside the DB host and port is pointed at the proper place, and when inside it's pointed at 'localhost' and the default port '3306'

1 like
sitesense's avatar

Is there some confusion here?

Unless you've actively set up some different configuration, if you're using Homestead, the database is hosted in the VM. If you connect locally (in the scope of the VM) to the database, the port is the standard 3306. When you connect to the database from your local machine (your local OS) to the VM, then you use port 33060.

Any migrations must be run from the VM via SSH. There is no local database (unless you did something out of the ordinary).

1 like
bashy's avatar

My way only works if you allow remote access to the MySQL server.

I run mine from my host machine (not the VM but I can do either). I don't have it binded to 127.0.0.1 so I can access it via the IP.

Next

Please or to participate in this conversation.