sequeliz如何连接已有的数据库,Laravel MySql与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.

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

f0d3b28c4db5d102a009eaaa023b8afa.png

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

),

解决方案

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

),

Second, establish a tunnel:

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

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

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

$users = DB::connection('mysql_EC2')

->table('users')

->get();

var_dump($users);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值