Laravel Homestead Vagrant Box Database Problems

laravel, mysql, vagrant

Solution

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:

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.

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.

Problem

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... :/

Original source