2

I'm using phpseclib to connect to mysql database with php for my app. I can connect to mysql with workbench using TCP/IP over SSH without a problem but I can't connect my code to it. I'm creating a ssh connection first and then I start with mysql connections and querys but I have this error

mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES)

I've found some people using a command like "$ssh->exec('mysql -u user -p password') to connect to mysql but after that, how can I do all my querys? as I understand, the "exec" command is as send commands to the server console?

Here is my code, I hope you can help me: This is conexion.php

include('Net/SSH2.php');
include('remote_conexion.php');

$db_host="127.0.0.1"; 
$db_user="root";
$db_password=" ";
$db_name="mascotas"; 
$ssh = new Net_SSH2($host);
if (!$ssh->login($sftp_user, $sftp_password)) {
    exit('Login Failed');
}
$connection = mysqli_connect($db_host, $db_user, $db_password, $db_name);

And this is remote_conexion.php (ignore the "sftp" things, I'm using that for manage files)

include('Net/SFTP.php');
$host="***.***.**.***";              
$sftp_user="user";              
$sftp_password="*******";   
$port="22";

   $sftp = new Net_SFTP($host);
if (!$sftp->login($sftp_user, $sftp_password)) {
    exit('Login Failed');
}
natvare
  • 77
  • 1
  • 8
  • Does the user have sufficient privileges ? Is the password " " an empty string ? – Blacksmith Nov 03 '19 at 23:57
  • PD of [Connect to a MySQL server over SSH in PHP](//stackoverflow.com/q/464317). You can't pass a PHP userland ssh handle to libmysql to use. – mario Nov 04 '19 at 00:05

2 Answers2

2

phpseclib doesn't (currently) support tunneling. And even if it did you wouldn't be able to use mysqli_connect - you'd have to have a pure-PHP MySQL implementation, kinda like https://github.com/amphp/mysql .

In lieu of that the best you're gonna be able to do (with phpseclib) is something like this:

echo $ssh->exec('mysql -uUSER -pPASSWORD DATABASE -e "SQL COMMAND"');

connect to the mysql database using phpseclib library talks about how to improve on that slightly but even with that you're not going to be able to make use of (for example) prepared statements or even mysqli_real_escape_string.

The best solution, imho, would be to use something like AutoSSH

neubert
  • 15,947
  • 24
  • 120
  • 212
0

I've read all your answers and I tried to connect from the traditional way, without using the ssh functions. Just like this:

 $connection = mysqli_connect($host, $db_user, $db_password, $db_name);

Using the server ip as "host", the username and password from mysql but I got this message now:

mysqli_connect(): (HY000/2002):An error occurred during the connection attempt since the connected party does not respond after a period of time, or an established connection error occurred that the connected host has not responded.

This way works when I'm using localhost, but not in this case with the remote server. All the login data it's ok, I can connect to database via workbench without a problem (I have to connect with workbench using TCP/IP over SSH) but I don't know why I can't connect with php.

natvare
  • 77
  • 1
  • 8