symfony 4: An exception occurred in driver: could not find driver

user1748166 picture user1748166 · Mar 15, 2019 · Viewed 9.8k times · Source

Working with Doctrine DBAL in Symfony 4 with the following conofiguration (doctrine.yaml):

dbal:
    # configure these for your database server
    driver: 'pdo_mysql'
    server_version: '5.7'
    charset: utf8mb4
    default_table_options:
        charset: utf8mb4
        collate: utf8mb4_unicode_ci

    url: '%env(resolve:DATABASE_URL)%'
    driver_class: App\DBAL\Driver\PDOMySql\Driver

and .env:

DATABASE_URL=mysql://root:[email protected]:3306/dbname

we are unable to connect to the database via DBAL:

$conn = DriverManager::getConnection(Constants::connectionParams,new Configuration());

Getting An exception occurred in driver: could not find driver exception. I haven't found documentation about that error in this version in S4.

Answer

M. Xhafa picture M. Xhafa · Mar 15, 2019

Looks like you are missing the PDO mysql extension.

Try to install it like below :

If you are running linux with apache2 do the following:

apt-get install php-mysql

After the above command has finished edit you php.ini file like so :

  • Search for pdo_mysql extension

It will be something like this

;extension=pdo_mysql.so

Change this line to this:

extension=pdo_mysql.so

And after changing php.ini file, restart apache service like this:

service apache2 restart

PS: you may need to use sudo