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?