mysql - PDO via SSH2 in PHP? -


i'm trying access remote mysql database reachable locally (localhost).

my code looks this:

$connection = ssh2_connect('ip server', 22);      if (ssh2_auth_password($connection, 'user', 'password')) {         echo "authentication successful!\n";     } else {         die('authentication failed...');     }      $tunnel = ssh2_tunnel($connection, '127.0.0.1', 3306);      try {         $this->_connection = new pdo($dsn, $config['login'], $config['password'], $flags);         $this->connected = true;         if (! empty($config['settings'])) {             foreach ($config['settings'] $key => $value) {                 $this->_execute("set $key=$value");             }         }     } catch (pdoexception $e) {         throw new missingconnectionexception(array(                 'class' => get_class($this),                 'message' => $e->getmessage()         ));     } 

the script successful till $tunnel. pdo connection fails "connection refused". think pdo trying connect mamp? $dsn variable looks "mysql:host=127.0.0.1;port=3306;dbname=mydb".

how can tell mamp, 127.0.0.1 on remote server connected through ssh?

thank you!

as noted, script attempting connect local mysql server instead of remote one. in order pdo establish connection remote server, establish ssh tunnel , forward local port server port. shown here http://brettrawlins.com/mysql-ssh-tunnel/.

to run ssh command in php: shell_exec(ssh code here)

you might want see: connect mysql server on ssh in php

however noted in there well, method relatively slow have create new tunnel each time query database, making queries take quite bit longer. suppose development environment it's valid option since don't have static ip, don't see working in production.

if trying sake of encryption, recommend connecting database ssl. note ssl pdo might not supported php versions.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -