61

I have a bad experience while installing laravel. However, I was able to do so and move to the next level. I used generators and created my migrations. But when I type the last command

php artisan migrate

It's throwing a PDOException - could not find driver.

       'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

That's my configuration in config/database.php.

I tried searching on stackoverflow and laravel forums and people suggest that it's PDO problem and not artisan's or php's - I followed those suggestions like adding

extension=pgsql.so
extension=pdo_pgsql.so

in php.ini

No positive result. It always says [PDOException]could not find driver.

Can someone please help resolving this.

Environment that I am using: Mac, laravel 4, MAMP PRO with php 5.4.4

Vamshi Vangapally
  • 1,972
  • 6
  • 21
  • 25
  • 1
    Do you have the `pdo_mysql.so` extension? – Mariano Córdoba Mar 17 '14 at 19:32
  • Yes I have... sorry pasted only 2 extensions, but I do have extension=imap.so extension=yaz.so extension=mcrypt.so extension=gettext.so extension=pgsql.so extension=pdo_pgsql.so extension=pdo_mysql.so – Vamshi Vangapally Mar 17 '14 at 19:38
  • And are you sure that the module is enabled? In LAMP a module can be enabled using `sudo a2enmod `. Look at your `phpinfo()` if the driver is enabled. – Mariano Córdoba Mar 17 '14 at 19:44
  • on `phpinfo()` - it shows that PDO drivers for sqlite, pgsql, mysql are enabled! – Vamshi Vangapally Mar 17 '14 at 19:47
  • 1
    As you are using the CLI try running `php -i | less` and looking to see if the PDO extension is actually being loaded. If not then you will need to amend your php.ini file that is used by the CLI which is not the same one as is used by Apache. It usually lives in the same directory as the PHP exe but thats on windows, not sure where it would be on a mac. but this guy does http://stackoverflow.com/questions/2750580/how-to-find-the-php-ini-file-used-by-the-command-line – RiggsFolly Apr 29 '14 at 13:34
  • @RiggsFolly I had the issue you specified. I didn't need `pdo_mysql` in `php.ini` and needed it in php-cli.ini instead. Thanks! – AturSams May 28 '14 at 12:29
  • Hello fellow developers, for those users of **windows 10 and WAMP** that found this post, i have a solution.[Please go visit this link][1]. It worked for me and it was difficult to come to the solution but i hope it might be of help for others in the future. [1]: https://stackoverflow.com/questions/54580252/windows-10-pdoexception-with-message-could-not-find-driver-laravel-5-6-33-wamp/54633825#54633825 – abel406 Feb 11 '19 at 15:33

27 Answers27

98

You can use

sudo apt-get install php7-mysql

or

sudo apt-get install php5-mysql

or

sudo apt-get install php-mysql

This worked for me.

Hemen Ashodia
  • 499
  • 3
  • 16
narendro
  • 996
  • 7
  • 2
  • 2
    sudo apt-get install php-mysql This worked for me. Thanks mate – Niroj Adhikary Feb 27 '17 at 11:27
  • 2
    If you're not sure which, check your PHP version with `php --version` and install the corresponding driver. – The Unknown Dev Aug 19 '17 at 23:38
  • 1
    When running the current version of Ubuntu, three years later, I too found that this solution solved my problem. `apt-cache search mysql | grep php` showed me that the package name I needed was now `php7.2-mysql` ... I installed it and the problem went away. – Mike Robinson Feb 25 '20 at 21:39
  • I was using WSL with a docker container inside it, when i call `php artisan test` inside the wsl, it gives the no driver error, but when i call it inside the container, it works. Installing php-mysql on wsl ( where i was running the `php artisan test` originally ) solves the problem. Thanks man! – Carlos Salles Jun 22 '21 at 14:01
27

You need to specifically enable the pdo_mysql plugin. Assuming you're using a standard PHP installation, then generally you would simply need to add this to your PHP.ini file:

extension=pdo_mysql.so

You need to ensure that this file exists in the extension directory though.

Adding pdo_pgsql.so doesn't help because that is for PostgreSQL.

Additionally, you should ensure that you restart the web-server after you make the changes to the PHP ini file, as the changes may not be reflected otherwise.

JaTochNietDan
  • 1,013
  • 9
  • 13
  • How can I check if that file exists in extension directory? – Vamshi Vangapally Mar 17 '14 at 19:40
  • Generally your extension directory would be where you installed PHP under in ./ext, for example on Windows it's ``C:\Program Files (x86)\PHP\ext`` by default using PHP's installer package. – JaTochNietDan Mar 17 '14 at 19:43
  • `extension_dir ="/Applications/MAMP/bin/php/php5.4.4/lib/php/extensions/no-debug-non-zts-20100525/"` is pointing to this and that folder has all the required .so files – Vamshi Vangapally Mar 17 '14 at 19:44
  • Have you restarted the webserver after making the appropriate changes to the PHP ini file? – JaTochNietDan Mar 17 '14 at 19:44
  • After setting up the configuration make sure you restart the web server sudo service apache2 restart – Rick May 14 '14 at 21:45
20

This worked for me:

sudo apt-get install php5-sqlite

This installed sqlite for php5, then I ran the php artisan migrate command again and worked perfectly.

Alex
  • 556
  • 5
  • 8
15

Ran into the same issue here, and turns out this is because PHP at the command line uses a different php.ini file from the browser version, which is why it looks like it's loading the extension correctly when you look at phpinfo() in a browser.

As per this answer, you just need to run:

php --ini

At the command line, which will tell you the path to the currently loaded php.ini (probably actually a php-cli.ini file located in the same place as your regular php.ini file).

Once you've found that, modify it with the pdo extensions you want (in this case for MySQL):

extension=pdo_mysql.so

Or, for any other users that are on Windows using some kind of WAMP server, it probably looks like this instead:

extension=php_pdo_mysql.dll
Community
  • 1
  • 1
Leith
  • 3,139
  • 1
  • 27
  • 38
6

I think you missed the mysql driver for php5. Execute below command:

sudo apt-get install php5-mysql
/etc/init.d/php5-fpm restart
Hemen Ashodia
  • 499
  • 3
  • 16
Robin Wang
  • 779
  • 1
  • 8
  • 16
6

You must be installing the latest version of php mysql in my case I am install php7.1-mysql

Try this

sudo apt-get install php7.1-mysql

I am using the latest version of laravel

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Jalalkun
  • 181
  • 1
  • 8
5

You are missing a PDO driver.

First install the driver

For ubuntu: For mysql database.

sudo apt-get install php5.6-mysql/php7.2-mysql

You also can search for other database systems.

You also can search for the driver:

sudo apt-cache search drivername

Then Run the cmd php artisan migrate
abhishek subedi
  • 921
  • 7
  • 3
4

Please check your Laravel .env file also. Make sure DB_CONNECTION=mysql, otherwise it won't work with MySQL. It tried to load something else.

Thach Van
  • 1,381
  • 16
  • 19
3

This worked with me:(for pgsql: use 'pgsql' instead of 'mysql')

Step 1)

sudo apt-get install php-mysql

Step 2)

php artisan config:clear

Step 3)

php artisan config:cache

Step 4)
Then restart your server, and generate key again and migrate it, Its Done

3

I got this error for the PostgreSQL and the solution is:

sudo apt-get install php7.1-pgsql

You only need to use your installed PHP version, mine is 7.1. This has fixed the issue for PostgreSQL.

Khuram
  • 1,820
  • 1
  • 26
  • 33
2

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
2

I was also getting the same error --> "[PDOException]
could not find driver "

After that I used many commands but not didn't get any help

Finally I used the following command, which solved my problem.

sudo apt-get install php5-sqlite

hmmm
  • 41
  • 4
2

Laravel Testing with sqlite needs php pdo sqlite drivers

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
1

I'm unsing ubuntu 14.04

Below command solved this problem for me.

sudo apt-get install php-mysql

In my case, after updating my php version from 5.5.9 to 7.0.8 i received this error including two other errors.

errors:

  1. laravel/framework v5.2.14 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.

  2. phpunit/phpunit 4.8.22 requires ext-dom * -> the requested PHP extension dom is missing from your system.

  3. [PDOException]
    could not find driver

Solutions:

I installed this packages and my problems are gone.

sudo apt-get install php-mbstring

sudo apt-get install php-xml

sudo apt-get install php-mysql

Community
  • 1
  • 1
sh6210
  • 4,190
  • 1
  • 37
  • 27
1

I had this problem on debian. The issue was, I was using the dotdeb repository for more recent PHP packages. For some reason, it updated the php cli to 7.0, while the web php binary remained php 5.6. I used this answer to link the php command line binary to the 5.6 version, like so:

$ sudo ln -sfn /usr/bin/php5 /etc/alternatives/php
Community
  • 1
  • 1
user151841
  • 17,377
  • 29
  • 109
  • 171
  • The linked answer help me out in my debian 9, i needed to update alternatives to php5.6. Thanks – Bantu Apr 04 '19 at 13:22
1

first check your php version like this :

php -v

after you get version number for example i get 7.1 then install like that

