【发布时间】:2021-10-01 21:13:23
【问题描述】:
我正在尝试使用 Unix 套接字连接到 PostgreSQL。我认为只有一种方法可以连接到主机上的数据库,但应用程序在 Docker 中(我遵循不容器化非复制数据库的原则)。我正在使用带有最新 Doctrine 的 Symfony 4。
我用 PDO 对原始 PHP 进行了测试,它可以工作。我不知道如何强制 Doctrine 使用 unix 套接字。
<?php
$dbh = new PDO('pgsql:host=/var/run/postgresql;user=xxxx;dbname=zzzz;password=yyyy');
$dbh->setAttribute(PDO::ERRMODE_EXCEPTION, true);
var_dump($dbh->errorInfo());
$stmt = $dbh->query('SELECT 1 as dupa;');
$stmt->execute();
var_dump($stmt->fetchAll());
而且它的结果和预期的一样:
www-data@ef0f2269b69a:~/html$ php o.php
array(3) {
[0]=>
string(5) "00000"
[1]=>
NULL
[2]=>
NULL
}
array(1) {
[0]=>
array(2) {
["dupa"]=>
int(1)
[0]=>
int(1)
}
}
问题是我在 Doctrine 中配置的任何内容:
In PDOConnection.php line 27:
[PDOException (7)]
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
我试过Doctrine的语法,原始的PDO语法,不知道。 是否有可能我的 pdo_pgsql 没有编译 unix 套接字支持?
这是我在 Doctrine 中尝试的数据库 URL:
pdo-pgsql://yyyy:xxxxx@/var/run/postgresql:5432/zzzzz
pgsql:host=/var/run/postgresql;user=xxxx;dbname=zzzz;password=yyyy
【问题讨论】:
标签: php postgresql symfony pdo doctrine-orm