Connect to a mysql database via SSH through PHP(通过 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 文件.我能否获得以下方面的指导:
- 为什么当前代码可能会抛出错误?
- 如果可能的话.
- 您将如何使用 .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:
- Why the current code may be throwing an error?
- If this is possible.
- 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 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 SSH 通过 PHP 连接到 mysql 数据库
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01