sudo apt-get install  php7.1-sqlite     //for laravel testing with sqlite
sudo apt-get install  php-mysql         //for default mysql
sudo apt-get install  php7.1-mysql      //for version based mysql 
sudo apt-get install  php7.1-common     //for other necessary package for php

and need to restart apache2

sudo service apache2 restart
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
0

For future searchers. On FreeBSD try the next

pkg_info | grep php5-pdo_mysql

if you see the empty line in return do the following:

cd /usr/ports/databases/php5-pdo_mysql

and the do the install

make install clean

then simply restart your apache and you are done

e.g. sudo /usr/local/etc/rc.d/apache22 restart

Aleksey Potapov
  • 3,683
  • 5
  • 42
  • 65
0

Install it via this command

sudo apt-get install php5-gd

this worked for me.

briankip
  • 2,502
  • 2
  • 23
  • 26
0

In my case I wasn't aware that the PHP run by Apache was different from the one run by CLI. That might be the case if during configuration in httpd.conf you specified a PHP module, not being the default one your CLI uses.

Sven P
  • 429
  • 5
  • 11
  • I've expanded on this in my answer. I always forget that the Apache PHP and the CLI PHP might be different. – DavidHyogo Mar 22 '22 at 02:31
0

I found the proper solution for this error! Either you don't have any database installed or your default database in this file config/database.php is set to some other database like this

'default' => 'sqlite',
'default' => env('DB_CONNECTION', 'sqlite'), //laravel 5.2

change the default to which one you have installed or install that database which is made to be default! I had installed mysql and php artisan migrate worked after i changed that line to this:

'default' => 'mysql',
'default' => env('DB_CONNECTION', 'mysql'), //laravel5.2
lewis4u
  • 14,256
  • 18
  • 107
  • 148
0

I was also getting the same error --> "[PDOException] could not find driver "

I realized that the php was pointing to /usr/bin/php instead of the lampp /opt/lampp/bin/php so i simply created and alias

alias php="/opt/lampp/bin/php"

also had to make update to the .env file to ensure the database access credentials were updated.

And guess what, it Worked!

lightup
  • 634
  • 8
  • 18
0

If you are using Laravel 5.x, and the error is from trying to modify the attributes of a column, it is possible the error comes from a missing pre-requisite.

Try this:

 composer require doctrine/dbal

Then try running your migrations.

From here: https://laravel.com/docs/master/migrations#modifying-columns

Arlyn
  • 169
  • 2
  • 9
0

If you are using arch based system like in manjaro

just use

sudo pacman -S php-sqlite

Restart your httpd

systemctl restart httpd
nayan verma
  • 103
  • 1
  • 11
0

for those users of windows 10 and WAMP that found this post, i have a solution.Please go visit this link. It worked for me and it was difficult to come to the solution but i hope it might be of help for others in the future.

abel406
  • 359
  • 5
  • 9
0

Assuming you did the normal installation with virtual box, vagrant etc.. This can happen to new people. PHP can be installed on your PC but all the modules PHP for your laravel project is on your VM So to access your VM, go to your homestead folder and type: vagrant ssh

then to go your project path and execute the php artisan migrate.

Crazy Alien
  • 126
  • 1
  • 9
0

It's an issue with MAC and MAMP. Try to add in your .env file this:

DB_SOCKET= /Applications/MAMP/tmp/mysql/mysql.sock

It work for me

whynot
  • 29
  • 3
0

This expands on Sven P's answer:

I was baffled by this error message myself when upgrading to a new DigitalOcean Droplet running Ubuntu 20.04 and Laravel 8.x. One thing to keep in mind is that you are running php artisan migrate on the command line and that might be different from the version of PHP you're running on Apache server. My Laravel test apps were working smoothly, but I couldn't run Postgresql related commands from the command line. Then I remembered that the default version of PHP for Ubuntu 20.04 was 8.x and I had configured my Apache server to run PHP 7.4. This tutorial was my salvation: How To Switch PHP Version on Ubuntu 20.04 LTS. Check your PHP version. Sure enough, my CLI PHP was version 8.x so it didn't have the Postgresql driver installed and enabled. Following the tutorial, I ran

sudo update-alternatives --config php

and selected my PHP 7.4 installation and now when I log in to my SSH client and check the PHP version, it displays 7.4, where I have installed and enabled Postgresql. So now my php artisan migrate command runs smoothly and I can get back to work.

This tip is relevant to any database connection you're using. For instance, if you normally used MySQL for your Apache apps, you would have the same error if your CLI PHP version was different and didn't have MySQL installed and enabled.

DavidHyogo
  • 2,838
  • 4
  • 31
  • 48