简体   繁体   English

如何使用ssh在Laravel 5中连接远程数据库

[英]How to Connect Remote Database in Laravel 5 using ssh

I am using Laravel 5.0. 我正在使用Laravel 5.0。 I Want to Know How to Access Remote Database using SSH. 我想知道如何使用SSH访问远程数据库。

database.php 为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,
        ],

You should create SSH tunnel. 您应该创建SSH隧道。

More about SSH tunnel and some examples here: http://chxo.com/be2/20040511_5667.html 有关SSH隧道的更多信息以及此处的一些示例: http//chxo.com/be2/20040511_5667.html

Example: 例:

ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com
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',
            ...
        ],

You will need to create an SSH Tunnel as Alexey says . Alexey说,你需要创建一个SSH隧道。

You will also need to port-forward the MySQL connection port to your local; 您还需要将MySQL连接端口移植到本地; as Alexey has done also. 正如阿列克谢所做的那样。

Then in your Laravel config, set the port to the forwarded port. 然后在Laravel配置中,将端口设置为转发端口。 So building off Alexey's answer, your database configuration would read thus 因此,基于Alexey的答案,您的数据库配置将如此

'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 如果Alexey的回答消失了,这些是我答案的相关部分

Create the ssh tunnel 创建ssh隧道

ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com

Connect locally using 使用本地连接

mysql -h 127.0.0.1 -P 3307 -u dbuser -p db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM