6

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,
        ],
1
  • I am interested in the answer to this question. Not only for the Laravel scenario but for others as well Commented Mar 16, 2016 at 12:40

2 Answers 2

8

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',
            ...
        ],
Sign up to request clarification or add additional context in comments.

1 Comment

I am looking for a good answer to point duplicates of this question to - this would be it, but the config shown is wrong, and will confuse ppl who are asking this question. I suggest www.xxxxx.in should be changed here to 127.0.0.1 to avoid confusion.
3

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

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.