【问题标题】:How to create database with doctrine2?如何用教义2创建数据库?
【发布时间】:2016-01-03 03:02:57
【问题描述】:

我已经通过 docker 设置了一个新的开发环境。我有一个 symfony2 (2.6) 应用程序,所以我想使用 app/console doctrine 方法初始化 mysql 数据库。

在运行时

$ php app/console doctrine:database:create --if-not-exists

我收到此错误消息:

  [Doctrine\DBAL\Exception\ConnectionException]                                                
  An exception occured in driver: SQLSTATE[42000] [1049] Unknown database 'local_api'  

  [Doctrine\DBAL\Driver\PDOException]                          
  SQLSTATE[42000] [1049] Unknown database 'local_api'

  [PDOException]                                               
  SQLSTATE[42000] [1049] Unknown database 'local_api'

mysql数据库确实不存在。我想首先创建。这是怎么回事?

我找到了bug report for the doctrine/doctrine-bundle,但我们已经在使用固定版本 1.5。

我也找到了symfony2.6 issue,不过升级 symfony2 到 2.7.5 后还是一样。

我知道当我将 direclty 连接到我的 mysql 数据库并运行 CREATE DATABASE local_api 时我可以创建它们,但我希望我的工作流程能够通过学说 2。

只是为了绝对确定该问题与我运行的数据库设置无关:

$ mysql --host=10.10.10.10 --user=foo --password=bar 
mysql> CREATE DATABASE local_api;
Query OK, 1 row affected (0.00 sec)
mysql> Bye
$ php app/console doctrine:database:create --if-not-exists
Database `local_api` for connection named default already exists. Skipped.

然后我可以使用删除数据库

php app/console doctrine:database:drop --force

但由于上述错误,重新创建它再次失败。

尝试通过以下方式清除缓存时也会发生此错误:

./app/console cache:clear

所以这似乎与 symfony2 如何初始化整个数据库堆栈有关。

详细的错误消息如下所示:

[2015-10-06 17:26:28] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".  {"uid":"c9f3fb0"}
[2015-10-06 17:26:28] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".  {"uid":"c9f3fb0"}
Clearing the cache for the dev environment with debug true
  Clearing outdated warmup directory
  Warming up cache



  [Doctrine\DBAL\Exception\ConnectionException]                                        
  An exception occured in driver: SQLSTATE[42000] [1049] Unknown database 'local_api'  



Exception trace:
 () at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:103
 Doctrine\DBAL\Driver\AbstractMySQLDriver->convertException() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:133
 Doctrine\DBAL\DBALException::driverException() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:47
 Doctrine\DBAL\Driver\PDOMySql\Driver->connect() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:360
 Doctrine\DBAL\Connection->connect() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:429
 Doctrine\DBAL\Connection->getDatabasePlatformVersion() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:389
 Doctrine\DBAL\Connection->detectDatabasePlatform() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:328
 Doctrine\DBAL\Connection->getDatabasePlatform() at ~/src/api/vendor/doctrine/doctrine-bundle/ConnectionFactory.php:61
 Doctrine\Bundle\DoctrineBundle\ConnectionFactory->createConnection() at ~/src/api/app/cache/de_/ap_DevDebugProjectContainer.php:1093
 ap_DevDebugProjectContainer->getDoctrine_Dbal_DefaultConnectionService() at ~/src/api/app/bootstrap.php.cache:2155
 Symfony\Component\DependencyInjection\Container->get() at ~/src/api/app/cache/de_/ap_DevDebugProjectContainer.php:1142
 ap_DevDebugProjectContainer->getDoctrine_Orm_DefaultEntityManagerService() at ~/src/api/app/bootstrap.php.cache:2155
 Symfony\Component\DependencyInjection\Container->get() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/ManagerRegistry.php:35
 Symfony\Bridge\Doctrine\ManagerRegistry->getService() at ~/src/api/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php:226
 Doctrine\Common\Persistence\AbstractManagerRegistry->getManagers() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php:54
 Symfony\Bridge\Doctrine\CacheWarmer\ProxyCacheWarmer->warmUp() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php:48
 Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at ~/src/api/app/bootstrap.php.cache:2641
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at ~/src/api/app/bootstrap.php.cache:2411
 Symfony\Component\HttpKernel\Kernel->boot() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:128
 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->warmup() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:90
 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->execute() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
 Symfony\Component\Console\Command\Command->run() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:886
 Symfony\Component\Console\Application->doRunCommand() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
 Symfony\Component\Console\Application->doRun() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
 Symfony\Component\Console\Application->run() at ~/src/api/app/console:27





  [Doctrine\DBAL\Driver\PDOException]                  
  SQLSTATE[42000] [1049] Unknown database 'local_api'  



