I have successfully connect to the My VPS using phpscelib library.Now i want to connect to my existing database.Please help me for this ?
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
$ssh = new Net_SSH2('192.ccc.ccc.ccc');
if (!$ssh->login('ccc', 'cccc')) {
exit('Login Failed');
}
echo $ssh->exec("I need to put MySql commands here");
?>
short and simple
First, wouldn't it be better to allow remote access for that user to mysql? However, I don't know your reasons.
The most common an transparent way would be create a ssh tunnel. This can be done in two different ways. If the mysql port (3306) isn't open on the mysql machine, you'll need a reverse ssh tunnel which has to be opened by the remote machine. Log into the mysql machine and issue the following command:
If the mysql port is open on the remote machine then the tunnel can be opened by the php machine:
Regardless of the way the tunnels has been created, the PHP application can now just use PDO and connect to localhost port 12345.
All traffic will get crypted through the tunnel.
If you just want to issue a couple of simple commands you might use the following alternative.
The simplest but unsecure way would be to use the following command:
This is insecure because other users on the system could see the password.
You can workaround the security issue using
expect
.expect
is a program which can pass the password to mysql in a more secure way. Make sure thatexpect
is installed on the remote system. Here comes an example using theSHOW TABLES
command on databasetest
:To further understand what's going, I've recently wrote a my blog article about that.