2015-10-06 106 views
4

我已經通過docker設置了一個新的開發環境。我有一個symfony2(2.6)應用程序,所以我想使用app/console doctrine方法使mysql數據庫更新。如何用doctrine2創建數據庫?

尚未運行

$ 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時創建它們,但我希望我的工作流通過doctrine2。

而只是爲了是絕對肯定的問題是無關我DATABSE設置我跑:

$ 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中如何initalized整個數據庫堆棧:試圖通過清除緩存

這errror也會發生。

的詳細的錯誤信息是這樣的:

[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] 
+0

它似乎是[symfony2.6中的一個問題](https://github.com/symfony/symfony/issues/12278),我需要檢查 – k0pernikus

+0

剛剛更新到symfony2.7.5,問題仍然存在。 – k0pernikus

+0

只是測試,它按預期工作:第一次運行: '創建的數據庫「項目」爲命名default' 第二次運行連接: '數據庫「項目」爲命名的默認連接已經存在。跳過。「 – COil

回答

8

如果你還是想用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中設置這個值。

+1

'server_version'也解決了我的應用問題。感謝發佈。 –

+1

爲我解決了這個問題,謝謝! –

0

說實話,我不知道你爲什麼產生DB失敗。但是,您可以也手動創建數據庫並授予權限在命令行上所需的用戶,然後讓學說填充數據庫:

首先,創建數據庫並授予權限:

CREATE DATABASE whatever; 
GRANT ALL ON whatever.* TO [email protected] IDENTIFIED BY 'pAsSwOrD'; 

然後,讓學說建立數據庫:

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

,或者,讓學說轉儲SQL命令,它會執行:

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

我在問題中指出我可以使用mysql客戶端來創建數據庫。我認爲這是解決問題的方法,但不是解決方案。 – k0pernikus

2

原來是doctrine/dbal組件中的一個迴歸。

我要求"doctrine/dbal":"2.4.*composer.json

然後它工作得很好:

$ 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並作爲然而等待resoultion。