LoginSignup
2
1

More than 3 years have passed since last update.

ゼロから始めるPHP ~Laravelでmigrationに失敗する時の解決方法の1例~

Last updated at Posted at 2019-05-19

Laravel掲示板を作成する段階でmigration出来ないという自体に遭遇したので、メモがてら記事にしておきます。

LaravelがMySQL8に接続できない

接続できない原因

MySQL8ではデフォルトの認証方式(plugin)が「caching_sha2_password」になっています。
Laravelはこの認証方式では接続できない為、Pluginの方式を「mysql_native_password」に変更してやる必要があります。

以下のターミナルの画面で確認してみてください。

変更前

mysql> select user, host, plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

現状がどうなっているかを確認しました。
全てのユーザーの認証方式が「caching_sha2_password」になっていますね。
これではLaravelからMySQL8へ接続ができません。

認証方式を変更する

MySQLの認証方式を「caching_sha2_password」から「mysql_native_password」へ変更していきます。

MySQL8.0 認証方式変更手順

①サーバースタート

mysql.server start

②サーバー接続

mysql -uroot -p

ユーザー「root」のパスワードを入力してください。
(設定した覚えがなければ恐らくそのままEnterいけます。)

③「ユーザー」(root@localhost)の認証方式を確認

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '*****';

ALTER USERで認証方式の変更を行います。

④「ユーザー」(root@localhost)の認証方式が変更できているか確認

select user, host, plugin from mysql.user;

結果

mysql> select user, host, plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

変更後はuser:rootのpluginが「mysql_native_password」に変更されています。
これでMySQL8とlaravelが接続可能になり、Migrationが行えます。
参考記事:MySQL8.0 認証方式を変更する(Laravel5)
@ucan-lab

データベースがすでに存在する

("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'posts' already exists")
このエラーが発生した理由は私がすでにLaravel掲示板を作ってマイグレーションに失敗したが、原因が分からずファイルを破棄して再度、Laravelプロジェクトを作り直した為です。
その時にdrop databaseでデータベースの削除をしておけばよかったのですがしていませんでした。

エラーの内容

MySQLにすでに同じ名前のデータベースが存在するよ!!

$ php artisan migrate
Migrating: 2019_05_14_181304_create_posts_table

   Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'posts' already exists (SQL: create table `posts` (`id` int unsigned not null auto_increment primary key, `title` varchar(50) not null, `body` text not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

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

  Exception trace:

  1   PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'posts' already exists")
      /Users/owner/karavel-bbs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  2   PDOStatement::execute()
      /Users/owner/karavel-bbs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  Please use the argument -v to see more details.

解決方法

データベースの確認 ⇨ データベースの削除 ⇨ データベースの再作成 
この手順で行いました。
①サーバースタート

mysql.server start

②サーバー接続

mysql -uroot -p

③データベースの確認

show databases;

④該当データベースの削除

drop database {該当のデータベース名};

⑤データベースの再作成

create database {該当のデータベース名};

ここでデータベースの名前は.envファイルを一致させなくてはいけないので、データベース作成したら.envファイルも確認しましょう!

<最後に>
migrationができなくて2日ほど調べて悩んで、プロジェクトを作っては消しを繰り返して行く中でやっと解決に繋がりました。
誰かのお役に立てれば幸いです。

2
1
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
2
1