Help us understand the problem. What is going on with this article?

LaravelのDB設定

More than 1 year has passed since last update.

本日php artisan migrateでエラーが発生

$ php artisan migrate:refresh

 Illuminate\Database\QueryException  : SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

  at /Users/tky/Workspace/PHP/Laravel/tutorial/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[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)")
      /Users/tky/Workspace/PHP/Laravel/tutorial/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])
      /Users/tky/Workspace/PHP/Laravel/tutorial/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68

  Please use the argument -v to see more details.

DBの権限がどうのこうので、migrateできねぇっていうてはるんですかね?

DBもhomesteadのDBを使いますか?みたいになってるので明らか設定がまちがってるよね。って思ったのでDBの設定を変更していきます

Mysqlの設定変更

今回Mysqlを使っているのでMysqlの設定を変更していく
下記はデフォルトで設定されていた値

#デフォルトの値
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

homesteadでvagrantの仮想サーバー用の設定になっていたので
rootユーザー用の設定に変更

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorial
DB_USERNAME=root
DB_PASSWORD=

わざわざMysqlにアクセスしてtutorialって名前のDBを作ったけど、もしかしてmigrateした時点で作成された?

database.phpも設定変更していく

config/database.php
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'), #変更
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'tutorial'), #変更
            'username' => env('DB_USERNAME', 'root'), #変更
            'password' => env('DB_PASSWORD', ''), #変更
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

rootユーザーの設定にしたので
php artisan migrate実行

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
Migrating: 2018_05_18_120611_create_tasks_table
Migrated:  2018_05_18_120611_create_tasks_table

いけた!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away