Laravelで$php artisan migrateを打った時エラーが出ました。
ローカル環境で作業しています。
HPで参考サイトを見ながら作業をしています。
発生している問題・エラーメッセージ
Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user '〇〇〇'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = 〇〇〇 and table_name = migrations) at /Users/**/Desktop/PHP/Laravel/Project/〇〇〇/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 '〇〇〇'@'localhost' (using password: YES)") /Users/**/Desktop/PHP/Laravel/Project/〇〇〇/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68 2 PDO::__construct("mysql:unix_socket=/tmp/mysql.sock;dbname=〇〇〇", "〇〇〇", "〇〇〇", []) /Users/**/Desktop/PHP/Laravel/Project/〇〇〇/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68 Please use the argument -v to see more details.
該当のソースコード
2018_09_ _182535_create_posts_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('cat_id'); $table->text('content'); $table->unsignedInteger('comment_count'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
2018_09_ _182609_create_categories_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } }
2018_09_ _182620_create_comments_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCommentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('post_id'); $table->string('commenter'); $table->text('comment'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('comments'); } }
database.php
<?php return [ /* |-------------------------------------------------------------------------- | Default Database Connection Name |-------------------------------------------------------------------------- | | Here you may specify which of the database connections below you wish | to use as your default connection for all database work. Of course | you may use many connections at once using the Database library. | */ 'default' => env('DB_CONNECTION', 'mysql'), /* |-------------------------------------------------------------------------- | Database Connections |-------------------------------------------------------------------------- | | Here are each of the database connections setup for your application. | Of course, examples of configuring each database platform that is | supported by Laravel is shown below to make development simple. | | | All database work in Laravel is done through the PHP PDO facilities | so make sure you have the driver for your particular database of | choice installed on your machine before you begin development. | */ 'connections' => [ 'sqlite' => [ 'driver' => 'sqlite', 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', ], 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', '〇〇〇'), 'username' => env('DB_USERNAME', '□□□'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', '/tmp/mysql.sock'), 'charset' => 'utf8', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => 'InnoDB', ], ], /* |-------------------------------------------------------------------------- | Migration Repository Table |-------------------------------------------------------------------------- | | This table keeps track of all the migrations that have already run for | your application. Using this information, we can determine which of | the migrations on disk haven't actually been run in the database. | */ 'migrations' => 'migrations', /* |-------------------------------------------------------------------------- | Redis Databases |-------------------------------------------------------------------------- | | Redis is an open source, fast, and advanced key-value store that also | provides a richer set of commands than a typical key-value systems | such as APC or Memcached. Laravel makes it easy to dig right in. | */ 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_CACHE_DB', 1), ], ], ];
.esv
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=〇〇〇 DB_USERNAME=□□□ DB_PASSWORD=
.env.example
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=〇〇〇 DB_USERNAME=□□□ DB_PASSWORD=
試したこと
調べるときに「php Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user」と検索しました。
.サイトを見て、envの値を設定し直した結果が上記の「.esv」「.env.example」ですがエラーは変わりませんでした。
・mysql> show variables like '%sock%';
でコマンドをうち、socket を調べたところ「 /tmp/mysql.sock 」と表示され他ので、「'unix_socket' => env('DB_SOCKET', '/tmp/mysql.sock'),」と入れています
mysql> show variables like '%sock%'; +-----------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------+-----------------+ | performance_schema_max_socket_classes | 10 | | performance_schema_max_socket_instances | 322 | | socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+
・似たような事例があり、調べましたがエラーが解決しませんでした。
自分が理解しきれず、また調べ肩が悪いと思うのですが、ご教授お願いします。
*補足
・MAMPは使用していません。
・Laravelのバージョンは「Laravel Framework 5.7.6」です。
・〇〇〇は全て同じスペルです。
・□□□は全て同じスペルです。
・'password' => env('DB_PASSWORD', ''),パスワードは設定していません。
追記・修正依頼への返信
*mts10806さん
>失礼しました。使用はMacでローカルサーバーはHomebrewを使用しています。
また、PHP及びMySQLのバージョンは「PHP-> 7.1.16 」「MySQL->Ver 14.14 Distrib 5.7.21, for osx10.13」です。
参考サイトはhttps://manablog.org/laravel_bulletin_board/です。
DB名とユーザー名は変更しています。
ただ、「MySQLは最近のものであればパスワード設定必須」と言うのが明確な答えを見つけられすにいます。
*dyoshikawaさん
>失礼しました。記述ミスでして、ファイル名は「.env」です。