Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour Laravel Homestead Database Connection

I am experiencing a peculiar error working with Laravel, Homestead and MySQL. This is the part of my .env file related to the database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=mydatabasename
DB_USERNAME=homestead
DB_PASSWORD=secret

If I set the port to 3306 I can access the tables from my application but I cannot execute commands from Terminal such as php artisan migrate. If I set the port to 33060 I can execute commands from Terminal but I cannot access the tables from my application.

like image 824
Diego Vidal Avatar asked Feb 03 '26 11:02

Diego Vidal


1 Answers

Your application is running on the IP provided in Homestead.yaml so when localhost is relative to your application port 3306 works. When running artisan while not SSH into your vagrant vm you are running the command relative to the localhost of your machine, not the vm, so your'e trying to run the migrations against a machine with no database.

The reason that port 33060 works for your local machine is because Homestead by default forwards this port to your vagrant virtual machines port 3306. But due to your .env now specifying port 33060 the vagrant virtual machine now can not reach port 3306.

Leave the DB_HOST set to 127.0.0.1 with port 3306 and SSH into your vagrant vm via the vagrant ssh command to run your migration command.

Or alternatively you could have multiple .env files for your various environments

like image 86
Tyler Benzing Avatar answered Feb 06 '26 01:02

Tyler Benzing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!