【问题标题】:Yii2 impossible to connect to postgresqlYii2无法连接postgresql
【发布时间】:2016-06-16 21:17:54
【问题描述】:

当我将 yii2 与 postgresql 数据库一起使用时出现此错误。

SQLSTATE[HY000] [2002] No such file or directory

Caused by: PDOException

SQLSTATE[HY000] [2002] No such file or directory

我这样配置文件 main-local.php :

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=dbname',
            'username' => 'user',
            'password' => 'pass',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

顺便说一句,当我使用 mysql 时,它正在工作。

【问题讨论】:

  • 检查http://localhost/requirements.php (docs) 的输出并查看您的服务器中是否安装了PDO PostgreSQL 扩展
  • 也可以试试localhost 而不是127.0.0.1
  • 我这样做了,但我得到了同样的错误
  • ok 然后检查您的php.ini 文件(或phpinfo())并查找PostgreSQL 套接字路径以查看它们是否设置正确。我对 PostgreSQL 不太了解,但使用 MySQL,它们看起来像:mysql.default_socket= /tmp/mysql.sockmysqli.default_socket= /tmp/mysql.sockpdo_mysql.default_socket= /tmp/mysql.sock
  • 在 php.ini 中没有用于 postgresql 的套接字部分或注释

标签: php database postgresql pdo yii2


【解决方案1】:

这显然是配置问题。

  1. 调用yii2提供的requirements page,检查是否安装了PDO Postgresql扩展。
  2. 正确配置。 PostgreSQL 与 MySQL 不同。每个数据库集群至少包含一个名为数据库。一个数据库至少包含一个名为 schema 的数据库,而后者又包含表。

有了这些知识,您的 main-local.php 应该如下所示:

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=localhost;dbname=YOURDATABASE',
            'username' => 'YOURPOSTGRESUSERNAME',
            'password' => 'YOURPOSTGRESPASSWORD',
            'charset' => 'utf8',
            'schemaMap' => [
                'pgsql' => [
                  'class' => 'yii\db\pgsql\Schema',
                  'defaultSchema' => 'public' //specify your schema here, public is the default schema
                ]
            ], // PostgreSQL
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2019-11-02
    • 1970-01-01
    • 2020-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    相关资源
    最近更新 更多