php ssh 连接mysql数据库连接,通过PHP通过SSH连接到mysql数据库

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!!
";

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值