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

TonsOfLaz's avatar

Homestead DB connection problem

Hi, first time posting here. My issue is this: I want to use Homestead. I follow the instructions religiously, create my migrations, set my variables exactly as described:

'mysql' => [
        'driver'    => 'mysql',
        'host'      => $_ENV['DB_HOST'],
        'database'  => $_ENV['DB_DATABASE'],
        'username'  => $_ENV['DB_USERNAME'],
        'password'  => $_ENV['DB_PASSWORD'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ],

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

..then I get the homestead box going with homestead up. Now I want to run my migrations and populate my database, so I run

php artisan migrate

...and i get:

exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Users/lazimac/Developer/forge/experiments/donkey/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47
Stack trace:
#0 /Users/lazimac/Developer/forge/experiments/donkey/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(47): PDO->__construct('mysql:host=loca...', 'homestead', 'secret', Array)

The actual error is much longer. So what's the problem? It has to do with the ports being forwarded on the homestead box. The normal "php artisan migrate" function being called from here apparently will not work. If I then change my host to

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
DB_HOST=localhost:33060
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret 

(Note the port added to the localhost), I can run my migration!

But then the application can't connect to the database, because it is running inside the virtual machine. So when I try to go to a page I get another PDO connection error.

So it seems that it's one or the other: either you configure your app so you can run commands from the normal terminal (by setting port 33060), or you can configure you app to access the DB from within.

My question, for all that long explanation, is how can I do both? I would like to use homestead but still be able to run artisan commands on the DB. Has anyone else figured this out?

Thanks!

0 likes
16 replies
bashy's avatar

use 192.168.10.10 for DB_HOST

DB_HOST=192.168.10.10
9 likes
TonsOfLaz's avatar

Hi bashy, thanks for the reply. Unfortunately using

DB_HOST=192.168.10.10

I now get the following error for both migrations and pages:

exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Connection refused' in /Users/lazimac/Developer/forge/experiments/donkey/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47
Stack trace:
#0 /Users/lazimac/Developer/forge/experiments/donkey/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(47): PDO->__construct('mysql:host=192....', 'homestead', 'secret', Array)
bashy's avatar

Where are you running php artisan migrate from?

1 like
TonsOfLaz's avatar

I have been running php artisan migrate from the initial place I ran things before the virtual homestead box was set up. I have seen that you can run the commands by sshing into the virtual machine, but I am looking for a way to continue running my migrations from the original app root.

Or I wonder if everybody using Homestead sshes into their homestead box to run migrations, because it can't be done from the normal app root?

bashy's avatar

I use vagrant and run from either if needed. It works for me using the VM IP (I use Vaprobash though)

bashy's avatar

Not sure why it wouldn't. Not sure how my.cnf is setup, maybe it doesn't allow connections from outside.

Cocoon's avatar

I stumbled upon this thread while looking for a solution for the same problem and finally added the following line to my database.php file, which solved the problem:

'mysql' => [
            // ...
            'host' => env('DB_HOST', 'localhost') . ('homestead' == gethostname() ? null : ':33060'),
            // ...
        ],
12 likes
ovvessem's avatar

I am also looking for this answer to use my local terminal app to make use of the php artisan migrate command with the understanding that I use homestead.

I have tried all of the above but still facing the error message that says I can not connect (via localhost and or 192.168.10.10)

My situation is as follows:

I have a local install of MAMP which provides my PHP. I have MAMP running with Apache and MySQL and my .env settings are:

APP_ENV=local
APP_DEBUG=true
APP_KEY=<somekey>

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file

The reason why I would like to perform the action from my local machine is because I would like to use one terminal instance for all the artisan commands and gulp watch which does not support the information message in the VM.

I hope someone in the community stumbled on the same situation and can clear out.

shovsepian's avatar

If you use PostgreSQL with Homestead then port number can be set like this:

    DB_HOST=localhost port=54320
    DB_DATABASE=db
    DB_USERNAME=homestead
    DB_PASSWORD=secret
timetraveller1992's avatar

I faced this same problem. My solution was to add this after the opening php tag in artisan

define('ARTISAN', true);

and then in database.php under my db configuration array for PORT I used

'port'      => defined('ARTISAN') ? '33060' : '3306',

Since I always run the artisan commands from outside homestead, this solution works perfectly well. It will cause problems when on production thou so it’s advisable to exclude the artisan file to be pushed to production. I use update hooks so I’ve added it to my gitignore. Works well!

1 like
elena's avatar

If I remember correctly you can setup a new connection and pass it on as an argument in the migrate command. Something like this:

'default' => 'mysql_vm',

'mysql_vm' => [
        'driver'    => 'mysql',
        'host'      => $_ENV['DB_VM'],
        'database'  => $_ENV['DB_DATABASE_VM'],
        'username'  => $_ENV['DB_USERNAME_VM'],
        'password'  => $_ENV['DB_PASSWORD_VM'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ],

'mysql_host' => array(
        'driver'    => 'mysql',
        'host'      => $_ENV['DB_HOST'],
        'database'  => $_ENV['DB_DATABASE_HOST'],
        'username'  => $_ENV['DB_USERNAME_HOST'],
        'password'  => $_ENV['DB_PASSWORD_HOST'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
),

HOST is your "real" machine, VM is obviously your virtual machine :)

Then run the migrations and any other commands like this:

php artisan migrate --database=mysql_host

It's a late and untested reply, but maybe someone finds useful.

benswinburne's avatar

The way that I do this is set variables in the homestead.yml file. PhpDotenv doesn't populate the values if they exist so you can set the environment variables on the Homestead VM and config

# homestead.yml

variables:
 - key: APP_ENV
   value: local
 - key: DB_HOST
   value: 127.0.0.1
 - key: DB_PORT
   value: 3306

# .env

DB_HOST=192.168.10.10 # Homestead VM IP
DB_PORT=33060         # Port which is forwarded to 3306 on the VM 

--

vagrant@homestead:~/Sites/example$ php artisan migrate
Nothing to migrate.

--

Ben in ~/Sites/example on develop$ php artisan migrate

Nothing to migrate.

This obviously makes the assumption that all sites running on Homestead are using MySQL (or some database on 3306) and all databases are on the Homestead VM, as opposed to on the host, or at a 3rd party, say AWS etc.

At the time of writing, homestead doesn't allow you to configure per site variables.

jerrycoe's avatar

@Cocoon original solved my problem.

@bashy answer was probably what you should do. In order for me to get it to work I had to use

DB_HOST=192.168.10.10
DB_PORT=3306

Be mindful NOT to use port 33060. Using 33060 will cause that conflict.

Please or to participate in this conversation.