Laravel MySql与SSH的数据库连接 [英] Laravel MySql DB Connection with SSH

查看:1090
本文介绍了Laravel MySql与SSH的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个要访问的远程数据库,但是它们位于只能通过带密钥的SSH访问的服务器上.

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.

在Sequel Pro中,我将如下所示连接到该远程数据库:

In Sequel Pro, I connect to this remote DB something like this:

我如何配置Laravel应用程序以连接到这样的数据库?

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'    => '',
    ),

推荐答案

这是通过带密钥的SSH通过EC2实例托管在数据库上的可行解决方案.

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'    => '',
    ),

第二,建立一条隧道:

ssh -i ~/dev/awskey.pem -N -L 13306:127.0.0.1:3306 ubuntu@54.111.222.333

(我们将SSH密钥传递给i参数,并建立SSH连接,并绑定到端口13306)

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

第三,像在Laravel App中一样使用数据库:

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

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

var_dump($users);

这篇关于Laravel MySql与SSH的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