$remotehost="www.mysite.com";
$user="myuserid";
$pass="mypass";
$connection = ssh2_connect($remotehost, '22');
if (ssh2_auth_password($connection, $user, $pass))
{
echo "Authentication Successful!\n";
}
else
{
die('Authentication Failed...');
}
//database
$db_username = "mydatabaseusername";
$db_password = "mydatabasepassword"; // encrypted
$db_name="mydatabase";
$tunnel = ssh2_tunnel($connection, 'localhost', 3306);
$db = mysqli_connect('localhost', $db_username, $db_password, db_name, 3306) or die ('Fail: '.mysqli_connect_error());
Is this the right way to connect to mysql through ssh tunnel port 22,?
Run the script, the tunnel established fine, like $tunnel = ssh2_tunnel($connection, 'localhost', 3306);
The error is in this script.
$db = mysqli_connect('localhost', $db_username, $db_password, db_name, 3306) or die ('Fail: '.mysqli_connect_error());
I got the error Access denied for user 'mydatabaseusername'@'localhost'.
The user name and password for mysql database was right.
Through a desktop mysql client "toad for mysql". I can build ssh tunnel at port 22, and connect to mysql at 'mydatabaseuser'@ at port 3306. So the password and username works.
But in my php mysqli_connect code, it gave the error.