简体   繁体   中英

Why do I get a PDOException when I try to create the database using Doctrine?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM