当我尝试使用Doctrine创建数据库时,为什么会出现PDOException?

时间:2017-03-02 12:22:23

标签: php doctrine-orm doctrine symfony

首先,我想提一下,只有当我尝试使用php bin/console doctrine:database:create重新创建数据库时才会出现此问题。 所有其他Doctrine命令都可以

为了证明这一点,我们删除数据库:

$ php bin/console doctrine:database:drop --force
Dropped database for connection named `my_database`

Process finished with exit code 0 at 01:59:44.
Execution time: 2 834 ms.

但是,现在,当我尝试使用php bin/console doctrine:database:create重新创建数据库时,我得到:

[PDOException]
  SQLSTATE[HY000] [1049] Unknown database 'my_database'

config.yml中的doctrine配置:

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

parameter.yml中的相关参数:

parameters:
    database_host: 127.0.0.1
    database_port: ~
    database_name: my_database
    database_user: root
    database_password: ~

Doctrine包列表:

doctrine/annotations              v1.4.0  Docblock Annotations Parser
doctrine/cache                    v1.6.1  Caching library offering an object-oriented API for many cache backends
doctrine/collections              v1.4.0  Collections Abstraction library
doctrine/common                   v2.7.2  Common Library for Doctrine projects
doctrine/dbal                     v2.5.12 Database Abstraction Layer
doctrine/doctrine-bundle          1.6.7   Symfony DoctrineBundle
doctrine/doctrine-cache-bundle    1.3.0   Symfony Bundle for Doctrine Cache
doctrine/doctrine-fixtures-bundle 2.3.0   Symfony DoctrineFixturesBundle
doctrine/inflector                v1.1.0  Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator             1.0.5   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                    v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                      v2.5.6  Object-Relational-Mapper for PHP

1 个答案:

答案 0 :(得分:3)

使用Doctrine DBAL 2.5时会出现此问题,如Symfony文档中所述:

  

在Doctrine DBAL 2.5中添加了server_version选项... 如果您没有定义此选项但尚未创建数据库,您可能会获得PDOException错误,因为Doctrine会尝试自动猜测数据库服务器版本,但没有可用。

所以我们必须在config.yml中定义数据库服务器版本:

doctrine:
    dbal:
        server_version: 5.7

要查找服务器版本,您可以:

  • 使用mysql -V用于MySQL
  • 对PostgreSQL使用postgres -Vpsql -V