LoginSignup
3
7

More than 3 years have passed since last update.

EC2にデプロイ時、php artisan migrateができないエラーの対処法

Last updated at Posted at 2020-05-16

LaravelをEC2にデプロイ作業の中でphp artisan migrateを実行しようとするとエラーが出てしまってめっちゃハマってしまったのでメモ兼共有。

php artisan migrateが実行できない

エラー内容
[ec2-user@ip-172-31-36-11 laravel]$ 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: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|

  • Database name seems incorrect: You're using the default database name `laravel`. This database does not exist.

    Edit the `.env` file and use the correct database name in the `DB_DATABASE` key. 
    https://laravel.com/docs/master/database#configuration

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

DBとLaravel側で連携ができていないと予想。
よくよく考えたらSQLサーバーはインストールしていなかったのが原因なのかなって考えました。

Qiitaの記事を参考にSQLサーバーであるMariaDBをインストールしました。Mysqlでの派生のものでMysqlを実行できるとのことだったので。
LaravelのローカルでのDB連携でも行ったように.envと同じ名前のDBがないとLaravelとDBの連携ができません。そのため私の場合はMariaDB内にLaravelというDBを用意しました。

[ec2-user@ip-172-31-36-11 laravel]$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 247
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| laravel            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

これで準備完了です。いざ

migrate実行
[ec2-user@ip-172-31-36-11 laravel]$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

  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|

      +11 vendor frames 
  12  database/migrations/2014_10_12_000000_create_users_table.php:24
      Illuminate\Support\Facades\Facade::__callStatic()

      +22 vendor frames 
  35  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

1つのファイルだけmigrateできているのでとりあえず実行はできていることは確認。エラー文コピって調べていると下記のサイトを見つけました。
https://helog.jp/laravel/migrate-sql-error/
このサイトの通りに文を追記しました。
使用しているMariaDBのバージョンが古いとエラーが起きるそうです。
再度実行すると。。。

[ec2-user@ip-172-31-36-11 laravel]$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException 

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(191) not null, `email` varchar(191) not null, `email_verified_at` timestamp null, `password` varchar(191) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  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|

      +11 vendor frames 
  12  database/migrations/2014_10_12_000000_create_users_table.php:24
      Illuminate\Support\Facades\Facade::__callStatic()

      +22 vendor frames 
  35  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

またしてもエラー、、、しかしこれはローカルでのLaravel開発時に嫌という程見てきたエラーなのですぐに対処できました。
すでにusersテーブルがあるからmigrateできないとのこと。そのためusersテーブルを削除してあげる必要があります。

$ mysql -u root -p
$ use laravel;
$ drop table users;

これで大丈夫なはず。migrateを邪魔しているusersテーブルを削除しました。コード間違っていたらごめんなさい。
migrate実行したら見事いけました!!

しかし、、、、Laravelの画面にはこんなエラーが、、、
スクリーンショット 2020-05-14 13.10.01.png
これはWebサーバーの再起動で消えました。
晴れてデプロイ作業が完了しました。

備考

EC2にMysql実行環境があってもLaravelプロジェクト側にMysql実行環境がないとmigrateはできません。
そのためLaravel側にもMySQL実行できる環境(ドライバ)をインストールしてあげる必要があります。

Mysqlのドライバ入れた
$ sudo yum install php74-php-mysqlnd.x86_64

上記のコマンドを実行していないと下記のphpinfo()PDO driversにはsqliteの記述しかないはずなのでMysqlコマンドが実行されません。気をつけてください。
自分はこれに気づくのに一時間くらいかかりました。

phpinfo()
PDO

PDO support => enabled
PDO drivers => mysql, sqlite
3
7
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
3
7