Thierry
Thierry

Reputation: 187

Symfony 5 - "An exception occurred in driver: could not find driver"

I'm currently face to this problem. I am really new beginer at Symfony.

Problem : When i create a new db with cli symfony console doctrine:database:create, i am getting these errors :

In AbstractPostgreSQLDriver.php line 102:

  An exception occurred in driver: could not find driver  


In Exception.php line 18:

  could not find driver  


In PDOConnection.php line 38:

  could not find driver

Iam using php 7.4 with xampp and normaly pdo_sql is installed :

extension=bz2
extension=curl
;extension=ffi
;extension=ftp
extension=fileinfo
extension=gd2
extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
extension=mbstring
extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=odbc
extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=shmop

this is my .env file config :

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="mysql://root:@127.0.0.1:3306/dbname"
DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###

And this is my doctrine.yaml file config :

doctrine:
dbal:
    url: '%env(resolve:DATABASE_URL)%'
    # IMPORTANT: You MUST configure your server version,
    # either here or in the DATABASE_URL env var (see .env file)
    #server_version: '13'
orm:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    mappings:
        App:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: App

I have been searching for hours but i cant get any solutions to solve my problem, someone has an idea ? I'm on windows.

Thanks a lot !

Upvotes: 15

Views: 83619

Answers (14)

jmauclair
jmauclair

Reputation: 25

I've faced a similar issue when running composer install, doctrine:database:create, or schema:create.

Check in your twig config file (config/twig.yml) if you have service called globally like :

globals:
        service: '@App\Service\YourServiceName'

Or anything that would use autowiring with repository or entity manager

Upvotes: -1

d.r.bond.juez
d.r.bond.juez

Reputation: 61

I found this discussion very helpful. My issue was similar, but with SQLite using Symfony in a Windows system.

Just in case someone is sort of confused by some of the diversity of answers this may clarify some stuff.

  • The question by Thierry is related to the use of a certain database type and having the php files correctly set to use these database types. The situation was that in their .env file they had both the mysql and the postgresql database urls uncommented (This is without an # in front of them).

  • Then the second image provided by Thierry looks like the Dynamic
    Extensions section of php.ini file (this file should be in the
    directory wherever you installed php). In these extensions Thierry
    had uncommented (this is without a ; in front) the extensions
    corresponding to mysql (extension=mysqli | extension=pdo_mysql) and
    even for sqlite (extension=pdo_sqlite), but it had commented the
    extensions for postgresql, the other database used in the .env
    (;extension=pdo_pgsql | ;extension=pgsql).

Hence, some of the responses are addressing the elimination or correction of the postgresql database url from the .env (responses: example1, example2, example3). Other responses are centered on abilitating the dynamic extension in the php.ini file (responses: example1, example2, example3), which in this case should have been postgresql if Thierry wanted to have both databases. Some responses even sort of suggest both options.

In the question's comments it is made clear that only postgresql database usage was causing the issue and Thierry clarified they only wanted to use mysql.

In summary, make sure that in your .env you have uncommented the database type which corresponds to the uncommented database type in your php.ini dynamic extensions section. This at least solved it for me.

Thanks for all the discussion. You are great.

Upvotes: 0

magic_ayyub
magic_ayyub

Reputation: 21

I had the same problem to solve the problem:

  • type php --ini to see the path to the file php.init

  • if you are on vscode do ctr + click on the path otherwise follow the path manually on your computer to the file php.init make sure your open php.init file with admin right !!

  • edit the file and uncomment extension=pdo_mysql

  • save and retry php bin/console doctrine:database:create

  • type php -m for check if your're in list pdo_mysql

here an illustration

Upvotes: 1

mbyou
mbyou

Reputation: 116

Delete or comment from .env file the line:

DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"

Upvotes: 0

Adrian98do
Adrian98do

Reputation: 179

I had the same problem, I am runing a Symofny application with docker. When you add doctrine-bundle to your app, in Dockerfile will appear this lines what will install drivers for postgres.

You have to remove it and add this line, this will install drivers for mysql

RUN docker-php-ext-install pdo pdo_mysql

I hope that helps to someone.

Upvotes: 2

Jon Green
Jon Green

Reputation: 41

In my case, I got my .env.local file from a colleague and stupidly saved it without the leading dot, so Symfony was using the values from the .env file, which reads DATABASE_URL="postgresql://symfony:[email protected]:5432/app?serverVersion=13&charset=utf8" rather than the correct DATABASE_URL="mysql://foo:[email protected]:3306/baz?serverVersion=mariadb-x.y.z" from .env.local.

Upvotes: 3

Aswin Kumar
Aswin Kumar

Reputation: 11

step 1 => go to Xaamp control panel then click config of Apache

step 2 => click on PHP(php.ini).browse through the file. you can able to see these extensions.

extension=bz2 extension=curl

remove ; from ;extension=pdo_pgsql and save it and close file

step 3 => now come back to your project.click on .env file and comment this line:

DATABASE_URL="postgresql://symfony:[email protected]:5432/app?serverVersion=13&charset=utf8"

this works!

Upvotes: 1

if you are working in Linux, probably some extensions are missing,run those command


sudo apt-get install php-mysql php-pdo

Upvotes: 19

Droom
Droom

Reputation: 191

I had the same problem, PHP 7.4.29, xampp, windows.

For the purpose of installing Xdebug I upgraded my PHP version to 7.4.29 (previously 7.4.18), and I guess this errror occured since then. Database connexion worked before. I copied the extensions from the old php.ini to the new one and the error remained.

What resolved the problem : added this line :

extension_dir = "D:\programs\xampp\php\ext"

And activated these extensions :

extension=bz2
extension=curl
extension=fileinfo
extension=gd2
extension=gettext
extension=mbstring
extension=exif
extension=mysqli
extension=pdo_mysql
extension=pdo_sqlite

Upvotes: 2

jozy kial
jozy kial

Reputation: 11

I had the same problem when I typed the following command: symfony console make:user User I solved the problem with the command: php bin/console make:user User. Wanting to create a user

Upvotes: 0

faiz334
faiz334

Reputation: 11

I also had this problem, just uncomment the DATABASE_URL that you have on your computer either (sqlite, mysql or postresql).

Upvotes: 1

oussama methnani
oussama methnani

Reputation: 117

php.ini

  1. copy path
  2. find the file then open it
  3. delete the ; in extension=mysqli

modified php.ini file without the ; in extension=mysqli

Upvotes: 1

Rami Nour
Rami Nour

Reputation: 2303

I'm running Homestead/vagrant on windows and my problem was, I wasn't able to execute this command php bin/console doctrine:database:create on windows even though I opened a terminal as administrator

As seen on these 2 pictures I tried Cmder and Git bash and both of them failed, saying an execption occured in driver: could not find driver

enter image description here

enter image description here

I then solved it by SSH into my vagrant box vagrant ssh navigated to the working symfony folder and ran the command from there php bin/console doctrine:database:create and for some reason that worked !

enter image description here

I wish there was an easier way to debug this stuff, but I guess that's how it is working with Windows. Hope this can help someone out there

Upvotes: 1

Micka Bup
Micka Bup

Reputation: 439

If you want to use Mysql , comment this line DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8" If not comment the other line

Upvotes: 9

Related Questions