1

我需要与另一台服务器上的 mysql 数据库建立连接,我有 RSA 密钥来连接到服务器,并且我还拥有连接到数据库所需的所有凭据,并且使用以下代码,我能够连接完美

try {
    $this->db = new PDO("mysql:host=$this->host;port=$this->port;dbname=$this->dbname", $this->sqlUser, $this->sqlPass);
} catch (Exception $e) {
    shell_exec("ssh -i /path/to/rsa/key/the_key myusername@some.ip.address -L3307:localhost:3306 -N");
};

try {
    $this->db = new PDO("mysql:host=$this->host;port=$this->port;dbname=$this->dbname", $this->sqlUser, $this->sqlPass);
} catch (Exception $e) {
    die($e->getMessage() . " (line:" . __LINE__ . ")");
};

但是,我正在使用 cakePHP 2.x 构建应用程序,如何使用 cakePHP 的配置连接到远程服务器和数据库?

谢谢

4

2 回答 2

1

好的,我通过在 app/Model/DataSource/Database 下创建一个名为 FarAwayMysql.php 的扩展类来设法做到这一点

App::uses('DboSource', 'Model/Datasource');

class FarAwayMysql extends DboSource {
public $description = "Faraway MySQL DBO Driver";

protected $_baseConfig = array(
    'persistent' => true,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'cake',
    'port' => '3306',
    'rsa' => '',
    'remote_username' => '',
    'remote_ip' => ''
);
protected $_connection = null;

protected $_useAlias = true;

public function connect() {
    $config = $this->config;
    $this->connected = false;
    try {
        $flags = array(
            PDO::ATTR_PERSISTENT => $config['persistent'],
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        if (!empty($config['encoding'])) {
            $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
        }
        if (empty($config['unix_socket'])) {
            $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
        } else {
            $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
        }
        $this->_connection = new PDO(
            $dsn,
            $config['login'],
            $config['password'],
            $flags
        );
        $this->connected = true;
    } catch (PDOException $e) {
        echo "ssh -i {$config['rsa']} {$config['remote_username']}@{$config['remote_ip']} -L3307:{$config['host']}:3306 -N";
        shell_exec("ssh -i {$config['rsa']} {$config['remote_username']}@{$config['remote_ip']} -L3307:{$config['host']}:3306 -N");
    }


    try {
        $flags = array(
            PDO::ATTR_PERSISTENT => $config['persistent'],
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        if (!empty($config['encoding'])) {
            $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
        }
        if (empty($config['unix_socket'])) {
            $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
        } else {
            $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
        }
        $this->_connection = new PDO(
            $dsn,
            $config['login'],
            $config['password'],
            $flags
        );
        $this->connected = true;
    } catch (PDOException $e) {             
        throw new MissingConnectionException(array(
            'class' => get_class($this),
            'message' => $e->getMessage()
        ));
    }

    $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");

    return $this->connected;
}
}

通过添加 3 个新配置

protected $_baseConfig = array(
    'persistent' => true,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'cake',
    'port' => '3306',
    'rsa' => '',
    'remote_username' => '',
    'remote_ip' => ''
);

然后在你的 app/Config/database.php 上,它会是

public $faraway = array(
    'datasource' => 'Database/FarAwayMysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'faraway_username',
    'password' => 'faraway_password',
    'database' => 'faraway_db_name',
    'port' => '',
    'rsa' => 'path/to/rsa/key',
    'remote_username' => 'your_username',
    'remote_ip' => 'your.ip.address'
);
于 2013-04-18T17:48:22.613 回答
1

谢谢你的帖子。我用PostgreSQL代替MySQL所以做了一些调整。其中一些可能也与此相关MySQL

1)当shell_exec'ing时,将输出重定向到/dev/null后台并在后台运行。端口也从更改3306/33075432/5433

shell_exec("ssh -i {$config['rsa']} {$config['remote_username']}@{$config['remote_ip']} -L5433:{$config['host']}:5432 -N > /dev/null &")

2)FarAwayPostgres.php如下

<?php
App::uses('Postgres', 'Model/Datasource/Database');

class FarAwayPostgres extends Postgres {
        public $description = "Postgres over SSH";

        protected $_baseConfig = array(
                'persistent'            =>  true,
                'host'                  => 'localhost',
                'login'                 => 'root',
                'password'              => '',
                'database'              => 'cake',
                'port'                  => '5432',
                'rsa'                   => '',
                'remote_username'       => '',
                'remote_ip'             => '',
                'sslmode'               => 'allow',
                'schema'                => 'public',
                'flags'                 => array()
        );

        protected $_connection = null;
        protected $_useAlias = true;

        public function connect() {

                $config = $this->config;
                $this->connected = false;

                $flags = $config['flags'] + array(
                        PDO::ATTR_PERSISTENT => $config['persistent'],
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                );

                try {
                        if (empty($config['unix_socket'])) {
                                $dsn = "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']};sslmode={$config['sslmode']}";
                        } else {
                                $dsn = "pgsql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
                        }

                        $this->_connection = new PDO( $dsn, $config['login'], $config['password'], $flags);
                        $this->connected = true;
                } catch (PDOException $e) {
                        $cmd="ssh -i {$config['rsa']} {$config['remote_username']}@{$config['remote_ip']} -L5433:{$config['host']}:5432 -N > /dev/null &";
                        shell_exec($cmd);
                        sleep(3);
                }


                try {
                        if (empty($config['unix_socket'])) {
                                $dsn = "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']};sslmode={$config['sslmode']}";
                        } else {
                                $dsn = "pgsql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
                        }

                        $this->_connection = new PDO( $dsn, $config['login'], $config['password'], $flags);
                        $this->connected = true;
                } catch (PDOException $e) {
                        throw new MissingConnectionException(array(
                                'class' => get_class($this),
                                'message' => $e->getMessage()
                        ));
                }

                return $this->connected;
        }
}

请注意如何插入三秒钟的睡眠,以便与数据库的连接尝试等待 ssh 完成。

最后,database.php

<?php
class DATABASE_CONFIG {

        public $default = array(
                'datasource'      => 'Database/FarAwayPostgres',
                'persistent'      => false,
                'host'            => 'localhost',
                'port'            => '5433',
                'login'           => 'postgres',
                'password'        => '',
                'database'        => 'remote_db',
                'rsa'             => '/var/www/.ssh/id_rsa',
                'remote_username' => 'root',
                'remote_ip'       => 'remote_server',
                'prefix'          => '',
                'encoding'        => 'utf8',
        );

私钥id_rsa位于 Apache 的主目录中,可供 Apache 读取。

现在,最后,在使用之前,执行以下操作:

su -m apache
ssh -i /var/www/.ssh/id_rsa root@remote_server

为了更新known_hosts

于 2016-03-30T03:56:32.227 回答