gpt4 book ai didi

mysql - 使用 SSH 的 Laravel MySql 数据库连接

转载 作者:IT老高 更新时间:2023-10-28 23:56:35 29 4
gpt4 key购买 nike

我有几个想要访问的远程数据库,但它们位于只能通过 SSH 使用 key 访问的服务器上。

在 Sequel Pro 中,我像这样连接到这个远程数据库: enter image description here

我将如何配置我的 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' => '',
),

最佳答案

这是一个可行的解决方案,通过带有 key 的 SSH 使用托管在 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 应用程序中一样使用 DB:

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

var_dump($users);

关于mysql - 使用 SSH 的 Laravel MySql 数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495364/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com