Laravel Homestead Vagrant Box Database Problems

11,497

Solution 1

This is what I answered in the Laracasts forums, in case it helps:

Inside the VM the sql port is 3306. Outside of the VM the host machine just has a forward to the SQL port on the VM. That is why 33060 points to 3306.

Unfortunately that is why you can't use the same database stanza for both.

Two ideas come to mind:

  1. Change the sql port from 33060 to be 3306 also on the host inside the homestead.rb file. I know machines get picky if you choose something under port 10000 so you might get prompted for admin credentials (if it even lets you). As long as you are not running something on that port it "should" work.

  2. You might consider setting up two Laravel environments for when working outside of the VM and one for inside. That way, you can override the database.php settings for when running artisan commands on the VM or if running artisan on the Host. In reality, you only care about changing the port number since all other settings would be identical. You can leave everything else as it is.

Just something to try. I just leave a SSH session open to the VM and run commands there since connecting to it is pretty fast after resuming the machine.

Solution 2

You have to execute the migration command from inside the VM. First, you log into the VM:

ssh [email protected] -p 2222

After that, you just cd into your project's folder and run the migration

cd Code/blog
php artisan migrate

The reason why this happens is because localhost:3306 from the perspective of your machine is one thing, from inside the VM is another.

Solution 3

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.

Solution 4

In homestead, MySQL is configured to bind to the VM's 10.x.x.x address, and does not bind to 127.0.0.1

You can change this by editing /etc/mysql/my.cnf and adding the line:

bind-address = 0.0.0.0

This will allow you to use 127.0.0.1 in your database configuration for both web and CLI use of the database.

Share:
11,497
MCG
Author by

MCG

Updated on June 26, 2022

Comments

  • MCG
    MCG almost 2 years

    I can't use the same database.php settings when browsing the local website in a browser (example.app:8000) and when using php artisan migrate.

    If my database.php settings are:

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

    Artisan works but I get this when browsing the site:

    SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111) (View: /home/vagrant/Code/playnamics/app/views/hello.blade.php)

    If my database.php settingd are:

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

    Browsing the site works but artisan give this error:

    [PDOException] SQLSTATE[HY000] [2002] No such file or directory

    Basically, I need the host set to localhost when browsing and 127.0.0.1 when using artisan. How can I simply use the same host so I don't have to keep changing the value every 2 minutes?

    Really confused. Seemed simpler before I started using homestead & vagrant... :/

  • Jared Eitnier
    Jared Eitnier almost 10 years
    SSH'ing into the VM and running artisan commands is def. the easiest way to go.
  • Kent Liau
    Kent Liau over 9 years
    the second idea is great, I created a development environment for me to run migrate at my host machine
  • kratos
    kratos over 7 years
    If you are using vagrant then just do vagrant ssh