通过PHP通过SSH连接到mysql数据库 [英] Connect to a mysql database via SSH through PHP

查看:619
本文介绍了通过PHP通过SSH连接到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个PHP文件,该文件在本地连接到mysql数据库.现在,我想通过SSH连接到远程数据库.目前,在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>";
    }

我只想更改connect函数(上面),以便其余代码仍然有效.我已经成功安装了phpseclib,并且对安装php的ssh扩展不感兴趣,因为经过近5个小时的努力,这些扩展无法正常工作. phpseclib正在工作,我认为这是因为当我使用require时,它不会消失.

但是,当我尝试开始使用ssh东西时,它会引发服务器错误:

$ssh = new Net_SSH1(myURL);

我通常SSH进入服务器的方式是使用.pem文件.我可以在以下方面获得一些指导吗?

  1. 为什么当前代码可能会引发错误?
  2. 如果可能的话.
  3. 如何使用.pem文件编写连接代码.

解决方案

我认为您对此不走运.您可以使用ssh PHP代码中的扩展名,或者如果您有权访问服务器,则可以 可以尝试在命令行上创建ssh隧道.

不过,您可能需要特殊的权限才能执行此操作.它也是 看起来您没有SSH对此托管帐户的访问权限.

重复由@jpm回答 >

设置隧道 @ÓlafurWaage在,用于通过@进行隧道传输豆豆

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);

I have already written a php file that connects to the mysql database locally. Now, I want to connect to a remote database via SSH. Currently the connect function for my database is the following 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 want to only change the connect function (above) so that the rest of the code still works. I have successfully installed the phpseclib and am not interested in installing php's ssh extensions because those were not working after nearly 5 hours of effort. The phpseclib is working, and I think this because when I use require it does not die.

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

$ssh = new Net_SSH1(myURL);

The way that I usually SSH into my server is with a .pem file. Can I get some guidance on:

  1. Why the current code may be throwing an error?
  2. If this is possible.
  3. How would you write the connection code with the .pem file.

解决方案

I think you are out of luck on this one. You can either use the ssh extension in your PHP code, or if you have access to the server, you could try to create a ssh tunnel on the command-line.

You probably need special permissions to do that, though. It also looks like you don't have ssh access to this hosting account.

duplicate answered by @jpm

Setting up tunneling posted by @Ólafur Waage on Connect to a MySQL server over SSH in PHP

And this one for tunneling by @Sosy

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);

这篇关于通过PHP通过SSH连接到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