Laravel +Vagrant + CentOS環境でphp artisan migrate時のcould not find driverエラーの解決方法


事象

could not find driver、ドライバーがないよと怒られる。

経緯としては、Homesteadでの環境構築の利用ではないLaravel環境構築後にて(CentOS利用)Todoアプリを作成中php artisan migrate実行時に以下のエラー出現

$ php artisan migrate

   Illuminate\Database\QueryException  : could not find driver (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at /vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   PDOException::("could not find driver")
      /vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

環境情報

name version
CentOS 7.5.1804
Vagrant 2.2.6
VirtualBox 6.0.16
Laravel 6.12.0
PHP 7.4
Windows 10

※2020/02/16時点

原因

PHPのモジュールにMySQLのドライバ(php-mysqlnd)が入っていなかった。

私の凡ミスだがPHPモジュールにMySQLのドライバを入れ忘れていた。

解決策

1.モジュール確認

PHPにMySQLのドライバがいるか確認
pdo_mysqlがいればOK

$ php -m | grep pdo
pdo_sqlite

sqlite用のドライバしかいない

2.php-mysqlndをインストール

yum installにて PHPのMySQLドライバ、『php-mysqlnd』をインストール
※remiリポジトリ使っていたのでremiもインストール

$ sudo yum -y install --enablerepo=remi,remi-php74 php-mysqlnd
Loaded plugins: fastestmirror
~~~~~~~以下省略~~~~~~~

3.確認

$ php -m | grep pdo
pdo_mysql
pdo_sqlite

確認できたのでApacheを再起動

$ sudo service httpd restart

artisanコマンド実行

$ php artisan migrate
Migration table created successfully.
Migrating: 2020_02_15_150504_create_posts_table
Migrated:  2020_02_15_150504_create_posts_table (0 seconds)

解決

結論

MySQLをインストール時、PHPのMySQLモジュールのインストールも忘れずに

参考サイト

【Laravel】CentOSのLAMP環境でcould not find driver