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

Archiemoses's avatar

Laravel/Homestead MySQL access denied

Brand new to Laravel and trying to set up homestead. Long and short of it:

php artisan migrate

Leads to

exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)'

SSH in an mysql -u homestead -p works just fine?

See many many instances of this question; Changing host to 127.0.0.1:33060 makes no difference. Putting credentials directly into database.php rather than .env makes no difference.

database.php

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

.env

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
0 likes
14 replies
Archiemoses's avatar

And I feel stupid.

Artisan migrate only works via SSH to vagrant/code.

1 like
michaeldyrynda's avatar

Yep, if you're accessing the MySQL server on localhost, you have to run artisan from localhost.

bashy's avatar

Better to use the full IP and normal port. Then you can use it on both host machine and VM.

tomhorvat's avatar

I tried that and didn't work for me... Only localhost:33060 when connecting via main machine. Port 3306 on VM.

tomhorvat's avatar

I'm not expert in vagrant/homestead but all ports are forwarded during vagrant up. In L4 all worked...

screen

bashy's avatar

Strange, port isn't forwarded properly or MySQL doesn't allow remote connections. I use Vaprobash so not sure about Homestead.

grusch-it's avatar

Homesteads MySQL is bound to the guests NAT interface (10.0.2.15) to which you can't connect from the host.

The only way to get connected to MySQL from outside the guest is using the forwarded port 33060 (127.0.0.1:33060).

Currently its a bit tricky to get commands using MySQL (e.g. migration) working on both host and homestead/guest. My setup:

$ homestead edit

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

.env file (APP_ENV=dev is optional and could be left to APP_ENV=local):

APP_ENV=dev
[...]
DB_HOST=127.0.0.1
DB_PORT=33060
[...]

Add 'port' => env('DB_PORT', 3306), to config/database.php:

    'connections' => [
        # [...]
        'mysql' => [
            # [...]
            'port'      => env('DB_PORT', 3306),
            # [...]
        ],
        # [...]
    ],

And that's it. Now you can run

$ php artisan migrate

either on the host or on homestead directly.

2 likes
bashy's avatar

Yeah I always make sure it's not bound to 127.0.0.1 in my.cnf for MySQL.

webkenny's avatar

Old thread but why not in case someone else is looking for this... I was thinking out loud here and realized one could easily alias the artisan command on your local host to first tunnel to the vagrant VM before executing. I'm going to try to make that work and I'll post back a gist if I do.

webkenny's avatar

Ok, here it is. So simple in case anyone wants to do this. The following function placed in your .bash_profile or similar file on *nix will allow you to do the following:

  1. SSH to the VM
  2. Change into the directory where your site lives
  3. Run an artisan command.

I named mine artisan but you can name it whatever you like. This also assumes you have setup an alias in your ~/.ssh/config file but that isn't strictly necessary. This will also work simply passing in the username and IP. e.g. ssh vagrant@192.168.10.10

Change your path if you are using something other than the default of /home/vagrant/Code.

#######################################
# Run artisan on the Homestead VM
#
# Arguments:
#   Folder
#   Command to Run
# Returns:
#   Output of the Artisan Command
#######################################
function artisan() {
    ssh homestead cd /home/vagrant/Code/$1 && php artisan $2
}

Example usage if I want to run a list command inside the site called myapp:

artisan myapp list

Hope this helps someone!

tesark's avatar

This steps working try it,

  1. .env file DB_HOST=localhost DB_DATABASE=mydbname DB_USERNAME=root DB_PASSWORD=''
  2. php artisan config:clear
  3. php artisan migrate. working fine in my system.

Please or to participate in this conversation.