数据库
首页 > 数据库> > 通过PHP通过SSH连接到mysql数据库

通过PHP通过SSH连接到mysql数据库

作者:互联网

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

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

但是,当我尝试开始使用ssh的东西时,它会抛出一个服务器错误:

$ssh = new Net_SSH1(myURL);

我通常SSH到我的服务器的方式是使用.pem文件.我可以得到一些指导:

>为什么当前代码可能会抛出错误?
>如果可以的话.
>您将如何使用.pem文件编写连接代码.

解决方法:

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由@jpm回答

Setting up tunneling由@ÓlafurWaage于Connect to a MySQL server over SSH in PHP发布

this one由@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,mysql,mysqli,ssh,phpseclib
来源: https://codeday.me/bug/20190620/1242837.html