34

I have a Laravel 5.4 project on my Ubuntu 14.04 (VPS with Plesk 12.5.30). After creating the database and setted up the .env file with required information I ran php artisan migrate and exceptions were thrown:

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

[PDOException]
could not find driver

My .env file has:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=pmaramaldb

DB_USERNAME=user

DB_PASSWORD=password

I've followed most of the guides to solve this error and there wasn't any positive results:

Enabling Extension Solution:

PDO is enabled

The php.ini file is being generated automatically, I guess this is made by Plesk. At the start of the file it says:

; ATTENTION!
;
; DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
; SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.

So I've tried it anyway. After that I've restarted Apache and then tried again... Didn't work.

Installing php7.0-mysql Solution:

I've also tried to install MySQL in case is not:

user@server:/var/www/vhosts$ sudo apt-get install php7.0-mysql

Leyendo lista de paquetes... Hecho

Creando árbol de dependencias

Leyendo la información de estado... Hecho

php7.0-mysql is already the newest version.

0 actualizados, 0 se instalarán, 0 para eliminar y 67 no actualizados.

** SQL Lite Install Solution:**

I've also tried as question link says: sudo apt-get install php5-sqlite and the result was:

user@server:/var/www/vhosts/system/maramal.io/etc$ sudo apt-get install php5-sqlite

Leyendo lista de paquetes... Hecho

Creando árbol de dependencias

Leyendo la información de estado... Hecho

php5-sqlite is already the newest version.

0 actualizados, 0 se instalarán, 0 para eliminar y 67 no actualizados.

Composer Dump-autoload Solution:

It didn't work either. I've tried composer dump-autoload and it didn't work.

After ran the command php -i:

PDO

PDO support => enabled

PDO drivers =>

...

PHP Version => 5.6.30-1+deb.sury.org~trusty+1

Well, I am not sure if this has anything to do with the error, but the version shown by running php -v is:

user@server: ~/path$ php -v

PHP 5.6.30-1+deb.sury.org~trusty+1 (cli)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
Maramal
  • 3,145
  • 9
  • 46
  • 90
  • have you tried deleting the datbase & recreating it? – pseudoanime Mar 20 '17 at 16:55
  • @pseudoanime, of course. – Maramal Mar 20 '17 at 16:55
  • 2
    it reads like you do have taken the usual steps. time to double check some basics: did you perhaps set laravel up to use another db type, mssql or postgresql? is there a mixup of php versions on the server (i think ubuntu 14.04 uses php5.x per default) – cypherabe Mar 20 '17 at 16:58
  • 1
    @cypherabe, Actually Plesk lets you choose the supported PHP version that you want to use. The selected database in **.env** and **database.php** file is MySQL. Also the application is working fine (without the database). – Maramal Mar 20 '17 at 17:04
  • 2
    May be worth doing a `php -i` in the CLI to make sure that it matches the web based one. Sometimes you may get a different PHP version on the CLI than the one in apache. – apokryfos Mar 20 '17 at 17:10
  • 2
    hmm... another idea: if i understand the process correctly, the error occures in a php cli call. Did you see the answer by leith in the linked question? php-cli uses another ini file, maybe it could be that the pdo driver is not activated in that case – cypherabe Mar 20 '17 at 17:16
  • @apokryfos, I ran that and updated the original post. – Maramal Mar 20 '17 at 17:22
  • @cypherabe might be, but I only referred to the phpinfo() -> php.ini file. Shouldn't be that this is the only php.ini file? – Maramal Mar 20 '17 at 17:24
  • 2
    @Maramal best thing to do is do `which php` find the one that currently runs, delete it and link the PHP 7 binary in your `/usr/bin` or other path dir. Your current CLI php doesn't have any PDO drivers – apokryfos Mar 20 '17 at 17:24
  • @apokryfos, When did `which php` it says `/usr/bin`, so with what I should replace it? – Maramal Mar 20 '17 at 17:32
  • 2
    @Maramal try `mv /usr/bin/php /usr/bin/php.old` and then `ln -s /path/to/php/bin/php /usr/bin/php` Don't know where you have PHP installed though you may need to look for it (phpinfo may say where it is) – apokryfos Mar 20 '17 at 17:39
  • @apokryfos, It worked. I did that. **1.** renamed /usr/bin/php to /usr/bin/php.old **2.** `sudo ln -s /opt/plesk/php/7.0/bin/php /usr/bin/php` **3.** Restarted Apache Reply as an answer and I'll make it as the correct one. – Maramal Mar 20 '17 at 18:13
  • 1
    I think it's important to understand *why* replacing the php executable worked. It seems your PHP installation was a bit of a mess. Both PHP5 and PHP7 installed. You probably ran PHP5 (as evidenced by your `php -i`) but there was no `php5-mysql` (only the PHP7 version) – aross Mar 21 '17 at 09:34

