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

时间:2013-06-27 00:52:04

标签: php mysql codeigniter ssh mysqli

我正在将意大利面条代码PHP应用程序重构到Codeigniter框架中,并且应用程序的开发版本与生产数据库位于不同的服务器上。为了测试,我想从我的EC2实例建立一个ssh隧道,我将开发版本托管到生产数据库。我已经通过libssh2和phpseclib(http://phpseclib.sourceforge.net/)成功创建了与远程服务器的ssh连接。但是,在建立ssh隧道后,我无法使用codeigniter mysqli db驱动程序连接到数据库。

每次尝试连接数据库时,都会出现以下错误:

无法使用提供的设置连接到数据库服务器。 文件名:core / MY_Controller.php 行号:6

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  }

我修改了mysqli驱动程序,将隧道对象作为参数包含在mysqli real_connect函数中,如下所示,无济于事。有谁知道我怎么能做这个工作?

提前感谢您的帮助!

麦克

1 个答案:

答案 0 :(得分:0)

我认为我的第一次尝试是创建一个ssh隧道,如下所示:

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

然后将mysqli db配置为连接到localhost,就像它在生产实例中一样。

相关问题