96

I am trying to install Laravel. I have installed Xampp, but when I try to setup my database using php artisan migrateI get the error:

[Illuminate\Database\QueryException] could not find driver (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations) [PDOException] could not find driver

config/database.php file has the relevant connections:

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],

    'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '1433'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
    ],

],

Any ideas?

3

25 Answers 25

143

In your php.ini configuration file simply uncomment the extension:

;extension=php_pdo_mysql.dll

(You can find your php.ini file in the php folder where your stack server is installed.)

If you're on Windows make it: extension=php_pdo_mysql.dll

If you're on Linux make it: extension=pdo_mysql.so

And do a quick server restart.

If this isn't working for you, you may need to install pdo_mysql extension into your php library.

4
  • 26
    it can also be the whole php-mysql extension missing. so maybe also something like: sudo apt install php7.3-mysql (change 7.3 whit your php ver)
    – andymnc
    Commented Mar 31, 2019 at 21:54
  • @Krishna Kant Chourasiya , hello i am also getting error in migration please help me , i have tried all the solution given in this page nothing working . here is the link stackoverflow.com/questions/60716326/… Commented Mar 17, 2020 at 5:51
  • The error is the same for all SQL drivers, so for who are not using MySQL try install your SQL driver... like php-pgsql or php-sqlite3. Commented May 14, 2022 at 22:46
  • 3
    for php version 8.2.5 its: extension=pdo_mysql just search for pdo_mysql in your php.ini file instead of php_pdo_mysql, and then uncomment. Commented Jun 12, 2023 at 17:03
62

We have solved the same error by following the below steps.

linux command for this type of error occurred then, first of all, check your php.ini file

If your php.ini file exists then in configuration file simply uncomment the extension:

;extension=php_pdo_mysql.dll 

Else follow below steps

step1:php -v

step2: Install php mysql extension

php 7.0 sudo apt-get install php7.0-mysql

php 7.1 sudo apt-get install php7.1-mysql

php 7.2 sudo apt-get install php7.2-mysql

php 7.3 sudo apt-get install php7.3-mysql

step3: service apache2 restart

step4: php artisan migrate
0
49

if you've installed php and mysql in your linux machine, php needs php-mysql extention to communicate with mysql. so make sure you've also installed this extention using:

sudo yum install php-mysql in redhat based machines.

and

sudo apt-get install php-mysql in debian machines.

4
  • 3
    This answer worked for me in ubuntu 18.04 thanks @Ali_Hr Commented Jul 2, 2020 at 11:55
  • 1
    This answer worked for me on Ubuntu 20.04 LTS, i was using Xampp as my localhost environment thanks for saving my day @Ali_Hr
    – matynjr
    Commented May 28, 2021 at 10:23
  • 1
    This worked for me on Ubuntu 20.04 LTS. Commented Mar 16, 2022 at 17:57
  • 1
    Worked on Ubuntu 22.04 LTS. Commented Jun 2, 2022 at 4:53
32

You also need to install the PHP-MySQL drivers:

sudo apt install php-mysql
0
14

Make sure that you've installed php-mysql, the pdo needs php-mysql to operate properly. If not you could simply type

sudo apt install php-mysql
14

the same problem I got but after I run this line of code.

sudo apt install php-mysql

Done.

11

I know this is a little late, nevertheless i'm answering this for anyone still experiencing this issue on windows (USING XAMPP).

Step 1. Verify that there is a mismatch in your PHP version. To do this, open routes/web.php and create a simple route to return the php information like so:

Route::get('/', function() {
   return response()->json([
    'stuff' => phpinfo()
   ]);
})

phpinfo

and compare this with output from your command line

enter image description here

As we can see, in my case there is a mismatch, this usually happens if you have multiple versions of php installed.

Step 2. Add the php version being used by your xampp to your system path enter image description here

Step 3. Verify that you have the extension enabled in your php.ini file. NB. Make sure you are editing the php.ini file that is shown under the 'loaded configuration file' entry in the results of phpinfo() command. enter image description here

11

In your php.ini configuration file simply uncomment the extension:

;extension=pdo_mysql

(You can find your php.ini file in the php folder where your server is installed.)

make this to

extension=pdo_mysql

now you need to configure your .env file in find DB_DATABASE= write in that database name which you used than migrate like if i used my database and database name is "abc" than i need to write there DB_DATABASE=abc and save that .env file and run command again

php artisan migrate

so after run than you got some msg like as:

php artisan migrate
Migration table created successfully.
7

I am a Windows and XAMPP user. What works for me is adding extension=php_pdo_mysql.dll in both php.ini of XAMPP and php.ini in C:\php\php.ini.

Run this command in your cmd to know where your config file is

php -i | find /i "Configuration File

6

In Windows and PHP 7.4.5 go to php.ini and uncomment this line

