Connecting to mysql database via SSH via PHP

I already wrote a php file that connects locally to the mysql database. Now I want to connect to a remote database via SSH. Currently the connection function for my database in php:

$this->db = new mysqli(_SERVR_URL, _SERVR_USER , _SERVR_PASS, _SERVR_DB); if ($this->db->connect_errno) { echo "Failed to connect to MySQL: (" . $this->db->connect_errno . ") " . $this->db->connect_error; } else{ //echo "Successfully connected!! <BR><BR>"; } 

I only want to change the connection function (see above) so that the rest of the code still works. I have successfully installed phpseclib and am not interested in installing php ssh extensions because they did not work after almost 5 hours of effort. Phpseclib works, and I think that is because when I use it does not die.

However, when I try to start working with the ssh file, it gives a server error:

 $ssh = new Net_SSH1(myURL); 

As usual, I use SSH on my server with a .pem file. Can I get some recommendations on:

  • Why can the current code cause an error?
  • If possible.
  • How would you write the connection code with the .pem file.
+4
source share
2 answers

I think you're out of luck. You can use ssh extensions in your PHP code, or if you have access to the server, you can try to create an ssh tunnel on the command line.

You probably need special permissions for this. It also looks like you do not have access to this ssh hosting account.

duplicate answered @jpm

Tunneling Tuning published by @ Γ“lafur Waage in Connecting to a MySQL Server via SSH in PHP

And this> for @Sosy tunneling

 shell_exec("ssh -f -L 3307:127.0.0.1:3306 user@remote.rjmetrics.com sleep 60 >> logfile"); $db = mysqli_connect('127.0.0.1β€², 'sqluser', 'sqlpassword', 'rjmadmin', 3307); 
+4
source

The mysql extension does not currently support this. Modifying the extension itself may not be so complex, but at this point, at best, it should be a regular PECL extension. The idea was discussed on the PHP Internals mailing list:

http://comments.gmane.org/gmane.comp.php.devel/79520

+1
source

Source: https://habr.com/ru/post/1495426/


All Articles