Homestead环境下Laravel项目MySQL迁移问题:仅能通过Vagrant SSH执行php artisan migrate
Hey Sergio, I’ve run into this exact issue with Homestead before—let’s break down why this is happening and how to fix it quickly.
Why the Local Terminal Fails
When you run php artisan migrate locally, your .env is set to DB_HOST=localhost and DB_PORT=33060. Here’s the core problem:
- On your local machine,
localhostoften tries to connect via a UNIX socket instead of the TCP port you’ve mapped (33060) to Homestead’s MySQL. - Inside Homestead SSH,
localhostpoints directly to the virtual machine’s MySQL instance (which listens on port 3306 natively), so the connection works without extra hoops.
Step-by-Step Fixes
1. Update Your .env Database Host
Change DB_HOST=localhost to DB_HOST=127.0.0.1. This forces your local terminal to use a TCP connection instead of a socket, which plays nice with Homestead’s port mapping:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=33060 DB_DATABASE=db_corsi DB_USERNAME=homestead DB_PASSWORD=secret
2. Verify Homestead Port Mapping
Double-check your Homestead.yaml file to ensure the MySQL port mapping exists. It should include this block:
ports: - send: 33060 to: 3306
If you added or modified this, reload Homestead to apply the changes:
vagrant reload --provision
3. Clear Laravel’s Configuration Cache
Laravel might be holding onto old cached config values, so clear them locally to make sure your updated .env settings take effect:
php artisan config:clear
4. Test the Connection Manually (Optional)
To confirm your local machine can reach the Homestead MySQL instance, run this MySQL command:
mysql -h 127.0.0.1 -P 33060 -u homestead -psecret db_corsi
If you get a MySQL prompt, the connection is working—your artisan migrate command should now run successfully locally.
内容的提问来源于stack exchange,提问作者SergioDe

