The*_*age 6 php postgresql debugging ssh remote-access
在为该服务器创建 ssh2_tunnel 后,我正忙于尝试连接到 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());
?>
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误
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?
Run Code Online (Sandbox Code Playgroud)
来自 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
Run Code Online (Sandbox Code Playgroud)
对不起,它不会以这种方式工作。ssh2_tunnel创建一个远程文件的指针,又名资源,在PHP函数中使用像fgets(),fwrite()等,这是无法使用ssh端口转发是相同的。
你可以尝试打开SSH隧道你的PHP服务器上形成壳: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 主机的连接。
| 归档时间: |
|
| 查看次数: |
2784 次 |
| 最近记录: |