31

I have a couple of remote databases I would like to access, but they are sitting on a server accessible only through SSH with a key.

In Sequel Pro, I connect to this remote DB something like this: enter image description here

How would I configure my Laravel app to connect to such a DB?

'mysql_EC2' => array(
        'driver'    => 'mysql',
        'host'      => '54.111.222.333',
        'database' => 'remote_db',
        'username' => 'ubuntu',
        'password' => 'xxxxxxxxxxxxxxxxxxxx',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

4 Answers 4

56

Here's a workable solution of working with a database hosted on an EC2 instance via SSH w/ a key.

First, setup a corresponding connection in your database config:

'mysql_EC2' => array(
        'driver'    => 'mysql',
        'host'      => '127.0.0.1:13306',
        'database' => 'EC2_website',
        'username' => 'root',
        'password' => 'xxxxxxxxxxxxxxxx',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

Second, establish a tunnel:

ssh -i ~/dev/awskey.pem -N -L 13306:127.0.0.1:3306 [email protected]

(we pass in the SSH key to the i parameter and establish an SSH connection, binding to port 13306)

Third, use the DB how you normally would in a Laravel App:

$users = DB::connection('mysql_EC2')
        ->table('users')
        ->get();

var_dump($users);
Sign up to request clarification or add additional context in comments.

7 Comments

The tunnel command for some reason didn't work for me with the -N option. Also there's a type there for the local host. This command (-L joined with remote port or not, doesn't matter) worked for me: ssh -i PATH_TO_YOUR_KEY -L13306:127.0.0.1:3306 [email protected]
Is there a way to automate the execution of the script? Or create an artisan command such like "artisan tunnel"?
This worked for me, but I had to set the port separately with 'port' => '13306'. Using Laravel 5.1, thanks!
Thank you! This answer got me on the right track. I finally figured it out here: stackoverflow.com/questions/464317/…
Saved me so much frustration. just a note to anyone else reading this in the future, you can also use this for postgres using the same method (with the appropriate port, of course)
|
23

I wrote a Laravel Package to handle for us. stechstudio/laravel-ssh-tunnel

composer require stechstudio/laravel-ssh-tunnel

register the TunnelerServiceProvider::class and set up the configuration in your .env.

2 Comments

awesomely easy to use and works as expected, thanks for the package. 🎉
No longer maintained and does not support Laravel 11
4

In case the ssh server is just the tunnel and the db is not located there. You can use this.

ssh -i ~/path/your-key.pem -N -L 13306:your-db-hostname.com:3306 [email protected]

And set the db hostname in your laravel .env or database config file to 127.0.0.1 port 13306

1 Comment

This is the clean answer when you have RDS databases (where your db is no on the ssh instance).
0

Here is the solution if you're using Laravel Sail/Docker with private key authorization to connect to a MySQL database over SSH.

FIRST STEP:

Run the below command:

ssh -i "YOUR_PUBLIC_KEY_PATH.pem" -N -L 13306:YOUR_DATABASE_HOST:YOUR_DATABASE_PORT SSH_USER@SERVER_ADDRESS
  • -N tells SSH not to execute remote commands.

  • -L forwards your local port which is 13306 to the mysql port

  • -i specifies the path of your private key.

Once you hit the command, you won't likely to get any response if the command is run successfully.

SECOND STEP

Update your laravel.test service in docker-compose.yml file to allow another port which is 13306

 ports:
       
        - "13306:13306"

Rebuild the containers

THIRD STEP:

Update the following entries in your .env file:

DB_HOST=host.docker.internal
DB_PORT=13306
DB_USERNAME="keep the same as your db username"
DB_PASSWORD="keep the same as your db password"

This setup will direct Laravel to connect to the forwarded local port 13306, allowing access to your remote database over SSH.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.