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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36034878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:25:14  来源:igfitidea点击:

How to Connect Remote Database in Laravel 5 using ssh

phplaravelssh

提问by Karthik

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

我正在使用 Laravel 5.0。我想知道如何使用 SSH 访问远程数据库。

database.php

数据库.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,
        ],

回答by Alexey Mezenin

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 [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',
            ...
        ],

回答by iGbanam

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; as Alexey has done also.

您还需要将 MySQL 连接端口转发到本地;正如阿列克谢所做的那样。

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

然后在 Laravel 配置中,将端口设置为转发端口。所以建立阿列克谢的回答,你的数据库配置会这样读

'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 隧道

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