Comment créer une base de données avec doctrine2?
J'ai de l'installation d'un nouveau dev nécessitant via docker. J'ai une symfony2 (2.6) de l'application, donc je veux intitalize la base de données mysql à l'aide de la app/console doctrine
approche.
Encore lors de l'exécution de
$ php app/console doctrine:database:create --if-not-exists
J'obtiens ce message d'erreur:
[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'
- Il vrai que la base de données mysql n'existe pas. Je veux créer en premier lieu. Ce qui se passe ici?
J'ai trouvé un rapport de bug pour la doctrine/doctrine-bundle, pourtant, nous sommes déjà à l'aide de la version fixe de 1,5.
J'ai aussi trouvé un symfony2.6 question, mais elle prévaut également après la mise à niveau de symfony2 pour 2.7.5.
Je sais que je peux les créer quand je connecte directement à ma base de données mysql et exécuter CREATE DATABASE local_api
, mais je veux que mon flux de production grâce à doctrine2.
Et, juste pour rendre absolument sûr que le problème n'est pas lié à mon databse de l'installation, j'ai couru:
$ 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.
Je peux supprimer la base de données à l'aide de
php app/console doctrine:database:drop --force
Encore recréant il échoue à nouveau avec de l'erreur mentionnée ci-dessus.
Cette erreur se produit également lors de la tentative de vider le cache via:
./app/console cache:clear
Il semble donc liée à la façon dont symfony2 initalized l'ensemble de la db de la pile.
Détaillé message d'erreur ressemble à ceci:
[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]
Juste mis à jour pour symfony2.7.5, numéro persistent.
Viens de tester et il fonctionne comme prévu: 1er terme:
Created database "project" for connection named default
2e terme: Database "project" for connection named default already exists. Skipped.
Est-ce que votre utilisateur des privilèges suffisants pour la création de bases de données?
Quand j'ai créer la base de données sur mysql directement-je utiliser les mêmes informations d'identification que l'application symfony2 serait de l'utiliser, donc oui.
OriginalL'auteur k0pernikus | 2015-10-06
Vous devez vous connecter pour publier un commentaire.
Si vous voulez toujours le faire avec la dernière version de
doctrine/dbal
, tout ce que vous avez à faire est de spécifier laserver_version
dans le dbal de configuration:Je ne peux pas vous dire pourquoi ils n'ont pas mis par défaut server_version bien, mais ici, c'est la discussion à ce sujet https://github.com/doctrine/DoctrineBundle/issues/351
Depuis que vous utilisez une base de données mysql, voici comment voir votre server_version:
Nous voyons que le courant Distrib est
5.5
.Bien sûr, je vous recommande de définir cette valeur dans
parameters.yml
.server_version
résolu mon apps question. Merci pour l'affichage.Le problème est réglé pour moi aussi, merci!
Salut, cela a résolu mon problème. Mais pouvez-vous expliquer pourquoi cela a fonctionné? Je n'ai jamais eu à utiliser cette avant. De quoi ont-elles changé? Pourquoi j'ai besoin de ma base de données mysql version, cela n'a aucun sens? Merci à l'avance!
OriginalL'auteur Oliboy50
Il s'est avéré être une régression dans le
doctrine/dbal
composant.Je
"doctrine/dbal":"2.4.*
dans lecomposer.json
.Puis il a bien fonctionné:
Le problème est connu et encore de l'attente pour resoultion.
OriginalL'auteur k0pernikus
Pour être honnête, je ne sais pas pourquoi la génération de la DB échoue pour vous. Cependant, vous pouvez ainsi créer manuellement la base de données et l'octroi d'autorisations à l'utilisateur souhaité dans la ligne de commande, alors la Doctrine de remplir la base de données:
Tout d'abord, créer la base de données et l'octroi d'autorisations:
Alors, laissez-la Doctrine de configurer la base de données:
Ou, sinon, la Doctrine dump SQL la commande exécuter:
OriginalL'auteur lxg