Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Connect Remote Database in Laravel 5 using ssh

Tags:

php

ssh

laravel

I am using Laravel 5.0. I Want to Know How to Access Remote Database using SSH.

database.php

'mysql' => [
            'driver'    => 'mysql',
            'host'      => 'www.xxxxx.in',
            'port' => '2222',
            'database'  => 'xxxx_xxx',
            'username'  => 'xxxxx_xx',
            'password'  => 'xxxx0xx',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
            'engine'    => null,
        ],
like image 924
Karthik Avatar asked Mar 16 '16 11:03

Karthik


People also ask

How to connect remote database using SSH tunnel in Laravel?

Use the following steps to connect remote database using ssh tunnel in laravel applications; as follows: Open ssh tunnel using the following ssh command; as follows: The ssh tunnel will open, but if you want to keep alive open that ssh tunnel then you can follow the below given steps.

How to connect DBeaver to a remote database server via SSH?

Bellow are the steps you can use to connect your DBeaver client to a remote database server via SSH. Note that you only need to do these steps once. The connection configurations will be saved and you can re-use them by right-click + connect. This guide is created using DBeaver version 6.3. 1. Create a new connection

How do I tail a remote connection in Laravel?

Laravel includes a helpful command for tailing the laravel.log files on any of your remote connections. Simply use the tail Artisan command and specify the name of the remote connection you would like to tail: Laravel Envoy provides a clean, minimal syntax for defining common tasks you run on your remote servers.

How do I connect to a remote server using SSH?

The SSH facade provides the access point to connecting to your remote servers and running commands. The configuration file is located at app/config/remote.php, and contains all of the options you need to configure your remote connections. The connections array contains a list of your servers keyed by name.


2 Answers

You should create SSH tunnel.

More about SSH tunnel and some examples here: http://chxo.com/be2/20040511_5667.html

Example:

ssh -fNg -L 3307:127.0.0.1:3306 [email protected]
mysql -h 127.0.0.1 -P 3307 -u dbuser -p db

Of course, then you need to change credentials:

'mysql' => [
            'driver'    => 'mysql',
            'host'      => 'www.xxxxx.in',
            'port'      => '3307',
            'database'  => 'xxxx_xxx',
            ...
        ],
like image 187
Alexey Mezenin Avatar answered Nov 15 '22 00:11

Alexey Mezenin


You will need to create an SSH Tunnel as Alexey says.

You will also need to port-forward the MySQL connection port to your local; as Alexey has done also.

Then in your Laravel config, set the port to the forwarded port. So building off Alexey's answer, your database configuration would read thus

'mysql' => [
      'driver'    => 'mysql',
      'host'      => 'www.xxxxx.in',
      'port'      => '3307',
      'database'  => 'xxxx_xxx',
      'username'  => 'xxxxx_xx',
      'password'  => 'xxxx0xx',
      'charset'   => 'utf8',
      'collation' => 'utf8_unicode_ci',
      'prefix'    => '',
      'strict'    => false,
      'engine'    => null,
],

EDIT
In case Alexey's answer goes away, these are the relevant parts to my answer

Create the ssh tunnel

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

Connect locally using

mysql -h 127.0.0.1 -P 3307 -u dbuser -p db
like image 20
Igbanam Avatar answered Nov 14 '22 22:11

Igbanam