我有几个想要访问的远程数据库,但它们位于只能通过 SSH 使用 key 访问的服务器上。
在 Sequel Pro 中,我连接到这个远程数据库,如下所示:
如何配置我的 Laravel 应用程序以连接到这样的数据库?
'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 使用 key 使用 EC2 实例上托管的数据库。
首先,在数据库配置中设置相应的连接:
'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 key 传入i参数并建立SSH连接,绑定(bind)到端口13306)
第三,按照您通常在 Laravel 应用程序中的方式使用数据库:
$users = DB::connection('mysql_EC2')
->table('users')
->get();
var_dump($users);
关于mysql - 使用 SSH 的 Laravel MySql 数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27796641/