artus90
artus90

Reputation: 1440

Laravel: PDOException: could not find driver

I am developing a website on a server I only have access to MySQL and FTP, so all commands I run are through the b374k php shell . I am experiencing a Laravel problem with SQL driver. I tried switching to file-hosted SQLite (in database/database.sqlite), but the thrown exception is the same as when using MySQL. The input & output of the console is as follows:

   php artisan migrate:refresh --seed
    [Illuminate\Database\QueryException]
could not find driver (SQL: select * from sqlite_master where type = 'table ' and name = migrations)
 [Doctrine\DBAL\Driver\PDOException]
could not find driver
 [PDOException]
could not find driver

The Apache extensions installed on the server are:

    /etc/php/5.6/apache2/conf.d/10-mysqlnd.ini, 
/etc/php/5.6/apache2/conf.d/10-opcache.ini,
 /etc/php/5.6/apache2/conf.d/10-pdo.ini,
 /etc/php/5.6/apache2/conf.d/15-xml.ini, 
/etc/php/5.6/apache2/conf.d/20-calendar.ini,
 /etc/php/5.6/apache2/conf.d/20-ctype.ini, 
/etc/php/5.6/apache2/conf.d/20-curl.ini,
 /etc/php/5.6/apache2/conf.d/20-dom.ini, 
/etc/php/5.6/apache2/conf.d/20-exif.ini, 
/etc/php/5.6/apache2/conf.d/20-fileinfo.ini, 
/etc/php/5.6/apache2/conf.d/20-ftp.ini, /etc/php/5.6/apache2/conf.d/20-gd.ini, 
/etc/php/5.6/apache2/conf.d/20-gettext.ini, 
/etc/php/5.6/apache2/conf.d/20-iconv.ini, 
/etc/php/5.6/apache2/conf.d/20-json.ini, 
/etc/php/5.6/apache2/conf.d/20-mbstring.ini, 
/etc/php/5.6/apache2/conf.d/20-mcrypt.ini, 
/etc/php/5.6/apache2/conf.d/20-mysql.ini, 
/etc/php/5.6/apache2/conf.d/20-mysqli.ini, 
/etc/php/5.6/apache2/conf.d/20-pdo_mysql.ini,
 /etc/php/5.6/apache2/conf.d/20-phar.ini, 
/etc/php/5.6/apache2/conf.d/20-posix.ini, 
/etc/php/5.6/apache2/conf.d/20-readline.ini, 
/etc/php/5.6/apache2/conf.d/20-shmop.ini, 
/etc/php/5.6/apache2/conf.d/20-simplexml.ini, 
/etc/php/5.6/apache2/conf.d/20-sockets.ini,
 /etc/php/5.6/apache2/conf.d/20-sysvmsg.ini,
 /etc/php/5.6/apache2/conf.d/20-sysvsem.ini,
 /etc/php/5.6/apache2/conf.d/20-sysvshm.ini,
 /etc/php/5.6/apache2/conf.d/20-tokenizer.ini, 
/etc/php/5.6/apache2/conf.d/20-wddx.ini,
 /etc/php/5.6/apache2/conf.d/20-xmlreader.ini, 
/etc/php/5.6/apache2/conf.d/20-xmlwriter.ini, 
/etc/php/5.6/apache2/conf.d/20-xsl.ini, 
/etc/php/5.6/apache2/conf.d/20-zip.ini

