PHP使用ssh2_tunnel连接到PostgreSQL [英] PHP Connect to PostgreSQL using ssh2_tunnel

查看:107
本文介绍了PHP使用ssh2_tunnel连接到PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为该服务器创建ssh2_tunnel之后,我正忙于尝试连接到PostgreSQL数据库(在另一台服务器上运行)。



ssh2_tunnel似乎可以正常工作很好,但是我的pg_connect无法正常工作,希望我缺少一些小东西

 <?php 


ini_set('display_startup_errors',1);
ini_set(’display_errors’,1);
error_reporting(-1);


echo< span>测试脚本< br />< / span>;


$ connection = ssh2_connect(’xxx.xx.xx.xx’,22);


if(ssh2_auth_pubkey_file($ connection,'usname','./ssh_key.pub','./ssh_key','psword')){
echo< ; span>公钥身份验证成功< // span>;
} else {
die(公钥认证失败);
}


if($ tunnel = ssh2_tunnel($ connection,'127.0.0.1',5432)){
echo< span>隧道成功< br />< / span>;
}否则{
死亡(隧道失败);
};


//这是失败的地方,我不确定为什么
$ string = host = 127.0.0.1 port = 5432 dbname = dbname user = dbuser password = dbpass;
$ pgsqlcon = pg_connect($ string)或die(’无法连接:’。pg_last_error());


?>

这给了我以下错误

 警告:pg_connect():无法连接到PostgreSQL服务器:无法连接到服务器:连接被拒绝服务器在主机 127.0.0.1上运行并且接受端口上的TCP / IP连接5432? 

pg_last_error的另一个错误

 警告:pg_last_error():在第26行

解决方案

对不起,它不会以这种方式工作。 ssh2_tunnel 创建一个远程文件指针,也就是资源,可用于 fgets() fwrite()等。与SSH端口转发不同。



您可以尝试在您的php上打开ssh隧道服务器从外壳中输入: ssh usname@xxx.xx.xx.xx -i ./ssh_key -L 5555:localhost:5432 。会话处于活动状态时,您应该可以通过php脚本以 pg_connect( host = 127.0.0.1 port = 5555 dbname = dbname user = dbuser password = dbpass);



当然不是用于生产的。生产所需的是允许从php应用程序服务器访问数据库。您可能需要编辑 postgresql.conf 以确保服务器绑定到正确的接口,而 pg_hba.conf 允许来自您的php主机的连接。


I'm busy trying to connect to a PostgreSQL database (running on a different server) after creating an ssh2_tunnel to that server.

The ssh2_tunnel seems to be working fine but my pg_connect is not working and I'm hoping I'm missing something small

<?php


ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);


echo "<span>test script<br /></span>";


$connection = ssh2_connect('xxx.xx.xx.xx', 22);


if (ssh2_auth_pubkey_file($connection, 'usname', './ssh_key.pub', './ssh_key', 'psword')) {
    echo "<span>Public Key Authentication Successful<br /></span>";
} else {
    die('Public Key Authentication Failed');
}


if ($tunnel = ssh2_tunnel($connection, '127.0.0.1', 5432)) {
    echo "<span>Tunnel Successful<br /></span>";
} else {
    die('Tunnel Failed');
};


// this is where it is failing and I'm not sure why
$string = "host=127.0.0.1 port=5432 dbname=dbname user=dbuser password=dbpass";
$pgsqlcon = pg_connect($string) or die('Could not connect: ' . pg_last_error());


?>

It is giving me the following error

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

Another error from pg_last_error

Warning: pg_last_error(): No PostgreSQL link opened yet in /home/shoepack/public_html/admin_php_ssh/notused.php on line 26

解决方案

Sorry, it is not going to work this way. ssh2_tunnel creates a remote file pointer, aka resource, to be used in php functions like fgets(), fwrite() etc. It is not the same with ssh port forwarding.

You can try to open ssh tunnel on your php server form the shell: ssh usname@xxx.xx.xx.xx -i ./ssh_key -L 5555:localhost:5432. While the session is alive, you should be able to connect to the database from your php script as pg_connect("host=127.0.0.1 port=5555 dbname=dbname user=dbuser password=dbpass");

It is not for production use of course. What you need for production is to allow access to the database from your php application server. You may need to edit postgresql.conf to ensure the server is bound to correct interface, and pg_hba.conf to allow connections from your php host.

这篇关于PHP使用ssh2_tunnel连接到PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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