Php - Pdo Ssh Tunnel

2019-07-14 05:08发布

问题:

right now i am creating a ssh tunnel, so i can connect to my remote database, but for some reason the connection is still refusing... my script:

try {

    $host = 'remote host';
    $sshuser = 'ssh user';
    $sshpass = 'ssh password';
    $dbuser = 'db user';
    $dbpass = 'db user';
    $dbname = 'db name';

    shell_exec("ssh -p$sshpass ssh -o StrictHostKeyChecking=no -f -L 3307:127.0.0.1:3306 $sshuser@$host");

    $dbh = new PDO('mysql:host=127.0.0.1;port=3307;dbname=' .$dbname. '', $dbuser, $dbpass);

    $sth = $dbh->prepare("SELECT * from table");

    $sth->execute();

    $result = $sth->fetchAll();

    print_r ($result);

    shell_exec("kill $(ssh-pid)");

    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

And as a result i get:

Error!: SQLSTATE[HY000] [1130] Host 'host' is not allowed to connect to this MySQL server

回答1:

-p$sshpass ssh This particular part of the ssh command seems a bit odd to me. There should probably be a space after -p and the ssh shouldn't be there too.

Also, -p option is for port, not password.