Connect Laravel to remote MySql DB with SSH

Quick way of connecting remote database with SSH key, will be by running SSH tunnel.

ssh -i ~/.ssh/key.pem -f username@000.000.000.00 -L 33061:remote-database-host:3306 -N

Once you have tunnel running, in app .env file you can connect database by simply following the config below.

DB_HOST=127.0.0.1
DB_PORT=33061
DB_DATABASE=db_name
DB_USERNAME=db_user_name
DB_PASSWORD=db_password

That’s it, cheers 🙂

About the author

Danyal Ali Butt

I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.

View all posts