What can be the problem and how can I fix it? (I don't have access to root bash)

Upvotes: 126

Views: 512503

Answers (30)

Stan Smulders
Stan Smulders

Reputation: 6237

Even simpler in Ubuntu (18.04)

apt install php-mysql

Done. No need to edit any .ini files.

Upvotes: 60

Nibb
Nibb

Reputation: 2025

You might need to uncomment the following in your php.ini file.

;extension=pdo_mysql.so

Taken from this post: Laravel 5 PDOException Could Not Find Driver . I think I had to do something exactly like this when setting up laravel on digital ocean.

Upvotes: 158

MUHINDO
MUHINDO

Reputation: 1213

Make sure pdo_mysql extension is enabled on your cPanel

Upvotes: 0

Steferson Ferreira
Steferson Ferreira

Reputation: 361

Create a .env file.

if using laravel and composer, generate a key for project.

php artisan key:generate

Upvotes: 6

NoOorZ24
NoOorZ24

Reputation: 3222

Not really answer to original question but might help to someone:

When using docker you have to execute:

docker-compose exec [NAME_OF_DOCKER_SERVICE] php artisan migrate

Instead of only php artisan migrate

Just wanted to share this as I have made this error few times already

Upvotes: 3

Way Too Simple
Way Too Simple

Reputation: 305

This error happened to me using sail for development. In this case the solution was to run the command inside the container

docker exec -it <app-container-id> php artisan migrate

The app container id is probably like this:

<app-name>_laravel.test_1

Upvotes: 1

stilinski254
stilinski254

Reputation: 111

For php 8 running on laravel 8 (ubuntu), run sudo apt install php8.0.3-mysql then restart apache sudo service apache2 restart

Upvotes: 2

Nasser Ali Karimi
Nasser Ali Karimi

Reputation: 4663

In my case I the php path was different from xampp php path that my project hosted on.

So I just change the system path from the pure php path to the php path in xamppp and all working fine.

I run this command in Ubuntu

sudo ln -s /opt/lampp/bin/php /usr/bin/php

may you have installed the php so like me you need to first run this command and then try above again

sudo rm /usr/bin/php

May your composer has some issue so you need to run this as well

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer

Source: https://askubuntu.com/a/563993

Upvotes: 1

Vritika Malhotra
Vritika Malhotra

Reputation: 369

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

Upvotes: 2

Samad
Samad

Reputation: 1832

For sqlite on debian/ubuntu

PHP 7.1

sudo apt install php7.1-sqlite3

PHP 7.2

sudo apt install php7.2-sqlite3

PHP 7.3

sudo apt install php7.3-sqlite3

PHP 7.4

sudo apt install php7.4-sqlite3

Upvotes: 2

Henrique Sales
Henrique Sales

Reputation: 11

On Windows, I followed the same path as the previous answers. The difference is that the path to php.ini is usually different from the traditional ones, usually in Apache.

To find php.ini, I used the following path: C:\tools\php80\php.ini

After that, I uncommented the pdo extension files and the migration worked.

Upvotes: 1

Shashi PHP Dev
Shashi PHP Dev

Reputation: 21

For me, these steps worked in Ubuntu with PHP 7.3:

1. vi /etc/php/7.3/apache2/php.ini > uncomment:
  ;extension=pdo_sqlite
  ;extension=pdo_mysql
2. sudo apt-get install php7.3-sqlite3
3. sudo service apache2 restart

Upvotes: 2

Netwons
Netwons

Reputation: 1648

in ubuntu or windows

  • path php.ini

  • php -i enter image description here

  • 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

Upvotes: 1

jeti
jeti

Reputation: 1908

For me, php.ini was OK, but I needed to restart php artisan serve.

Upvotes: 1

Macedo_Montalv&#227;o
Macedo_Montalv&#227;o

Reputation: 579

You need to enable these extensions in the php.ini file

Before:

;extension=pdo_mysql
;extension=mysqli
;extension=pdo_sqlite
;extension=sqlite3

After:

extension=pdo_mysql
extension=mysqli
extension=pdo_sqlite
extension=sqlite3

It is advisable that you also activate the fileinfo extension, many packages require this.

Upvotes: 7

Vasu Naik
Vasu Naik

Reputation: 191

ERROR: could not find driver (SQL: select * from tests where slug = a limit 1)

I was getting the above error in my laravel project, i am using nginx server on ubuntu 16.04

This error is because, php-mysql driver is missing. To install it type following command.

sudo apt-get install php7.2-mysql

Please specify your current php version in above command.

Open php.ini file and uncomment the followling line of code(Remove Semicolon).

;extension=pdo_mysql

Then Restart the nginx and php service

sudo systemctl restart php7.2-fpm
sudo systemctl restart nginx

It worked for me.

Upvotes: 12

Deepesh Thapa
Deepesh Thapa

Reputation: 1789

Some Additional information for people who upgraded their php version.

This is for laravel framework using MySql or MariaDB.

I had issues after upgrading php version from 7.2 to 7.4.

  1. Make sure you completely install php extentions and packages for php version 7.4 (Or your installed php version).
  2. Make sure you enable these extentions. pdo_mysql, xmlrpc, mbstring, mysqli, openssl, fileinfo, gd2, curl at php.ini.
  3. Then clear cache, config
  4. Restart all services such as apache, supervisor

Upvotes: 1

rashedcs
rashedcs

Reputation: 3725

In Linux Download Mysql and Php

sudo apt install php-mysql

Then the problem will be solved.

Upvotes: 5

Kevin Mendez
Kevin Mendez

Reputation: 668

In case that you run test in sqlite run

sudo apt install sqlite3 && sudo apt install php-sqlite3

If you run test in window, you dont need install nothing. Only config your phpunit.xml

Upvotes: 2

Enos Okello
Enos Okello

Reputation: 121

I faced the same problem while using sqlite 3:

  1. Make sure the changes made on .env.example and the same on the .env file.
  2. Then run $ sudo apt-get install php-sqlite3 on terminal.
  3. Finally $ php artisan migrate.

Upvotes: 5

Ian Nato
Ian Nato

Reputation: 1037

I had a similar issue in Ubuntu 16.04 and what help me was that i installed php-mysql for php 7.2. I would recommend you run the following command if you have php 7.2 or install php mysql depending on your version of PHP. Make sure that you have installed the DBAL packege

apt-get install php7.2-mysql

systemctl restart apache2

Upvotes: 6

Vrushal Raut
Vrushal Raut

Reputation: 1208

In my case I'm using Ubuntu so I found that my system is not installed php-sqlite3, after installation its got fixed.

sudo apt-get install php7.2-sqlite3

Upvotes: 2

Muhammad
Muhammad

Reputation: 387

if you have installed new version of php 7.2+ then you have to uncomment this line in php.ini ;extension=pdo_mysql

Upvotes: 2

Radhe Shyam Sharma
Radhe Shyam Sharma

Reputation: 1019

Solution 1:

1. php -v

Output: PHP 7.3.11-1+ubuntu16.04.1+deb.sury.org+1 (cli)

2. sudo apt-get install php7.3-mysql

Solution 2:

Check your DB credentials like DB Name, DB User, DB Password

Upvotes: 16

Prodigy
Prodigy

Reputation: 380

You are probably missing sqlite or the specific DB driver you are trying to migrate.

Install the DB driver and you'd be fine. You don't necessarily need to edit your php.ini file (it's one way to fix it too though).

For sqlite do --> sudo apt-get install php-sqlite3 Then php artisan migrate

NB: I assume you have PHP and MySql already installed

Upvotes: 8

Chetam Okafor
Chetam Okafor

Reputation: 573

I had the same issue, and I uncomment extension=pdo_sqlite and ran the migration and everything worked fine.

Upvotes: 5

Donne
Donne

Reputation: 211

In CentOS7,I try this: yum install php-mysql, then edit php.ini

Upvotes: 2

chokelive
chokelive

Reputation: 11

In window OS. I have uncomment extension=pdo_mysql for php.ini file that locate at same directory of php.exe. after that it work fine.

Upvotes: 1

Mubashar Abbas
Mubashar Abbas

Reputation: 5663

I got the same error when running my test suite. It was working before but it stopped working after I upgraded php. So I did

sudo apt-get install php-sqlite3

and it worked.

Upvotes: 59

Nadim Tareq
Nadim Tareq

Reputation: 481

First check php -m
.If you don't see mysql driver install mysql sudo apt-cache search php-mysql
Your results will be similar to:

php-mysql - MySQL module for PHP [default]


install php- mysql Driver

sudo apt-get install php7.1-mysql

Upvotes: 16

Related Questions