
前提・実現したいこと
Docker上でLaravelを用いたアプリ開発をしようとしています。
発生している問題・エラーメッセージ
下記の記事を元にDocker上でLaravelを動かすことはできたのですが、
https://qiita.com/A-Kira/items/1c55ef689c0f91420e81
次に下記記事に沿って、Laravelを触ってみようとしたところで詰まってしまいました。
https://qiita.com/sano1202/items/6021856b70e4f8d3dc3d
2.マイグレーションの実行の部分ですが、マイグレーションファイルを作成し、php artisan migrate
で、マイグレーションの実行を行ったところ、次のようなerrorが出ました。
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = database and table_name = migrations and table_type = 'BASE TABLE') at /var/www/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] [2002] Connection refused") /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=database", "root", "root", []) /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 Please use the argument -v to see more details.
※追記
マイグレーションファイルはこちらです。記事に従ってphp artisan make:migration create_books_table --create=books
で作成しました。
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateBooksTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('books', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 50); $table->integer('price'); $table->string('author', 50)->nullable(); $table->timestamps(); }); // Schema::create('books', function (Blueprint $table) { // $table->bigIncrements('id'); // $table->timestamps(); // }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('books'); } }
試したこと
現状、.envファイルのDB部分は、下記のようになっております。
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database DB_USERNAME=root DB_PASSWORD=root
docker exec -it db-host bash
でmySQLコンテナに入り、USERNAM/PASSWORDともにrootでログインしましたが、問題なくログインできます。
databaseというデータベースも存在してました。
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | database | | mysql | | performance_schema | | sys | +--------------------+
また、(関係ある試みか分からないですが)
1つ目の記事のdocker-compose.yamlファイル内で、MYSQL_USER: docker/MYSQL_PASSWORD: dockerも作っていたので、.envファイルを下記のように書き換えてphp artisan migrate
を実行しても同様のerrorでした。
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database DB_USERNAME=docker DB_PASSWORD=docker
補足情報(FW/ツールのバージョンなど)
Docker: 19.03.1
PHP: 7.2.21
Laravel: 5.8.31
MySQL:5.7.27


















回答1件
あなたの回答
tips
プレビュー