简体   繁体   English

如何通过SSH隧道将PHP / Codeigniter应用程序连接到远程MySQL数据库?

[英]How can I connect a PHP/Codeigniter application to a remote MySQL database via SSH tunnel?

I am refactoring a spaghetti code PHP application into the Codeigniter framework and the development version of the application is on a different server than the production database. 我将意大利细面条代码PHP应用程序重构为Codeigniter框架,并且该应用程序的开发版本与生产数据库位于不同的服务器上。 For testing, I would like to establish an ssh tunnel from my EC2 instance where I am hosting the development version to the production database. 为了进行测试,我想从我将开发版本托管到生产数据库的EC2实例建立ssh隧道。 I have successfully created an ssh connection with the remote server via libssh2 AND with phpseclib ( http://phpseclib.sourceforge.net/ ). 我已经通过libssh2和phpseclib( http://phpseclib.sourceforge.net/ )与远程服务器成功创建了ssh连接。 However, after the ssh tunnel has been established, I am not able to connect to the database with the codeigniter mysqli db driver. 但是,在建立ssh隧道之后,我无法使用codeigniter mysqli db驱动程序连接到数据库。

With every attempt to connect to the database, I get the following error: 每次尝试连接数据库时,都会出现以下错误:

Unable to connect to your database server using the provided settings. 无法使用提供的设置连接到数据库服务器。 Filename: core/MY_Controller.php Line Number: 6 文件名:core / MY_Controller.php行号:6

The MY_Controller code is: MY_Controller代码为:

1  <?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3  class MY_Controller extends CI_Controller {
4 
5  public function __construct() {
6       parent::__construct();
7       $this->auth = new stdClass;
8       $this->load->library('flexi_auth');
9       if (!$this->flexi_auth->is_logged_in()) {
10          header('Location: /login');
11      }
12  }

I have modified the mysqli driver to include the tunnel object as a parameter in the mysqli real_connect function as follows, to no avail. 我修改了mysqli驱动程序,将隧道对象作为参数包含在mysqli real_connect函数中,如下所示,但无济于事。 Does anyone know how I can make this work? 有谁知道我该怎么做?

Thank you in advance for any help! 预先感谢您的任何帮助!

Mike 麦克风

I think my first attempt would be to create an ssh tunnel like this: 我认为我的第一个尝试是创建一个像这样的ssh隧道:

my_dev_ec2# ssh -L 3306:localhost:3306 myuser@my_production_ec2

And then config the mysqli db to connect to localhost as if it was in the production instance. 然后配置mysqli db以连接到localhost ,就像它在生产实例中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM