doupu0619 2017-03-02 04:22
浏览 68
已采纳

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

First, I would like to mention the issue appears only when I try to recreate the database using php bin/console doctrine:database:create. All other Doctrine commands work.

To prove it, let's delete the database:

$ 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.

However, now, when I try to recreate the database with php bin/console doctrine:database:create, I get:

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

The doctrine configuration in config.yml:

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

The related arguments in parameter.yml:

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

List of Doctrine packages:

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条回答 默认 最新

  • doujia7779 2017-03-02 04:22
    关注

    This issue can appear when using Doctrine DBAL 2.5, as stated in the Symfony documentation:

    The server_version option was added in Doctrine DBAL 2.5 ... If you don't define this option and you haven't created your database yet, you may get PDOException errors because Doctrine will try to guess the database server version automatically and none is available.

    So we have to define the database server version in config.yml:

    doctrine:
        dbal:
            server_version: 5.7
    

    To find you server version, you can:

    • use mysql -V for MySQL
    • use postgres -V or psql -V for PostgreSQL
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部