extension=pdo_mysql
6

This error is the same for all SQL drivers, so for who are not using MySQL try install your SQL driver...

SQL Lite:

sudo apt install php-sqlite3

PostgreSQL:

sudo apt install php-pgsql
1
  • 2
    for me, i need to install exactly the php version for it, such as php7.4-pgsql
    – fudu
    Commented Jun 20, 2023 at 7:35
4

If you are on linux systems please try running sudo php artisan migrate As for me,sometimes database operations need to run with sudo in laravel.

0
4

Install by running sudo apt install php-mysql and enabling extension=pdo_mysql in php.ini

Then reload apache by running sudo systemctl reload apache2

To quickly find your php.ini in your server, just run this command: php -i | grep "Loaded Configuration File"

Laravel Side:

php artisan config:clear

php artisan migrate

It works for me and good luck!

3

For anyone trying to enable the extension inside a Docker image:

RUN docker-php-ext-install pdo pdo_mysql \
    && docker-php-ext-enable pdo_mysql

Based on this answer.

Also props to @Krishna for shedding some light on the extension issue.

3

This solution works for me (Nginx / php8.1 / ubuntu 20.04)

  1. In php.ini uncomment Mysql PDO extension

     extension=pdo_mysql
    
  2. Install extension

     sudo apt install php8.1-pdo-mysql
    
  3. Restart Nginx

     sudo systemctl reload nginx
    
1
  • sudo apt install php8.1-pdo-mysql gives me unable to locate package php8.1-pdo-mysql
    – b_dubb
    Commented Oct 20, 2023 at 17:01
2

For php 8.2 > you will have to go into the php ini file which is located i assume in C:\php-8.2 and you want to find ;extension=pdo_mysql and remove the ;.

So now you will have inside the php ini file extension=pdo_mysql.

After that, restart the server and you will go to go.

Some of the above answers.. are old and they are using php7 etc.. That's why they are telling you to find in the php ini file the ;extension=php_pdo_mysql.dll which in the newest versions of php are written like this extension=pdo_mysql

1

If you are matching with sqlite database:
In your php folder open php.ini file, go to:

;extension=pdo_sqlite

Just remove the semicolon and it will work.

1

I am so embarrassed. I know this doesn't help those using XAMPP, but I had to share. I have an out-of-the-box Laravel installation in Vagrant using a ScotchBox virtual machine running in Windows 10. And I am following the Digital Ocean tutorial. This should just work.

Like you, I ran php artisan migrate and I got

could not find driver (SQL: select * from sqlite_master where type = 'table' and name = migrations)

A few hours later, I thought

Maybe I should try inside of the Vagrant shell?

I opened vagrant ssh and ran php artisan migrate again. There I got a different message:

Could not open input file: artisan

A few more hours later, I got it right.

vagrant ssh

cd \var\www\

php artisan migrate

Migration table created successfully

So, yeah. I was in the wrong shell and the wrong folder.

0

whilst sometimes you might have multiple php versions, you might also have a held-back version of php-mysql.. do a sudo dpkg -l | grep mysql | grep php and compare what you get from php -v

0
Go to .env file and change the following
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shreemad
DB_USERNAME=root
DB_PASSWORD=

Change the DB_PASSWORD field to

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shreemad
DB_USERNAME=root
DB_PASSWORD=" "

In my case it works

NOTE: If your password in mysql is null

0

in ubuntu or windows

  • Remove the ; from ;extension=pdo_mysql or extension=php_pdo_mysql.dll and add extension=pdo_mysql.so

    restart xampp or wampp

  • install sudo apt-get install php-mysql

and

php artisan migrate

0

If you have multiple php versions installed applied all other options like UN commenting driver in php.ini and it does not work you might missed following step

  1. Please check php -v from cmd

  2. Go to your environment variables by right clicking on PC and properties

  3. Check path of your php key, it might be pointing to older version.

0

This is for Windows Users. Make the below changes for both php.ini files in C:/xampp and the file in Program Files, which was created while installing PHP.

Uncomment the below line (by removing the ; sign before the line)

extension=pdo_sqlite

And add the below line, if not present.

extension=php_pdo_mysql.dll
0

If you have tried all the methods above, try this:

Check the PHP used by the terminal by typing php -v, then try to check the PHP used by your XAMPP by entering the XAMPP shell then typing php -v.

If there is a difference, then edit the PHP path in the system environment variable, and navigate to the PHP file used by XAMPP, Usually "c:\xampp\php".

Try to check if the PHP Terminal version with PHP XAMPP is the same, if so, then your problem should be solved

0

I'm using postgres with Laravel. I had to install php-pgsql. Since I'm on fedora, the commend for me is

sudo dnf install php-pgsql

Not the answer you're looking for? Browse other questions tagged or ask your own question.