【Laravel + Docker】migration で could not find driver が出たときの対応

Laravel + Dockerの環境を構築し、Laravel導入時から入っているmigrationを実行したところ could not find driver が出てデータベース接続に失敗したのでその際の解決方法をまとめます。

なお、データベースはDockerコンテナにMySQLを作成しています。

環境について

Docker

docker-compose.yml

環境はウェブサーバー、PHP、データベースでそれぞれコンテナを立てています。

version: '3'
services:
  web:
    image: nginx:1.15.6
    ports:
      - "8000:80"
    depends_on:
      - app
    volumes:
      - ./docker/web/default.conf:/etc/nginx/conf.d/default.conf
      - .:/var/www/html
  app:
    image: php:7.3-fpm
    depends_on:
      - mysql
    volumes:
      - .:/var/www/html
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: test
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
volumes:
  mysql-data:

migrationエラーについて

セットアップ時に入っているmigration

Laravelにはセットアップした段階で以下のように3つのmigrationファイルが入っているのでこれを実行してみます。

migrationを実行する

$ php artisan migrate --seed

   Illuminate\Database\QueryException 

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

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

      +33 vendor frames 
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

解決方法について

php-mysql ライブラリをインストールする

コンテナの中で実行したりといろいろ試しましたが、結局は実行する環境がコンテナの外からなのでPC自体にライブラリが入っていなければだめでした。ということでphp-mysqlをインストールします。

$ sudo apt-get install php-mysql
[sudo] user のパスワード: 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
… 省略 …
Creating config file /etc/php/7.4/mods-available/pdo_mysql.ini with new version
php-mysql (2:7.4+75) を設定しています ...
php7.4-cli (7.4.3-4ubuntu2.4) のトリガを処理しています ...

再度migrationを実行する

$ php artisan migrate --seed
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (523.17ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (376.66ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (452.52ms)
Database seeding completed successfully.

確認

$ docker exec -it laravel-project_mysql_1 bash
root@c97930100445:/# mysql -u user -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_test  |
+-----------------+
| failed_jobs     |
| migrations      |
| password_resets |
| users           |
+-----------------+
4 rows in set (0.00 sec)

コメント

このブログの人気の投稿

docker-compose up で proxyconnect tcp: dial tcp: lookup proxy.example.com: no such host

docker-compose で起動したweb、MySQLに接続できない事象

【PHP】PHP_CodeSnifferを使う(コーディングルールのカスタマイズ)