13 Answers13

58

As an alternative solution be sure you have installed php7-mysql

try to run command below

sudo apt install php7.0-mysql

or, if you want to automatically install the current version

sudo apt install php-mysql
Prasun Jajodia
  • 470
  • 3
  • 15
İsmail Atkurt
  • 1,360
  • 1
  • 12
  • 17
24

Same problem, ubuntu 17.10, Laravel 5.5.

$ sudo apt install php7.1-pdo php7.1-mysql

enter image description here

enter image description here

A nice quick fix, hope this helps

Devin Norgarb
  • 1,119
  • 13
  • 18
19

There's a conflict between the PHP that is used by Apache and the PHP that is linked to the command line. (It happens more often that it should to be honest).

What is typically done is:

which php

This tells you which php will be expecuted when running in the command line. e.g. /usr/bin/php

mv /usr/bin/php /usr/bin/php.old

Then link or copy the correct PHP version to an executable path:

ln -s /path/to/php/bin/php /usr/bin/php

or the following should also work.

cp /path/to/php/bin/php /usr/bin/php

Also suggested if you want to be able to manually install mods:

ln -s /path/to/php/bin/phpize /usr/bin/phpize
ln -s /path/to/php/bin/php-config /usr/bin/php-config

This way your CLI will match your webserver.

Update:

If as noted in this answer if you are using Ubuntu with multiple alternative installations of PHP you can do:

sudo update-alternatives --set php /usr/bin/php<version>
sudo update-alternatives --set phar /usr/bin/phar<version>
sudo update-alternatives --set phar.phar /usr/bin/phar.phar<version> 
sudo update-alternatives --set phpize /usr/bin/phpize<version> 
sudo update-alternatives --set php-config /usr/bin/php-config<version>
apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • 1
    I made that changes and I got the same error yet. I just want to migrate my db schema, and it works on my Vagrant VM, but not on my Aruba VPS. I installed Debian 8, and latest php, mysql, phpmyadmin, composer, git, and then deployed my laravel project with Dploy npm package seamlessly. Then I made some changes on VPS because of gitignore, and I changed .env because of mysql. Then up and ran. Everything looked good. But when I wanted to migrate a simple table into my db, it dropped an error same as @Maramal asked. Your solution is not worked for me. – Zsolt Takács May 30 '17 at 11:11
  • 1
    `which php` just worked for me on Windows. I replaced `wamp server` with `nginx` and I can clearly see it is using the wamp PHP still. – agm1984 Jun 03 '18 at 05:50
  • 1
    My `composer.json` had `php8.0`, but when I upgraded all packages on Ubuntu and add `php` without specify a version, installed the newest at the moment (8.1), so Ubuntu used `php8.1` but Apache used `php8.0`. So I just made the movements to install `sudo apt-get install php8.0-mysql` specifying a version and that's it ‍ – Chris Apr 08 '22 at 19:21
10

I had the same scenario you have. I did resolve mine the following:

1) on your terminal, type

php --ini

this will output your current php.ini configuration file path.

