Zend_Db: How to connect to a MySQL database over S

2019-02-07 10:00发布

问题:

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

回答1:

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);


回答2:

Is there any way to set this up to create a persistent connection in either the apache2 config or through Zend so it connects when needed from the server and remains open for a period of time in case subsequent calls come in?