問題描述
我已經寫了一個本地連接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 數據庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!