2) edit php.ini using vim or your preferred editor.

vim /etc/php.ini

note that /etc/php.ini is your path file for your php.ini. it may be different on your machine.

3) find ;extension=php_pdo_mysql.dll and uncomment it by removing semicolon (;)

4) save your php.ini change/s

**5) restart the web server to apply the changes

sudo systemctl restart apache2

**6) Now run your command.

php artisan migrate

hope this helps for you.

Dinuka Thilanga
  • 4,220
  • 10
  • 56
  • 93
Arelancelot
  • 491
  • 3
  • 14
6

in linux do like that

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
4

You can try these following command

php artisan cache:clear
php artisan config:clear  

Then restart your apache server. After then reconfigure your .env file

Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
Farhad Hossen
  • 263
  • 1
  • 6
3

Your database driver is missing. To solve the probelem

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
abhishek subedi
  • 921
  • 7
  • 3
2

Ok I once experienced this error... I don't know why but it looks like in php 7.3 there is problem in relation to drivers or something there what I did was switch back to php 7.2 and everything worked fine...

  • 2
    Rolling back to php7.2 worked for me also running the following on my terminal: `sudo a2dismod php7.3; sudo a2enmod php7.2; sudo update-alternatives --set php /usr/bin/php7.1; sudo service apache2 restart` – Felix Runye Mar 24 '19 at 14:23
2
sudo apt install php7.2-pdo php7.2-mysql
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

clear artisan cache

php artisan cache:clear

and

php artisan migrate:refresh

that's it

Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
Fatih TÜZEN
  • 39
  • 1
  • 4
0

Actually, I'm pretty sure that you are not aware of your PHP - versions and used the wrong paths:

For your vendor PHP version ( 5.6 by sury.org ), you would use PHP - commands as "/usr/bin/php", but when you use different PHP - versions, as for example the ones from Plesk, then you have different paths:

/opt/plesk/php/5.2/bin/php -v
/opt/plesk/php/5.3/bin/php -v
/opt/plesk/php/5.4/bin/php -v
/opt/plesk/php/5.5/bin/php -v
/opt/plesk/php/5.6/bin/php -v
/opt/plesk/php/7.0/bin/php -v
/opt/plesk/php/7.1/bin/php -v

Your PHP - commands for possible additional PHP versions ( 7.0 and 7.1 if you installed the sury.org MAIN ppa PHP versions from https://launchpad.net/~ondrej/+archive/ubuntu/php ) would be:

/usr/bin/php7.0 -v
/usr/bin/php7.1 -v

Show the installed modules for each PHP version with the "-m" option string and use the "-i" option string for informations ( example ):

/opt/plesk/php/7.0/bin/php -m
/opt/plesk/php/7.0/bin/php -i
or
/usr/bin/php7.0 -m
/usr/bin/php7.0 -i

To use the "composer", you would certainly as well use the FULL path of your PHP executable ( example ):

/opt/plesk/php/7.0/bin/php composer-setup.php
or
/usr/bin/php7.0 composer-setup.php

Additional "composer" - commands are then:

/opt/plesk/php/7.0/bin/php composer.phar AND-ADDITIONAL-STRINGS/OPTIONS-TO-BE-INCLUDED
or
/opt/plesk/php/7.0/bin/php composer.phar AND-ADDITIONAL-STRINGS/OPTIONS-TO-BE-INCLUDED
UFHH01
  • 523
  • 4
  • 9
0

If you are using WSL2, with docker, and recently had an incident with switching db drivers, you could try:

docker ps (to retrieve your app image)
docker exec <app_code> php artisan cache:clear
docker exec <app_code> php artisan config:clear
Luis Gomes
  • 11
  • 1
-1

For windows check in your "C:\Program Files\PHP" or "C:\Program Files (x86)" for the php.ini file.

From there look ";" and remove it for:

extension=mysqli extension=pdo_mysql

If your using AMMPS or IIS you may need to restart the service.