John Davidson

php - Connect to MySQL over SSH

0 comments
Message:


I have to connect to a remote database. I must connect to the remote server with SSH in order to connect to the database.
I'm able to do that with MySQLWorkbench : I have no problem to access the database.


I'm able to tell my website to connect to the remote server :
(I use phpseclib because PHP libssh2 refuse to work with me)


$ssh = new SSH2('111.222.333.444', 12345);
if (!$ssh->login("SSH-username", "SSH-password")) {
echo 'Error';
} else {
echo 'Tadam !';
}

This short code (above) works fine. I have indeed access to the remote server.


If I add this (using default MySQL port 3306) :


new PDO('mysql:host=111.222.333.444;port=3306;dbname=DB-name', 'DB-username', 'DB-password');

PHP displays "Fatal error" (and only that...)


If I try that (using port 12345, used for SSH connection) :


new PDO('mysql:host=111.222.333.444;port=12345;dbname=DB-name', 'DB-username', 'DB-password');

PHP displays :



Fatal error: Uncaught PDOException: Packets out of order. Expected 0 received 45. Packet size=4739923


Next PDOException: SQLSTATE[HY000] [2006] MySQL server has gone away



Another info :
I can read the database using $ssh->exec(), but I would like to do this with PDO instead


$ssh->exec('mysql -uDB-username -pDB-password DB-name -e "SELECT * FROM table"');

I must confess that all these SSH things are not really my domain... I don't really understand how it works or what I must do...


I know that some similar questions have already been asked here on stackoverflow, I read them, but I still can't connect to this remote database.


Please, help me.



My Answer:
>login('username', 'password')) {
die('Login Failed');
}_$ssh->exec('mysql -h localhost -u db_username -p db_name');

Rate this post

4 of 5 based on 6724 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us