LoginSignup
3
2

More than 3 years have passed since last update.

Laravel マイグレーションで詰まった話

Posted at

目的

初めてPHPのLaravelを使ってアプリケーションを作成しました。
1日解決出来なかったエラーがあったので解決に至った経緯をまとめます。

エラー内容

・マイグレーション時のエラー画面

% php artisan migrate

   Illuminate\Database\QueryException 

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

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
    674▕         // If an exception occurs when attempting to run a query, we'll format the error
    675▕         // message to include the bindings with SQL, which will make this exception a
    676▕         // lot more helpful to the developer instead of just the database's errors.
    677▕         catch (Exception $e) {
  ➜ 678▕             throw new QueryException(
    679▕                 $query, $this->prepareBindings($bindings), $e
    680▕             );
    681▕         }
    682▕ 

      +33 vendor frames 
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

エラー原因と結論

・.envファイルを変更したが、サーバーに反映出来ていなかった。

・mysql.sockというMySQLソケットに関するファイルがうまく読み込まれていなかった。

解決法

まず.envファイルを以下のように変更

.envファイルの修正前

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=root
DB_USERNAME=root
DB_PASSWORD=  //mysqlにログインした時のパスワードを入力しました。
DB_SOCKET=/var/run/mysqld/mysqld.sock

修正後の.envファイル

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=photos-project //データベース名に合わせて変更
DB_USERNAME=root
DB_PASSWORD=  //mysqlにログインした時のパスワードを入力しました。
DB_SOCKET=/tmp/mysql.sock //socketを調べて入力

DB_SOCKETについては、下記の方法で調べました。
まずmysqlにログイン。
下記コマンドを実行しstockの部分を.envファイルにも記述する。

mysql> show variables like '%sock%';
+-----------------------------------------+-----------------+
| Variable_name                           | Value           |
+-----------------------------------------+-----------------+
| performance_schema_max_socket_classes   | 10              |
| performance_schema_max_socket_instances | 322             |
| socket                                  | /tmp/mysql.sock |
+-----------------------------------------+-----------------+
3 rows in set (0.00 sec)

.envファイルは正しく入力できたと思う。
% php artisan migrate マイグレーションを実行するが同じエラーが出る。

サーバーで入力内容が正しく反映しているか確認する為に下記のコードを入力。

php artisan tinker

データベースのデフォルト結果などアプリケーションの中身を確認することができるみたいです。
早速確認。

% php artisan tinker
Psy Shell v0.10.6 (PHP 7.3.11 — cli) by Justin Hileman
 config('database.default');
=> mysql
 config('http//database.connetions.mysql.host');
=> null
 config('database.connections.mysql.port');
=> 3306
 config('database.connections.mysql.database');
=> root
 config('database.connections.mysql.username');
=> root
 config('database.connections.mysql.host');
=> localhost
 exit

config('database.connections.mysql.database');だけ変更前のrootという結果になっている!!
.envファイルは修正しているのでどうするか分からず調べると、下記のコマンドを入力しマイグレーションすると出来たという記事を発見。

php artisan config:clear

上記のコマンドを入力を入力し再度tinkerにて変更できているか確認。
変更出来てた!!

php artisan tinker      
Psy Shell v0.10.6 (PHP 7.3.11 — cli) by Justin Hileman
>>> config('database.connections.mysql.database');
=> "photos-project"

その後マイグレーションを行うとやっと無事にマイグレーション出来ました。

 php artisan migrate     
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (54.89ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (57.46ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (49.70ms)
Migrating: 2021_02_02_050953_create_photos_table
Migrated:  2021_02_02_050953_create_photos_table (25.12ms)

最後に

プログラミング初心者です。初めてQiitaを書かせていただきました。エラーが解決したのが嬉しくて勢いで記事を書いたので読みにくいと思います。ご了承ください。
自分自身が今後同じことを繰り返さないように今後も継続的に投稿していきたいと思っております。

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