views:

148

answers:

1

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

+4  A: 

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

For example, you start tunnel as this,

ssh -f [email protected] -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
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
The error means you don't have SSH running on the server. It has nothing to do with MySQL.
ZZ Coder
ah, turns out it was an issue with the way I was connecting via ssh.
Andrew