LoginSignup
62
54

More than 5 years have passed since last update.

Laravel での「php artisan migrate」ができないとき、解決法

Posted at

環境

ローカル環境にlravel環境を作っている


PHP 7.3.2
mysql 8.0.15
osx10.12
Laravel Framework 5.8.5

やろうとしたこと

users,password_resetsのテーブルを
 マイグレーションしようとしたらエラーが出ました。

users,password_resetsテーブルは、laravel newしたときに、作られています。

2014_10_12_00000000_create_users_table.rb
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

2014_10_12_00000000_create_password_resets_table..rb
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('password_resets');
    }
}

エラーの内容


$ php artisan migrate

 Illuminate\Database\QueryException  : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations)

スクリーンショット 2019-03-23 18.33.23.png

スクリーンショット 2019-03-23 19.24.43.png

エラーの結論

結局laravelとmysqlの接続ができてないこと
具体的に言うと
 laravelが指定したmysqlデータベース名、user名が
 自分のmysqlにないことです

解決法

データベースを作る

ここではmysqlでデータベースを作ります。

手順としては以下の通りです。

  • mysql8.0以降では、mysql5.6と少し違うので、注意してください

①mysqlインストールする
②mysqlでユーザー名、rootで入り、同時にpassword設定する
③ユーザーrootの中で
  ユーザー名forgeを作る(同時にパスワードを作る)
④ユーザーrootの中で
  ユーザー名forgeにデータベースを作る権限を与える。
⑤ユーザー名rootを抜ける
⑥ユーザー名forgeで入り、データベースforgeを作る。
⑦mysqlのユーザー承認方式を変える

mysqlについて知らない方は以下を参考してください
https://blog.codecamp.jp/what_is_mysql

①mysqlインストールする


mysqlインストール
$brew install mysql

mysqlを起動
$mysql.server start

②mysqlでユーザー名、rootで入り、同時にpassword設定する


$ mysql -uroot -p(パスワード設定するためにパスワードを書く)


 したいパスワード abcd1234

 $ mysql -uroot -pabacd1234

③ユーザーrootの中でユーザー名forgeを作る(同時にパスワードを作る)

rootユーザーは、なんでもできる権限のため、別にユーザーを作ります。

ユーザー名 forge
パスワード Mt@rvy5ii3

mysql> create user 'forge' identified by 'Mt@rvy5ii3';
Query OK, 0 rows affected (0.01 sec)

上が出た成功

*パスワードの注意

 LOW ポリシーは、パスワードの長さのみテストします。
パスワードは少なくとも 8 文字の長さでなければなりません。

MEDIUM ポリシーは、パスワードが最低 1 つの数値文字を含み、1 つの小文字および
大文字を含み、1 つの特殊文字 (英数字以外) を含む必要があるという条件を追加します。

STRONG ポリシーは、パスワードの 4 文字以上の部分文字列が、(辞書ファイルが指定された場合に)
辞書ファイル内の単語と一致してはならないという条件を追加します。

④ユーザーrootの中で

  ユーザー名forgeにデータベースを作る権限を与える。

grant all on forge. * to 'forge';

⑤ユーザー名rootを抜ける

qiita.rb
> exit;

⑥ユーザー名forgeで入り、データベースforgeを作る。


ターミナルからユーザーforgeに入る
$mysql -uforge -pMt@rvy5ii3;
 mysql -uユーザー名 -pパスワード

データベースforgeを作る。
$create database forge;

⑦mysqlのユーザー承認方式を変える

LaravelがMySQLに接続するために、
MySQL上のユーザ認証に指定された方式に変える。

alter user 'forge' identified with mysql_native_password by 'pMt@rvy5ii3';

laravelの設定

mysqlのユーザー名、パスワード、データベースの名前をlaravelに教える。

ディレクトリ直下にある.envファイルを開く

DB_DATABASE,DB_USERNAME,DB_PASSWORDを以下のように変える。

プロジェクト名/.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge(自分のデータベース)
DB_USERNAME=forge(自分のユーザ名)
DB_PASSWORD=pMt@rvy5ii3(自分のパスワード)

最後に

以下のようにmigrateできた完成

 $ php artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table


参考文献

エラー解決のやり方
https://teratail.com/questions/154105

mysql8.0の変更点
https://www.sejuku.net/blog/82303#i

mysqlインストールした方
https://qiita.com/hkusu/items/cda3e8461e7a46ecf25d

mysqlのパスワードルール
https://qiita.com/hermannsw/items/b4da81201d69cefb080b

mysql8のmigrationのエラーについて
https://takakisan.com/laravel-mysql-8-migration-error-fix/

62
54
1

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
62
54