doupu0619 2017-03-02 12: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 12: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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程