Laravel MySql DB Connection with SSH(使用 SSH 的 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.
在 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 应用程序中一样使用 DB:
Third, use the DB how you normally would in a Laravel App:
$users = DB::connection('mysql_EC2')
->table('users')
->get();
var_dump($users);
这篇关于使用 SSH 的 Laravel MySql 数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 SSH 的 Laravel MySql 数据库连接
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
