394

I have just installed Debian Lenny with Apache, MySQL, and PHP and I am receiving a PDOException could not find driver.

This is the specific line of code it is referring to:

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)

DB_HOST, DB_NAME, DB_USER, and DB_PASS are constants that I have defined. It works fine on the production server (and on my previous Ubuntu Server setup).

Is this something to do with my PHP installation?

Searching the internet has not helped, all I get is experts-exchange and examples, but no solutions.

ghbarratt
  • 11,496
  • 4
  • 41
  • 41
Mike Moore
  • 7,228
  • 10
  • 42
  • 63
  • 33
    Look in your `php.ini' file and uncomment `extension=php_pdo_mysql.dll`. The path to your `php.ini` file can be found by looking at your phpinfo(). – styfle Jan 26 '12 at 21:58
  • 3
    FYI to any future readers, if you get this error and GoDaddy is your host, login your admin account. Hosting Details->Programming Languages. Upgrade your PHP version to the latest or at least 5.4 – Joe Jun 22 '14 at 00:06
  • @Joe I upgraded but still get an error (GoDaddy) – EugenSunic Oct 19 '15 at 13:47
  • @styfle 1up, On a Linux machine, I set `extension=msql.so` and it works! – AjayKumarBasuthkar Oct 17 '18 at 15:42
  • 6
    To whom it may concern: if you are using php 7.1+ on docker, you can `docker exec` into the container and run `docker-php-ext-install pdo pdo_mysql`. – cleybertandre Jun 09 '19 at 18:35
  • 3
    You can, but you shouldn't. Changes made like this only exist in the ephemeral, temporary container, and are easily lost from restarts, rebuilds and updates. Instead, you should add the line to your Dockerfile to assure it becomes a permanent part of the Docker image. – Torque Sep 12 '19 at 11:55
  • The fix through the `php.ini` file worked. Make sure to reboot apache. – El Dude Sep 14 '21 at 17:00
  • After changing php.ini a restart of the server is compulsory – erik-stengel May 10 '22 at 08:26

43 Answers43

306

You need to have a module called pdo_mysql. Looking for following in phpinfo(),

pdo_mysql

PDO Driver for MySQL, client library version => 5.1.44
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
224

The dsn in your code reveals you are trying to connect with the mysql driver. Your error message indicates that this driver is unavailable.

Check that you have the mysql extension installed on your server.

In Ubuntu/Debian you check for the package with:

dpkg --get-selections | grep php | grep mysql

Install the php5-mysql package if you do not have it.

In Ubuntu/Debian you can use:

  • PHP5: sudo apt-get install php5-mysql
  • PHP7: sudo apt-get install php7.0-mysql

Lastly, to get it working, you will need to restart your web-server:

  • Apache: sudo /etc/init.d/apache2 restart
  • Nginx: sudo /etc/init.d/nginx restart
ghbarratt
  • 11,496
  • 4
  • 41
  • 41
  • php-mysql is already the newest version (1:7.1+49+deb.sury.org~xenial+4). When try to run command sudo apt-get install php-mysql in ubuntu 16.04 with nginx php fpm i also restart the php fpm and then restart the nginx. – Prashant Barve Jan 12 '17 at 12:01
  • Currently it appears to be php-mysql and php7.1-mysql. Hopefully this is the pattern moving forward. – John P Mar 11 '18 at 22:21
97

Update: newer versions should use php-sqlite3 package instead of php5-sqlite. So use this, if you are using a recent ubuntu version:

sudo apt-get install sqlite php-sqlite3

Original answer to question is here:

sudo apt-get install sqlite php5-sqlite
sudo /etc/init.d/apache2 restart

If your phpinfo() is not showing the pdo_sqlite line (in my case, on my Ubuntu Server), you just need to run the lines above and then you'll be good to go.

Sudipta Chatterjee
  • 4,592
  • 1
  • 27
  • 30
  • Just the first line is necessary. – mattalxndr Feb 16 '12 at 15:57
  • 65
    Why did you give instructions for sqlite when the OP is using MySQL? – Andrew Ensley Sep 21 '12 at 19:29
  • 7
    @Andrew if you use `pdo_sqlite` and didn't install `php5-sqlite` you only get a `PDOException` with no info about missing sqlite and try to install `php5-mysql` twice. – Aitch Feb 09 '15 at 22:30
  • 2
    Note that php5-sqlite has become obsolete and may not be available for your system anymore, ie Ubuntu 16.04. Install php-sqlite3 instead. – Matthias Oct 21 '16 at 23:44
  • I am using php 7.1 and nginx how to install for php 7.1, I run the command sudo apt install php-mysql and it install the package and still getting the same problem. – Prashant Barve Jan 12 '17 at 11:59
  • 3
    @briankip Of course it is, if you're trying to connect to SQLite and that driver is missing. But the OP is trying to connect to MySQL. Installing sqlite would/could not solve his problem. – Andrew Ensley Mar 03 '17 at 17:21
  • 5
    @AndrewEnsley. The title of this page is the error that everyone gets regardless of their db platform. As a result, this page has THE highest google search rank if you google just the error. Therefore, IMO, any answer to this question relevant to other databases should be welcome here. If they don't work for the OP, so be it...OP doesn't have to accept those answers. That doesn't mean this answer isn't "useful" to others investigating the error message in the title that gifted this question with Zoltar's search rank. +1 – elrobis Mar 05 '19 at 17:52
  • 1
    We used MySQL but there were some integration tests that used SQLite, so this totally saved us. – n0nag0n May 10 '22 at 18:52
52

For newer versions of Ubuntu that have PHP 7.0 you can get the php-mysql package:

sudo apt-get install php-mysql

Then restart your server:

sudo service apache2 restart
ThomasAFink
  • 1,257
  • 14
  • 25
33

I had the same issue. The solution depends on OS. In my case, i have debian, so to solve it:

  • Updated my php version from (php5 to php7)
  • Install php-mysql and php7.0-mysql

    apt-get install php-mysql
    apt-get install php7.0-mysql
    
  • I edited my php.ini locate at /etc/php/7.0/apache2/php.ini

    uncomment the line : extension=php_pdo_mysql.dll
    
  • Then restart apache:

    service apache2 restart
    

This solves my problem

Community
  • 1
  • 1
onlyme
  • 3,776
  • 2
  • 23
  • 17
  • As an aside for those like me who found this didn't quite suffice and don't want to go through the steps to completely remove php5 before verifying that php7 will work: `a2dismod php5`, `a2enmod php7.0`, and `service apache2 restart` finally got me back up and running. – depwl9992 Nov 13 '17 at 04:05
26

On my Windows machine, I had to give the absolute path to the extension dir in my php.ini:

extension_dir = "c:\php5\ext"

fvrghl
  • 3,642
  • 5
  • 28
  • 36
berdzi
  • 261
  • 3
  • 2
  • 2
    I've just confirmed this to be true (Win2008). Strange - the other extensions work with just the recommended `extension_dir = "ext"`, but not this one. – scipilot Jul 09 '14 at 13:30
  • same here. added the path and started working. Windows10, php7.1 – Chris Gibb Nov 25 '17 at 22:54
15

Check if the module is available with php -m | grep pdo_mysql.

If not, for PHP 7.2, you can install relevant package with sudo apt install php7.2-mysql.

Use similar command on other PHP versions and package managers.

Pratik
  • 959
  • 1
  • 14
  • 20
14

On Ubuntu just execute

sudo apt-get install php5-mysql
Dmitry Sobolev
  • 927
  • 15
  • 18
  • 1
    I was behind a Symfony2 and Ubuntu. Pdo_mysql wasnt found ( [PDOException] could not find driver ). This solved the issue. – Reinherd Aug 01 '13 at 14:04
  • 2
    For me I was lacking Postgresql so it ended up being: `sudo apt-get install php5-pgsql` – Kzqai May 20 '16 at 17:46
10
sudo apt-get install php-mysql 

worked well on ubuntu and php 7

Moses Nandwa
  • 179
  • 2
  • 3
  • It worked for me, Although I am not using php7 but php5.6. But I do have 7 installed. I installed 5.6 later and switched to that without uninstalling php7 – Mubashar Abbas Jul 22 '16 at 07:20
6

When adding these into your php.ini ensure the php_pdo.dll reference is first before the db drivers dlls otherwise this will also cause this error message too. Add them like this:

[PHP_PDO]
extension=php_pdo.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
Paul Foster
  • 61
  • 1
  • 2
  • 2
    since PHP v5.3 the `php_pdo.dll` module is not required (not exists) anymore. http://php.net/manual/en/pdo.installation.php is probably the ultimate answer to this question. – Martin Zeitler Nov 12 '15 at 21:23
6

for Windows 8.1/10 in :\\php.ini file you should uncomment line "extension=pdo_mysql"

Egor Doynikov
  • 111
  • 2
  • 6
5

For PHP 5.5 on CentOS I fixed this by installing the php55-mysqlnd package.

sudo yum -y install php55w-mysqlnd # For Webtatic
sudo yum -y install php55u-mysqlnd # For Remi

For help installing, write a comment as it depends on the way PHP is installed on your system. Available repo's are webtatic and remi.

SimonW
  • 6,175
  • 4
  • 33
  • 39
5

Did you check your php.ini (check for the correct location with phpinfo()) if MySQL and the driver is installed correctly?

cem
  • 3,311
  • 1
  • 18
  • 23
4

I had the same problem during running tests with separate php.ini. I had to add these lines to my own php.ini file:

[PHP]
extension = mysqlnd.so
extension = pdo.so
extension = pdo_mysql.so

Notice: Exactly in this order

Facedown
  • 192
  • 1
  • 2
  • 10
4

Incorrect installation of PHP was being called

I was experiencing the same problem. And I hope this would help someone who is having a similar issue as me.

Scenario

OS = Windows 10  
Platform = XAMPP  
PHP Version = 7 (Multiple Version seem to have been installed in the PC)  

I created phpinfo.php file in the public folder and run the phpinfo() to look for the location of my php.ini file.

PHP.ini Location = c:\xampp\php\php.ini

Problem
Calling c:\xampp\htdocs> php -v returned PHP 7.2.3 but phpinfo.php showed PHP 7.2.2.

Solution
Instead of calling

php artisan migrate:install   

which gave me this error, I used

c:\xampp\php\php artisan migrate:install

and it worked.

RealSollyM
  • 1,530
  • 1
  • 22
  • 35
3

In my case my DSN string was incorrect, specifically it did not contain mysql://. I would have expected a different error message, perhaps something like 'DSN string does not specify driver/protocol.'

Adding mysql:// to the beginning of the DSN string resolved the issue.

random_user_name
  • 25,694
  • 7
  • 76
  • 115
gposton
  • 59
  • 7
3

Check if extension_dir in php configuration file set correctly. Try to comment/uncomment some extensions and see if it's reflected on phpinfo(). If it doesn't then either php config file cannot be loaded (wrong location) extension_dir is commented or set to the wrong location.

hserge
  • 905
  • 1
  • 9
  • 12
3

I spent the last day trying to figure out why I was getting the following error. I am running Ubuntu 14.04.

The Problem:
I noticed that my PHP-CLI version was running php7.0 but php_info() (the web version) was displaying php 5.5.9. Even though php_info() said pdo was enabled, using the command line (CLI) wasn't recognizing the pdo_mysql command. It turns out that mysql was enabled for my old version but not the CLI version. All I did was install mysql for php7.0 and it was able to work.

This is what worked:

To check the version:

php -v

To install mysql for php7.0

sudo apt-get install php7.0-mysql

1) make sure your CLI version is the same as your web version
2) If they are different, make sure your CLI version has the mysql plug-in since it doesn't come with it as a default.

2

The problem is a missing php to mysql library. In CentOs i fixed it by running # yum install php-mysql and then restarting apache with # /bin/systemctl restart httpd.service Note that the naming is slightly different from debian/ubuntu based distros, php->php5 and httpd->apache2.

2

I extremely recommend mysqllnd instead of mysql because of you would have a lot of problems like number converting and bit type evaluates problem with mysql extension.

on ubuntu install mysqllnd with following command:

sudo apt-get install php5-mysqlnd
MSS
  • 3,520
  • 24
  • 29
2

In my case, I was using PDO with php-cli, and it worked fine.

Only when I tried to connect from apache, I got the "missing driver" issue, which I didn't quite understand.

A simple apt-get install php-mysql solved it. (Ubuntu 16.04 / PHP7. credits go to the selected answer & Ivan's comment)

Hope it can help.

Balmipour
  • 2,985
  • 1
  • 24
  • 28
2
PHP Fatal error:  Uncaught PDOException: could not find driver

I struggled and struggled with "apt install php-mysql php7toInfinity and don't forget sqlite-what-ever's" and just could not get rid of this error message until I went back to basics and reset the file-permissions on the web-site in question.

These 3 commands reset file and folder permissions on the web-site and got it to work again.

cd /var/www/web-site-name.com/web/

# find (sub) directories and change permissions
find . -type d -exec chmod 755 {} \;

# find files and change permissions
find . -type f -exec chmod 664 '{}' \;
SamTzu
  • 167
  • 1
  • 7
1

I Fixed this issue on my Debian 6. Normally I just had installed php5-common package. After installation, you have to restart your web server (apache or nginx depending on which one you installed). Then I just do an lsof on the apache process id (lsof -p process_id) as followed :

sudo lsof -p 1399   #replace 1399 by your apache process id
apache2 1399 root  mem    REG  254,2    80352 227236 /usr/lib/php5/20090626/xmlrpc.so
apache2 1399 root  mem    REG  254,2   166496 227235 /usr/lib/php5/20090626/suhosin.so
apache2 1399 root  mem    REG  254,2    31120 227233 /usr/lib/php5/20090626/pdo_mysql.so
apache2 1399 root  mem    REG  254,2   100776 227216 /usr/lib/php5/20090626/pdo.so
apache2 1399 root  mem    REG  254,2   135864 227232 /usr/lib/php5/20090626/mysqli.so

As you can see above, the modules are installed on a file path not known or guided by common library path: /usr/lib/php5/20090626/. For your installation, it may be different, but only the path of pdo_mysql.so, pdo.so, mysqli.so. So, this is why Drupal or any other php engine couldn't find the library and shows that error: PDOException: could not find driver

I just don't know why it is installed on such a weird path, for me it's just a bug in the library package installation script in debian 6. I solved the issue by creating a symbolic for all the files under /usr/lib/php5/20090626/ to /usr/lib/php5/ with this command :

ln -s /usr/lib/php5/20090626/* /usr/lib/php5/

fvrghl
  • 3,642
  • 5
  • 28
  • 36
douggynix
  • 141
  • 2
  • 2
1
$DB_TYPE = 'mysql'; //Type of database<br>
$DB_HOST = 'localhost'; //Host name<br>
$DB_USER = 'root'; //Host Username<br>
$DB_PASS = ''; //Host Password<br>
$DB_NAME = 'database_name'; //Database name<br><br>

$dbh = new PDO("$DB_TYPE:host=$DB_HOST; dbname=$DB_NAME;", $DB_USER, $DB_PASS); // PDO Connection

This worked for me.

fvrghl
  • 3,642
  • 5
  • 28
  • 36
1

I faced the same issue after I removed the php5 package (that includes all the drivers as well) in order to install php7 package. I actually installed php7 package without a mysql module.

I managed to solve it by typing in the terminal:

1) $ apt-cache search php7 which lists all the modules, looking through the modules I found,

php7.0-mysql - MySQL module for PHP

2) $ sudo apt-get install php7.0-mysql

That's it. It worked for me in my linux system.

(use the appropriate php version, yours could be php5)

Roshimon
  • 1,991
  • 19
  • 16
1

Just one other thing to confirm as some people are copy/pasting example code from the Internet to get started. Make sure you have MySQL entered here:

... $dbh = new PDO ("mysql: ...  

In some examples this shows

$dbh = new PDO ("dblib ...
Dimitar
  • 4,402
  • 4
  • 31
  • 47
Joe
  • 35
  • 8
1

For those using Symfony2/3 and wondering why you're getting this error. If you're using "mapping_types", you might encounter this error. The reason is that "mapping_types" is placed at the wrong level. For instance :

doctrine:
  dbal:
    mapping_types:
        set: string

This "mapping_types" must be placed at this level :

doctrine:
dbal:
    #To counter the error caused by 'mapping_types'
    connections:
        default:
            server_version: %database_server_version%
            mapping_types:
                set: string

I hope this helps

I found the solution here : https://github.com/doctrine/DoctrineBundle/issues/327

aneth101
  • 509
  • 5
  • 9
1

Everywhere I go I read that the path of extension_dir should be changed from ext to an absolute path. It worked for me. However, when trying to build a server of my colleague's PC, I had to let the value to ext instead of putting an absolute path.

If you did put an absolute path and it does the extension is still not found, considerer trying both with the absolute path and ext.

papillon
  • 1,807
  • 2
  • 19
  • 39
1

Check correct path in extension_dir in you phpinfo().

1

Had the same issue, because I forgot to go into my virtual machine. If I go to my local directory like this:

cd /www/homestead/my_project
php artisan migrate

that error will appear. But it works on my virtual machine

cd ~/homestead
vagrant ssh   
cd /www/homestead/my_project
php artisan migrate
Adam
  • 25,960
  • 22
  • 158
  • 247
0

I had the same exception when trying to use pdo_sqlite. The problem was that the automatically created 20-pdo_sqlite.ini and 20-sqlite3.ini symlinks (in /etc/php5/mods-enabled) were broken.

Removeing the broken symlinks and adding them manually with:

  • sudo ln -s /etc/php5/mods-avaliable/pdo_sqlite.ini /etc/php5/mods-enabled/20-pdo_sqlite.ini
  • sudo ln -s /etc/php5/mods-avaliable/sqlite3.ini /etc/php5/mods-enabled/20-sqlite3.ini

fixed the problem.

Note: this won't work for older php versions as they did not look for configs in /etc/php5/mods-enabled

ioleo
  • 4,697
  • 6
  • 48
  • 60
0

If you are using sqlite for testing you will need php sqlite pdo drive. You can install them as below.

For Ubuntu 14.04

sudo apt-get install php5-sqlite
sudo service apache2 restart

In ubuntu 16.04 there is no php5-sqlite

sudo apt-get install php7.0-sqlite
sudo service apache2 restart
Ganesh Bhosale
  • 2,000
  • 18
  • 21
0

For Linux Mint

I had the same issue whilst using PhpBrew ( 5.5.9 / 7.0.14 ) and trying to create a PDO connection.

After I had tried most of the solutions on this post I did the following:

  1. Powered off PhpBrew

  2. Executed sudo apt-get install php7.0 (Linux Mint 17.2 / PHP7.0.16) - installed fresh php version

Bugs
  • 4,491
  • 9
  • 32
  • 41
Andrei .K
  • 11
  • 2
0

If you read all answer above and it still does not work...

Make sure that your PHP PDO connection string is fine. Not like mine:

$dbh = new PDO('"mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)

There is no information in error message what driver was not found.

After reinstalling all possible PDO and MySQL libraries I found out that there was " at start of my connection string.

JerzySBG
  • 109
  • 8
0

I had the same error on Shared Hosting using cPanel. By default pdo_mysql extension is installed if mysqlnd is installed.

But this was not the case, I had to enable the nd_pdo_mysql extension to use PDO. I used the PHP Selector GUI to change it, but that is because I was to lazy to setup a SSH connection.

O, forgot to add that the server is Apache with MariaDB (constantly forget that although MariaDB has the same parents as MySql, under the hood some functions may act slightly different).

PHP 8.0 Laravel 8.0

Hmerman6006
  • 1,622
  • 1
  • 20
  • 45
0

This answere is for mysql database and who are using Xammp

I tried alot of codes from StackOverflow but the main issue is that we have to check both files xammp php.ini file and also the php folder php.ini file and uncomment this extension "extension=pdo_mysql" in both .ini the comment one looks like this

;extension=pdo_mysql

just remove the ";" semicolon from the start after that restart your xammp and also run these commands in your terminal

php artisan migrate:fresh --seed

this command will fresh your migration and also did the seeding also

close your last run serve command and run it again

php artisan serve

Now it works for you.

If any mistake then any senior can update this answer

0

For people using MAMP,

If you have

  1. uncommented extension=php_pdo_mysql.dll
  2. set the right path where resides your extensions, (example: extension_dir = "C:\MAMP\bin\php\php7.4.1\ext\")

...and you still can't see the pdo_mysql extension on phpinfo(), then make sure you're calling phpinfo() from the document root of your web server and not from somewhere else.

If you've been calling it from somewhere else then you might have edited the wrong php.ini file:

  1. Go to the document root of your web server (the path can be found in Mamp > preferences > Web server)
  2. Create a file named info.php in this folder, open it and paste <?php phpinfo(); ?> then save the file.
  3. Open the info.php you've just created in your browser (typically, http://localhost/info.php)
  4. Now search for "Loaded Configuration File" in the page and look at the path of your php.ini file --> It's probably a different php.ini that the one you've been originally editing.
  5. So open this file and make the edits (uncomment.., etc)
  6. Restart your server
  7. Check info.php again, the extension pdo_mysql should appear.

If not, and if you've previously changed the path of your document root of your webserver like I did, I'd suggest you set it again to "C:\MAMP\htdocs" and place your website here because that's what really solved my problem.

Hope this could help some people.

0

I found that my config file php.ini with xammp is different than current config in php so edit the main file by running

php --ini
-2

Remove the semicolon before ;extension=php_pdo_mysql.dll in php.ini and save. Don't forget restart xampp Apache and Mysql.

-2

The solution for me was in the instalation of php, Dont have correctly configured the environment variable and the php.ini file. Doing this two correction an uncomented de line of extension = pdo_mysql.so works

-3

Had the same issue and just figured, website is running under MAMP's php, but when you call in command, it runs mac's(if no bash path modified). you will have issue when mac doesn't have those extensions.

run php -i to find out loaded extensions, and install those one you missed. or run '/Applications/MAMP/bin/php/php5.3.6/bin/php artisan {your command}' to use MAMP's

Entrust
  • 107
  • 1
-3

I solved this problem by enabling php_pdo_mysql.ddl in " IIS Information Service - > PHP Exstention " .

-7

In my case (Windows XP + Apache2.2 + PHP 5.4.31) I changed PHPIniDir in the httpd.conf to get around this problem:

from:

"C:\php5\" 

to:

"C:/php5/"
mhu
  • 17,720
  • 10
  • 62
  • 93
Anton
  • 1