ssuhat
ssuhat

Reputation: 7656

Laravel Homestead connect to remote database SSH

I'm stuck how to connect to remote database.

I've already tried to run

ssh -fNg -L 3307:127.0.0.1:3306 [email protected]

and my .env

 DB_CONNECTION=mysql
 DB_HOST=128.xxx.xxx.xxx
 DB_PORT=3307
 DB_DATABASE=projectdb
 DB_USERNAME=forge
 DB_PASSWORD=password;

I can connect the remote database using HeidiSql. But when I tried to connect using laravel project it always return me

 SQLSTATE[HY000] [2002] Connection timed out

Any other solution?

Upvotes: 0

Views: 997

Answers (1)

fideloper
fideloper

Reputation: 12293

In that scenario, your .env file should have setting DB_HOST=127.0.0.1 since you're tunneling (local) traffic from your Homestead server on port 3307 to traffic into the remote server on port 3306, specifically also on 127.0.0.1.

Another way to write that more explicitly would be:

ssh -fNg -L 127.0.0.1:3307:127.0.0.1:3306 [email protected]

(However, you can keep your current command of ssh -fNg -L 3307:127.0.0.1:3306 [email protected], I'm just showing you how you can write that another way to hopefully make the point more clear).

So, from Homestead, if you send traffic to 127.0.0.1:3307,it will get tunneled into the remote server and then connect to 127.0.0.1:3306 within it.

Upvotes: 2

Related Questions