LoginSignup
1
0

sail artisan migrateをしたら、migrateエラー(下から読む方が良いかもです)

Last updated at Posted at 2023-07-25

環境
macOS Ventura
Laravel Framework 10.15.0
Docker version 24.0.2, build cb74dfc
mysql Ver 8.0.32 for macos13.0 on x86_64 (Homebrew)↑Laravel sailを使っているので現在は関係ない

現象
sail artisan migrateをしたら、
UnexpectedValueException

There is no existing directory at "/Users/ss/Documents/Laravel-Project/Laravel-Project/storage/logs" and it could not be created: Permission denied

直訳
予期しない例外の値

「/Users/ss/Documents/Laravel-Project/Laravel-Project/storage/logs」に既存のディレクトリが存在しないため、作成できませんでした: アクセス許可が拒否されました

やりたいこと、行ったこと
php artisan make:model Post -mでPostモデルを作り、その後、sail artisan migrateをする。
その際にエラーが発生。

Laravelのマイグレーションファイルの内容をデータベースに反映させたい。

キャッシュのクリア

$ php artisan cache:clear
$ php artisan route:clear
$ php artisan config:clear

ディレクトリを確認すると、/storage/logsが、存在したので権限の付与をしました。

chmod 777 storage/logs

再度、

sail artisan migrate

すると、

Illuminate\Database\QueryException

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: select * from information_schema.tables where table_schema = laravel_project and table_name = migrations and table_type = 'BASE TABLE')

エラー発生。

直訳
イルミネート\データベース\QueryException

SQLSTATE[HY000] [2002] そのようなファイルまたはディレクトリはありません (接続: mysql、SQL: select * from information_schema.tables where table_schema = laravel_project and table_name = migrations and table_type = 'BASE TABLE')

データベースにテーブルmigrationsが存在しないことを意味します。

このエラーは、次のいずれかの原因らしい。
Laravelプロジェクトのマイグレーションファイルがまだ作成されていない。(作成済みのはず)
Laravelプロジェクトのマイグレーションファイルがまだデータベースに反映されていない。(データベースに反映できない。。。)

データベースに反映されないので

php artisan migrate:fresh

エラー
Dropping all tables .............................................................................. 42ms FAIL
Illuminate\Database\QueryException

SQLSTATE[HY000] [1045] Access denied for user 'sail'@'localhost' (using password: YES) (Connection: mysql, SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

直訳
イルミネート\データベース\QueryException

SQLSTATE[HY000] [1045] ユーザー 'sail'@'localhost' のアクセスが拒否されました (パスワードを使用: YES) (接続: mysql、SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

原因
MySQLのユーザー名とパスワードが正しくないか、MySQLデータベースに接続するための権限がない。
Laravelの環境変数にMySQLのユーザー名とパスワードが正しく設定されていない。
原因探し中であります。

ここで一旦、作業の仕方を変更しました。(アドバイスをいただきました)

・Macで使用しているMySQLをとめる。(MacとDockerのポートの競合を防ぐため。ずっとMacのMySQLで作業していたため。本来は、Laravel sailのMySQLで作業しないといけない。)
・sail mysqlで入る

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| laravel_project    |
| performance_schema |
| testing            |
+--------------------+
4 rows in set (0.01 sec)

phpMyAdominと同じ表示になりました!

今回の問題は、sail artisan migrateができないことでした

エラー文

sail artisan migrate

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: select * from information_schema.tables where table_schema = laravel_project and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:795
    791▕         // If an exception occurs when attempting to run a query, we'll format the error
    792▕         // message to include the bindings with SQL, which will make this exception a
    793▕         // lot more helpful to the developer instead of just the database's errors.
    794▕         catch (Exception $e) {
  ➜ 795▕             throw new QueryException(
    796▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    797▕             );
    798▕         }
    799▕     }

      +38 vendor frames

  39  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

直訳
SQLSTATE[HY000] [2002] そのようなファイルまたはディレクトリはありません (接続: mysql、SQL: select * from information_schema.tables where table_schema = laravel_project and table_name = migrations and table_type = 'BASE TABLE')

ここの時点で、あるはずのファイルがなぜ、ありませんと表示されるのか?しっかり考えるべきでした。

表示されないということは、接続されていない?
接続関係はどこにあるのか?
環境変数、MySQL

.envファイルのDB_HOSTが原因だった。

Before

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=todo
DB_USERNAME=
DB_PASSWORD=

After

DB_CONNECTION=mysql
DB_HOST=mysql  #localhost
DB_PORT=3306
DB_DATABASE=todo
DB_USERNAME=root
DB_PASSWORD=

成功!

 sail artisan migrate

   INFO  Running migrations.

参考にさせてもらいました!

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