PHP:无法通过ssh隧道连接到Mysql

时间:2020-01-15 11:43:26

标签: php mysql mariadb ssh-tunnel

我需要隧道连接到mysql,数据库安装在centos服务器上。我有,可以使用我的钥匙连接到服务器。但不是ssh。

php仅在xampp apache,Windows 10和mysql在远程服务器上运行。

$user = 'root';
$password = '123456';
$database = 'example_db';
$server = 'example.com';
$port = '3307';
exec('ssh -f -L 3307:127.0.0.1:3306 root@example.com sleep 10 > /dev/null');
try {
    $db = new PDO("mysql:host=$server;port=$port,dbname=$database", $user, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

错误是

Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found

如果我在不使用ssh的情况下正常连接,那么一切正常,但是我需要使用ssh隧道,因为有些公司只允许我们访问其服务器上的端口80,并且可以使用ssh访问。

0 个答案:

没有答案