Zend_Db: How to connect to a MySQL database over S

2019-02-07 10:02发布

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

2条回答
做个烂人
2楼-- · 2019-02-07 10:35

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?

查看更多
Luminary・发光体
3楼-- · 2019-02-07 10:43

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);
查看更多
登录 后发表回答