1

I'd like to ask you, how to connect to an external database via tunnel using PDO?

My Database class is:

<?php 

    class Database {

        //Server Params
        private $server_addres = 'xx.xx.xx';
        private $port = '222';
        private $user = 'myuser';
        private $pass = 'mypass';

        //DB Params 
        private $host = '127.0.0.1';
        private $db_name = 'mydbname';
        private $username = 'mydbuser';
        private $password = 'mydbpassowrd';
        private $conn;

        //DB Connect
        public function connect() {
            $this->conn = null;

            try {

                //???
                shell_exec();

                $this->conn =  new PDO('mysql:host='.$this->host.';dbname='.$this->db_name, $this->username, $this->passowrd);
                $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch( PDOException $e ) {
                echo 'Connection Error: ' . $e->getMessage();
            } 

            return $this->conn;
        }
    }

Should I use shell_exec() function, if yes how?

Thank you for your help!

KamS
  • 139
  • 1
  • 10
  • 2
    Can you tell us what you mean by `tunnel` – RiggsFolly Nov 08 '18 at 14:12
  • Please take a look at this article: https://hostpresto.com/community/tutorials/how-to-connect-to-a-remote-mysql-server-via-an-ssh-tunnel/. I need to connect to database in the same way but using PDO... – KamS Nov 08 '18 at 14:14
  • I think this is probably what you want to do https://stackoverflow.com/questions/9738712/connect-to-remote-mysql-server-with-ssl-from-php – RiggsFolly Nov 08 '18 at 15:52
  • @RiggsFolly not really. This is a similar question: https://stackoverflow.com/questions/36977365/php-pdo-ssh-tunnel. Take a look at line no. 10. There is shell_exec() function, but I'm not sure what is the right syntax for this. My last test is: shell_exec('ssh -p'.$this->pass. 'ssh -o StrictHostKeyChecking=no -f -L 3306:'.$this->host.':3306 '.$this->user.'@'.$this->server_address.$this->port); but I still can't connect to the database. – KamS Nov 08 '18 at 16:44

0 Answers0