LoginSignup
1

More than 1 year has passed since last update.

【初心者】MAMPのphpMyAdminでLaravelのDBのmigrateができない時に試した事【解決済み】

Last updated at Posted at 2021-06-16

はじめに

当記事の内容は表題のとおりです。
ローカル(MAMP)でLaravelアプリケーションを立ち上げた時、マイグレーション(migration)ができなかったのでその対処を記録します。
また、私は初学者なので誤っている点がありましたらご指摘いただけますと幸いです。

開発環境

macOS 11.2.2
MAMP(Apache/PHP version:7.4.12)
PHP 8.0.6
Laravel Framework 8.46.0

起こった事象

Laravelにて新規アプリケーションを立ち上げ

モデルを作成

phpMyAdmin上で使用するDBを作成

.envファイルを編集

マイグレーション実行

下記のエラー

% php artisan migrate

 Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1049] Unknown database 'phpMyAdminで作成したDB名' (SQL: select * from information_schema.tables where table_schema = mydbs and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕ 

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

行った対処

.envの内容を修正

マニュアルの手順どおりの記載だとエラーが発生したため下記のように変更しました。

.env
変更前(エラーになる状態)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=phpMyAdminで作成したDB名
DB_USERNAME=root
# DB_PASSWORD=

 下記に変更

変更後(migrationできる)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=phpMyAdminで作成したDB名
DB_USERNAME=root
DB_PASSWORD=root

記載を変更した箇所についてはローカルのMAMPのトップ画面(http://localhost:8888/MAMP/) を参照しました。
(DB_HOST=127.0.0.1127.0.0.1はlocalhostを指す)
MAMP_top.png

マイグレーションを再度実行

# マイグレーション前にキャッシュをクリア
% php artisan config:cache

Configuration cache cleared!
Configuration cached successfully!

# マイグレーションを実行
% php artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (75.91ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (63.72ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (68.31ms)
Migrating: 2021_06_16_021409_create_articles_table
Migrated:  2021_06_16_021409_create_articles_table (32.20ms)

↑のように今度はマイグレーションが正しく実行できました。

おわりに

同じような環境でつまずいている方のヒントになれば幸いです。
間違っている点がありましたらお知らせください。

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