简体   繁体   中英

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

如何使用PHP和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

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

有没有办法设置它在apache2配置或通过Zend创建持久连接,以便它在需要时从服务器连接并保持打开一段时间以防后续呼叫进入?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM