Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Db: How to connect to a MySQL database over SSH tunnel?

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

like image 952
Andrew Avatar asked May 10 '10 23:05

Andrew


1 Answers

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);
like image 73
ZZ Coder Avatar answered Oct 13 '22 01:10

ZZ Coder