首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PostgreSQL连接到ssh2_tunnel

使用PostgreSQL连接到ssh2_tunnel
EN

Stack Overflow用户
提问于 2015-11-03 08:53:56
回答 1查看 3.4K关注 0票数 7

在创建一个PostgreSQL数据库(运行在另一个服务器上)之后,我正忙着连接到该服务器上。

ssh2_tunnel似乎运转良好,但我的pg_connect没有工作,我希望我错过了一些小东西

代码语言:javascript
复制
<?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());


?>

它给了我以下错误

代码语言:javascript
复制
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?

来自pg_last_error的另一个错误

代码语言:javascript
复制
Warning: pg_last_error(): No PostgreSQL link opened yet in /home/shoepack/public_html/admin_php_ssh/notused.php on line 26
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-03 10:15:53

对不起,这样不行。ssh2_tunnel创建了一个远程文件指针,也就是资源,用于fgets()fwrite()等php函数。

您可以尝试从shell:ssh usname@xxx.xx.xx.xx -i ./ssh_key -L 5555:localhost:5432中打开php服务器上的ssh隧道。当会话处于活动状态时,您应该能够以pg_connect("host=127.0.0.1 port=5555 dbname=dbname user=dbuser password=dbpass");的形式从php脚本连接到数据库。

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33494865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档