5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

マイグレーション実行時のcould not find driverの対処法

Posted at

はじめに

あくまでPHP(Laravel)のマイグレーション時に発生した問題であり、他の開発環境では対応などが異なる場合があります。

Docker開発環境
php:8.1-fpm
mysql:5.7.36

バージョン
PHP | 8.1.27
Laravel | 10.46.0
MYSQL | 5.7.36

問題

docker-composeで立てた、php-fpmのコンテナにログインしてマイグレーションを実行。

Command
docker-compose exec php bash 
php artisan migrate

以下の問題が発生した。MYSQLドライバーがインストールされていないっぽい。

  Illuminate\Database\QueryException 

  could not find driver (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'mysql_test_db' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕ 
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

      +36 vendor frames 

  37  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

そのためphp-mysqlのパッケージをインストールしようとするものの、「php-mysqlパッケージが見つからなかった」というエラーが発生した。

Command
root@c47d6af87d0a:/var/www/html/LaravelProject$ apt-get install php-mysql
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package php-mysql is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'php-mysql' has no installation candidate

解決方法

以下のPHP拡張機能をインストールするためのコマンドをphp-fpmのコンテナ内で実行。
mysqliは、PHP5.5以降で非推奨、7.0で削除となったmysql_系のメソッドの代替らしいです。

Command
docker-php-ext-install mysqli pdo_mysql

もしくはDockerfile内に上記のコマンドを追加すれば、ビルド時に実行される。

docker/php/Dockerfile
RUN apt-get update \
 && apt-get install -y zlib1g-dev mariadb-client vim libzip-dev nodejs npm \
 && docker-php-ext-install mysqli pdo_mysql \  #この行を追加
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

下記のような表示が最後のほうの行で確認できればインストールは成功

Build complete.
Don't forget to run 'make test'.

+ strip --strip-all modules/pdo_mysql.so
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20210902/
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o -o -name \*.dep | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la      modules/* libs/*
rm -f ext/opcache/jit/zend_jit_x86.c
rm -f ext/opcache/jit/zend_jit_arm64.c
rm -f ext/opcache/minilua

マイグレーションを再度実行して以下のように出力されれば解決。

root@4c276abfd82c:/var/www/html/LaravelProject$ php artisan migrate

   INFO  Preparing database.  

  Creating migration table ..................................................................................... 42ms DONE

   INFO  Running migrations.  

  2014_10_12_000000_create_users_table ......................................................................... 39ms DONE
  2014_10_12_100000_create_password_reset_tokens_table .......................................................... 7ms DONE
  2019_08_19_000000_create_failed_jobs_table ................................................................... 17ms DONE
  2019_12_14_000001_create_personal_access_tokens_table ........................................................ 17ms DONE

参考記事

php公式dockerイメージで使えるphp拡張を調べる
クイックリファレンス(PHP公式イメージ)
DockerでPHPとMySQLのシンプルすぎる環境構築を行う
mysqli の忘備録 -PDOとmysqliの違い-
MySQL 改良版拡張モジュール

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?