Windows本地Apache PHP站点连接Cloud SQL Postgres(SSL)故障求助
PHP无法连接Google Cloud SQL Postgres实例(SSL证书验证失败)
Windows桌面部署Apache2+PHP网站,原访问本地PostgreSQL,现迁移至Google Cloud SQL Postgres实例,采用公网IP+SSL加密方式。Cloud SQL实例运行正常,所有Windows应用(.Net及.Net Core)均可成功连接,但PHP始终无法建立连接。
已尝试多种连接字符串及参数组合,示例pg_connect字符串(已脱敏):
host={secret information} port=5432 dbname={secret information} user={secret information} password={secret information} sslmode=require sslcert=client-cert.pem sslkey=client-key.pem
client-cert.pem和client-key.pem为Google生成/下载的证书,在Windows应用中可正常使用,文件存放于c:\apache24\htdocs目录下与HTML/PHP源码同目录,同时也存储了server-ca.pem并尝试引用。
测试页面代码:
<?php // Create a custom exception error handler for pg_connect function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new ErrorException($errstr, $errno, 0, $errfile, $errline); } // Set the error handler set_error_handler("exception_error_handler"); // Force output of all errors error_reporting(E_ALL); // Define constants define('DB_HOST', '{secret information}'); define('DB_USER', '{secret information}'); define('DB_PASS', '{secret information}'); define('DB_NAME', '{secret information}'); define('DB_PORT', '5432'); // Connection string with SSL certificate files $conn_str = 'host=' . DB_HOST . ' '; $conn_str .= 'port=' . DB_PORT . ' '; $conn_str .= 'dbname=' . DB_NAME . ' '; $conn_str .= 'user=' . DB_USER . ' '; $conn_str .= 'password=' . DB_PASS . ' '; $conn_str .= 'sslmode=require '; $conn_str .= 'sslcert=client-cert.pem '; $conn_str .= 'sslkey=client-key.pem '; // Try catch block grabbing stored exception try { echo "Attempting pg_connect: " . $conn_str . "<br>"; $conn=@pg_connect($conn_str); } catch (Exception $e) { echo $e->getMessage(); } // Attempt Connection $conn = pg_connect($conn_str) or die('Cannot connect to database.'); // Set SQL String $sql = 'SELECT * FROM client'; // Attempt query and iterate results if ($result = pg_query($conn, $sql)) { while($row=pg_fetch_row($result)) { var_dump($row); } } // Close it up pg_close($conn); ?>
测试得到的错误信息:
Attempting pg_connect: host={secret information} port=5432 dbname={secret information} user={secret information} password={secret information} sslmode=require sslcert=client-cert.pem sslkey=client-key.pem pg_connect(): Unable to connect to PostgreSQL server: FATAL: connection requires a valid client certificate Fatal error: Uncaught ErrorException: pg_connect(): Unable to connect to PostgreSQL server: FATAL: connection requires a valid client certificate in C:\apache24\htdocs\test.php:38 Stack trace: #0 [internal function]: exception_error_handler(2, 'pg_connect(): U...', 'C:\\apache24\\htd...', 38, Array) #1 C:\apache24\htdocs\test.php(38): pg_connect('host={secret information}...') #2 {main} thrown in C:\apache24\htdocs\test.php on line 38
已尝试添加证书和密钥的完整路径、引入server-ca.pem及添加sslpassword参数,但均无效,恳请提供新的解决思路。
内容的提问来源于stack exchange,提问作者Shane Brodie
相关产品推荐
相关产品推荐

