在创建一个PostgreSQL数据库(运行在另一个服务器上)之后,我正忙着连接到该服务器上。
ssh2_tunnel似乎运转良好,但我的pg_connect没有工作,我希望我错过了一些小东西
<?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());
?>它给了我以下错误
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的另一个错误
Warning: pg_last_error(): No PostgreSQL link opened yet in /home/shoepack/public_html/admin_php_ssh/notused.php on line 26发布于 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主机的连接。
https://stackoverflow.com/questions/33494865
复制相似问题