How to create a database with doctrine2?

I have new dev protection installed through docker. I have a symfony2 application (2.6), so I want to initialize the mysql database using an approach app/console doctrine.

However, at startup

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

I get this error message:

  [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'

It is true that the mysql database does not exist. I want to create first. What's going on here?

I found a bug report for the doctrine / doctrine , but we are already using the fixed version 1.5.

I also found the symfony2.6 problem , but it also prevails after upgrading symfony2 to 2.7.5.

I know that I can create them when I connect direclty to my mysql database and start it CREATE DATABASE local_api, but I want my workflow to go through doctrine2.

, , :

$ 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 db.

:

[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]
+4
3

doctrine/dbal, , , server_version dbal :

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

, , 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

, 5.5.

, parameters.yml.

+8

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`

.

+2

Honestly, I do not know why database creation fails for you. However, you can also create the database manually and grant permissions for the desired user on the command line, and then populate the Doctrine database:

First create a database and grant permissions:

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

Then let Doctrine configure the database:

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

Or else, let Doctrine remove the SQL commands that it executed:

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

Source: https://habr.com/ru/post/1610390/


All Articles