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

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

问题描述

我已经写了一个本地连接mysql数据库的php文件.现在,我想通过 SSH 连接到远程数据库.目前,我的数据库的连接函数在 php 中如下所示:

$this->db = new mysqli(_SERVR_URL, _SERVR_USER, _SERVR_PASS, _SERVR_DB);如果 ($this->db->connect_errno) {echo "无法连接到 MySQL: (" . $this->db->connect_errno . ") ".$this->db->connect_error;}别的{//echo "连接成功!!

";}

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

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

$ssh = new Net_SSH1(myURL);

我通常通过 SSH 连接到我的服务器的方式是使用 .pem 文件.我能否获得以下方面的指导:

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

解决方案

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

不过,您可能需要特殊权限才能执行此操作.它也是您似乎没有此托管帐户的 ssh 访问权限.

重复@jpm 回答>

设置隧道 由@Ólafur Waage 在 在 PHP 中通过 SSH 连接到 MySQL 服务器

这个用于@@索西

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

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

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