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

14

13

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 !

Neuman answered 14/3, 2021 at 16:38 Comment(2)
Did you happen to notice that it is complaining about postgres though it appears you are trying to use mysql?Bellyache
yeah , i just realize that. Thank you very much.^^Neuman
Z
9

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

Zoubek answered 14/3, 2021 at 18:58 Comment(1)
If you are using docker, don't forget install mysql drivers RUN docker-php-ext-install pdo pdo_mysqlUndercast
D
16

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


sudo apt-get install php-mysql php-pdo

Deter answered 17/6, 2022 at 11:10 Comment(1)
Thanks, this solved my issue with this error.Geochemistry
Z
9

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

Zoubek answered 14/3, 2021 at 18:58 Comment(1)
If you are using docker, don't forget install mysql drivers RUN docker-php-ext-install pdo pdo_mysqlUndercast
B
3

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.

Belligerency answered 5/8, 2022 at 8:31 Comment(0)
P
2

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
Phyto answered 19/5, 2022 at 7:18 Comment(0)
U
2

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.

Undercast answered 6/1, 2023 at 21:38 Comment(0)
H
1

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

Humankind answered 1/9, 2021 at 21:52 Comment(0)
S
1

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

Sinatra answered 4/11, 2021 at 9:18 Comment(0)
G
1

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

Glace answered 25/1, 2022 at 14:23 Comment(0)
P
1

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!

Pegeen answered 2/8, 2022 at 14:29 Comment(1)
thanks man! This solution eventually worked in my case!Saveloy
A
1

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

Attainture answered 13/2, 2023 at 12:15 Comment(1)
Please don't duplicate existing answers, unless you want to add some new insightsTatyanatau
K
0

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

Kellikellia answered 14/2, 2022 at 15:46 Comment(0)
M
0

Delete or comment from .env file the line:

DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"
Moly answered 7/1, 2023 at 10:27 Comment(2)
Please add some explanation to your answer such that others can learn from itTatyanatau
@Moly Repeating it with less sophisticated formatting is not the same as adding an explanation.Diphthong
R
0

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.

Responser answered 11/5, 2024 at 9:53 Comment(0)
H
-1

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

Haileyhailfellowwellmet answered 27/6, 2024 at 8:15 Comment(2)
Can you also share how to resolve the problem? Usually, Twig's global variables are not used when running doctrine:database:createTatyanatau
Just comment the the service line, that's a quick fix but not very the best option. This happen if your service have autowring arguments that use your DB like entitymanager or any repository. You can reproduce it if you wantHaileyhailfellowwellmet

© 2022 - 2025 — McMap. All rights reserved.