6

How can I connect to a MySQL database that requires an SSH tunnel using PHP and the Zend Framework?

Andrew
  • 227,796
  • 193
  • 515
  • 708

1 Answers1

10

Just start up SSH tunnel and use the local port as your MySQL port.

For example, you start tunnel as this,

ssh -f user@mysql-server.com -L 3306:mysql-server.com:3306 -N

And you can connect to MySQL like this,

$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');

For zend_db, you do this,

$config = new Zend_Config(
    array(
        'database' => array(
            'adapter' => 'Mysqli',
            'params'  => array(
                'host'     => 'localhost',
                'dbname'   => 'my_db',
                'username' => 'mysql_user',
                'password' => 'mysql_password',
            )
        )
    )
);

$db = Zend_Db::factory($config->database);
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • I'm getting a "connection refused" message. It's also saying that it's connecting on port 22 which doesn't make sense since I am specifying the two ports...do you know why this isn't working for me? – Andrew May 11 '10 at 14:52
  • The error means you don't have SSH running on the server. It has nothing to do with MySQL. – ZZ Coder May 11 '10 at 15:01
  • ah, turns out it was an issue with the way I was connecting via ssh. – Andrew May 11 '10 at 16:57