4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PHP/Laravel】マイグレート時に発生したエラー解決

Posted at

#はじめに
Laravelでアプリ開発をしております。
環境構築が完了し、いざマイグレートをする段階で今回のエラーが発生しました。
備忘のために記録しております。
同じような方の助けになれば幸いです。

#開発環境
・MacOS:10.14.6 (Mojave)
・PHP7.3
・Laravel6

#発生したエラー

$ php artisan migrate
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > yes


   Illuminate\Database\QueryException  : SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge' (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')

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

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'")
      /Users/ユーザーネーム/projects/stg/8001-laravel/phpsample/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=forge", "forge", "", [])
      /Users/ユーザーネーム/projects/stg/8001-laravel/phpsample/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

#解決方法
発生したエラーより、指定してるデータベースにアクセスができないとのこと。
これは「/config/database.php」の修正してやることで、指定できます。
私の場合は以下の「修正」あたりをMAMPの設定に合わせました。

database.php
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'), /*修正*/
            'port' => env('DB_PORT', '3306'), /*修正*/
            'database' => env('DB_DATABASE', 'php_sample_db'), /*修正*/
            'username' => env('DB_USERNAME', 'root'), /*修正*/
            'password' => env('DB_PASSWORD', 'root'), /*修正*/
            'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'), /*修正*/
            'charset' => 'utf8', /*修正*/
            'collation' => 'utf8_general_ci', /*修正*/
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false, /*修正*/
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

 
これで再度マイグレートを実行すると、無事に通りました。

$ php artisan migrate
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > yes

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.04 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.02 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.01 seconds)
Migrating: 2020_08_02_063050_create_categories_table
Migrated:  2020_08_02_063050_create_categories_table (0.01 seconds)
Migrating: 2020_08_02_063128_create_shops_table
Migrated:  2020_08_02_063128_create_shops_table (0.01 seconds)

#参考文献
以下の記事を参考にさせていただき、解決できました。
ありがとうございました。

本件に限らず、見やすいですし理解もしやすいのでオススメです。

PHPフレームワークのLaravelララベルをつかおう

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?