Exception trace:
 () at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:47
 Doctrine\DBAL\Driver\PDOConnection::__construct() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:45
 Doctrine\DBAL\Driver\PDOMySql\Driver->connect() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:360
 Doctrine\DBAL\Connection->connect() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:429
 Doctrine\DBAL\Connection->getDatabasePlatformVersion() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:389
 Doctrine\DBAL\Connection->detectDatabasePlatform() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:328
 Doctrine\DBAL\Connection->getDatabasePlatform() at ~/src/api/vendor/doctrine/doctrine-bundle/ConnectionFactory.php:61
 Doctrine\Bundle\DoctrineBundle\ConnectionFactory->createConnection() at ~/src/api/app/cache/de_/ap_DevDebugProjectContainer.php:1093
 ap_DevDebugProjectContainer->getDoctrine_Dbal_DefaultConnectionService() at ~/src/api/app/bootstrap.php.cache:2155
 Symfony\Component\DependencyInjection\Container->get() at ~/src/api/app/cache/de_/ap_DevDebugProjectContainer.php:1142
 ap_DevDebugProjectContainer->getDoctrine_Orm_DefaultEntityManagerService() at ~/src/api/app/bootstrap.php.cache:2155
 Symfony\Component\DependencyInjection\Container->get() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/ManagerRegistry.php:35
 Symfony\Bridge\Doctrine\ManagerRegistry->getService() at ~/src/api/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php:226
 Doctrine\Common\Persistence\AbstractManagerRegistry->getManagers() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php:54
 Symfony\Bridge\Doctrine\CacheWarmer\ProxyCacheWarmer->warmUp() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php:48
 Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at ~/src/api/app/bootstrap.php.cache:2641
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at ~/src/api/app/bootstrap.php.cache:2411
 Symfony\Component\HttpKernel\Kernel->boot() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:128
 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->warmup() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:90
 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->execute() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
 Symfony\Component\Console\Command\Command->run() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:886
 Symfony\Component\Console\Application->doRunCommand() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
 Symfony\Component\Console\Application->doRun() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
 Symfony\Component\Console\Application->run() at ~/src/api/app/console:27





  [PDOException]                                       
  SQLSTATE[42000] [1049] Unknown database 'local_api'  



