LoginSignup
29

More than 3 years have passed since last update.

Laravelのphp artisan migrateでSQLSTATE[HY000] [2002] Connection refusedが返る

Last updated at Posted at 2019-04-08

エラーメッセージ

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

  at /Users/username/code/LaravelSample/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")
      /Users/username/code/LaravelSample/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])
      /Users/username/code/LaravelSample/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

環境

homesteadを入れたばかり

原因の目星

PDOが例外を返していることから、PDOの引数が原因のように思われます。この引数をコントロールしている.envの設定がおかしいのではないか?と目星をつけて調べていきます。

原因を調べる

.envファイルの下記の行がPDOの引数と対応しているようです。

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

mysqlにログインして登録されたデータを出してみると下記の結果になりました。

1.vagrant sshしてログイン
2.MySQLにユーザー名rootでログイン(パスワードは未設定)

 mysql -u root

この状態で事前に登録したデータがきちんと出せることを確認してみます。

bash
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| homestead          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from homestead.people;
+----+---------------+----------------------------+-----+---------------------+---------------------+
| id | name          | mail                       | age | created_at          | updated_at          |
+----+---------------+----------------------------+-----+---------------------+---------------------+
|  1 | updatedname01 | updatedmail01@mail.co.jp   |  33 | 2018-01-01 00:00:00 | 2019-04-08 07:06:33 |
|  2 | hanako        | hanako@hanako.jp           |  64 | 2018-01-01 00:00:00 | NULL                |
|  3 | sachiko       | sachiko@sachiko.jp         |  48 | 2018-01-01 00:00:00 | NULL                |
|  4 | sampleName01  | sampleaddress01@mail.co.jp |  11 | 2019-04-08 06:40:27 | 2019-04-08 06:40:27 |
+----+---------------+----------------------------+-----+---------------------+---------------------+

登録されたデータが表示されました。つまりこの状態でログインできればmigrationも成功するだろうと予想できます。

MySQLにログインした際のusernameとpasswordは.envファイルのものと異なっている事がわかります。.envファイルの設定をこれに合わせればクリアできそうです。今MySQLにはユーザとしてrootしか登録されていない状態で、rootにパスワードは未設定なので、まずrootのパスワードをsecretに変更します。

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user SET authentication_string=password('secret') WHERE user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 0  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye

変更したパスワードでログインできるか確認します。

vagrant@homestead:~$ mysql -u root -p
Enter password: 
(secretと入力しEnter)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 80
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

パスワードがきちんと変わっている事が確認できました。
.envファイルのユーザも変更します。

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret

これで.envファイルの設定と、MySQLにログイン出来た際の設定を合わせることができました。この状態で再度php artisan migrate でイケるか!?と思いましたが、冒頭のエラーが出てダメでした。PDOの引数が原因ではないようです。

もうちょい調べてみる

調べていたところ、migrateをプロジェクトフォルダに対して行なっていた事が判明。homestead内で行なわなければ意味がない!というわけでvagrant sshでログインして再度migrateしてみます。


vagrant@homestead:~$ cd ~/code
vagrant@homestead:~/code$ php artisan migrate
Migrating: 2019_04_08_072752_create_boards_table
Migrated:  2019_04_08_072752_create_boards_table
vagrant@homestead:~/code$ 

通りました!

結論

php artisan migrateはvagrant sshにてログインして行う必要アリ。

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
29