Zend_Db:如何通过 SSH 隧道连接到 MySQL 数据库?

2021-12-29 00:00:00 ssh php mysql zend-framework

如何使用 PHP 和 Zend 框架连接到需要 SSH 隧道的 MySQL 数据库?

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

推荐答案

只需启动 SSH 隧道并使用本地端口作为您的 MySQL 端口.

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

你可以像这样连接到 MySQL,

And you can connect to MySQL like this,

$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');

对于 zend_db,你这样做,

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

相关文章