Exception trace:
 () at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
 PDO->__construct() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
 Doctrine\DBAL\Driver\PDOConnection->__construct() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:45
 Doctrine\DBAL\Driver\PDOMySql\Driver->connect() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:360
 Doctrine\DBAL\Connection->connect() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:429
 Doctrine\DBAL\Connection->getDatabasePlatformVersion() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:389
 Doctrine\DBAL\Connection->detectDatabasePlatform() at ~/src/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:328
 Doctrine\DBAL\Connection->getDatabasePlatform() at ~/src/api/vendor/doctrine/doctrine-bundle/ConnectionFactory.php:61
 Doctrine\Bundle\DoctrineBundle\ConnectionFactory->createConnection() at ~/src/api/app/cache/de_/ap_DevDebugProjectContainer.php:1093
 ap_DevDebugProjectContainer->getDoctrine_Dbal_DefaultConnectionService() at ~/src/api/app/bootstrap.php.cache:2155
 Symfony\Component\DependencyInjection\Container->get() at ~/src/api/app/cache/de_/ap_DevDebugProjectContainer.php:1142
 ap_DevDebugProjectContainer->getDoctrine_Orm_DefaultEntityManagerService() at ~/src/api/app/bootstrap.php.cache:2155
 Symfony\Component\DependencyInjection\Container->get() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/ManagerRegistry.php:35
 Symfony\Bridge\Doctrine\ManagerRegistry->getService() at ~/src/api/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php:226
 Doctrine\Common\Persistence\AbstractManagerRegistry->getManagers() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php:54
 Symfony\Bridge\Doctrine\CacheWarmer\ProxyCacheWarmer->warmUp() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php:48
 Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at ~/src/api/app/bootstrap.php.cache:2641
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at ~/src/api/app/bootstrap.php.cache:2411
 Symfony\Component\HttpKernel\Kernel->boot() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:128
 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->warmup() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:90
 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->execute() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
 Symfony\Component\Console\Command\Command->run() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:886
 Symfony\Component\Console\Application->doRunCommand() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
 Symfony\Component\Console\Application->doRun() at ~/src/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at ~/src/api/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
 Symfony\Component\Console\Application->run() at ~/src/api/app/console:27


cache:clear [--no-warmup] [--no-optional-warmers]

【问题讨论】:

  • 好像是an issue in symfony2.6,我需要查一下
  • 刚刚更新到 symfony2.7.5,问题依旧。
  • 刚刚测试,它按预期工作:第一次运行:Created database "project" for connection named default 第二次运行:Database "project" for connection named default already exists. Skipped.
  • 您的用户是否有足够的权限来创建数据库?
  • @lxg 当我直接在 mysql 上创建数据库时,我使用与 symfony2 应用程序相同的凭据,所以是的。

标签: php mysql symfony doctrine-orm


【解决方案1】:

如果您仍然想使用最新版本的doctrine/dbal,您只需在dbal 配置中指定server_version

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                dbname:         local_api
                user:           root
                password:       null
                host:           localhost
                driver:         pdo_mysql
                server_version: '5.5' # your database server version here

我无法告诉你为什么他们没有设置默认的 server_version,但这里是关于它的讨论 https://github.com/doctrine/DoctrineBundle/issues/351

由于您使用的是 mysql 数据库,因此查看 server_version 的方法如下:

mysql --version

> mysql  Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (x86_64) using readline 6.3

我们看到当前的 Distrib 是5.5

当然我建议你在parameters.yml中设置这个值。

【讨论】:

  • server_version 也解决了我的应用程序问题。感谢发帖。
  • 嗨,这解决了我的问题。但是你能解释一下为什么这会奏效吗?我以前从来没有用过这个。发生了什么变化?为什么我需要提供我的 mysql 数据库版本,这没有任何意义?提前致谢!
【解决方案2】:

原来是doctrine/dbal组件中的回归。

我在composer.json 中需要"doctrine/dbal":"2.4.*

然后它工作正常:

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)                                                
  - Removing doctrine/dbal (v2.5.2)
  - Installing doctrine/dbal (v2.4.4)
    Downloading: 100% 

$ php app/console doctrine:database:create
Created database for connection named `local_api`

The issue is known,目前还在等待解决。

【讨论】:

    【解决方案3】:

    老实说,我不知道为什么生成数据库会失败。但是,您也可以手动创建数据库并在命令行上授予所需用户权限,然后让 Doctrine 填充数据库:

    首先,创建数据库并授予权限:

    CREATE DATABASE whatever;
    GRANT ALL ON whatever.* TO user@localhost IDENTIFIED BY 'pAsSwOrD';
    

    然后,让 Doctrine 建立数据库:

    app/console doctrine:schema:update --force --complete
    

    或者,或者,让 Doctrine 转储它将执行的 SQL 命令:

    app/console doctrine:schema:update --dump-sql --complete
    

    【讨论】:

    • 我在问题中指出我可以使用mysql客户端创建数据库。我认为这是解决问题的方法,但不是解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2012-11-27
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多