Laravelのmigrate実行時に「Host ‘XX.XX.XXX.X’ is not allowed to connect to this MySQL server」エラーが発生

はじめに

Laravelのmigrate実行時に、下記エラーが発生しました。

$ php artisan migrate         

  Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1130] Host 'XX.XX.XXX.X' is not allowed to connect to this MySQL server (SQL: select * from information_schema.tables where table_schema = SampleTable and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675|

 

ポートの確認

まずは、ポートが開放されているかどうか確認をおこなってみます。

実際にDB接続元はMacの環境であり、接続先はCentOS+mysql8.0。

$ nc -v -w 1 XX.XX.XXX.XX -z 3306
Connection to XX.XX.XXX.XX port 3306 [tcp/mysql] succeeded!

問題なくポートが空いていることは確認できました。

エラー内容から見るに、接続許可されていませんということでしたので、今度はユーザを確認してみます。

 

すべての端末から接続可能ユーザの作成

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

ここで、すべての端末から接続可能なユーザを作成します。

mysql> CREATE USER 'admin'@'%' IDENTIFIED BY 'P@ssw0rd';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL ON *.* TO 'admin'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host from mysql.user where user = 'admin';
+-------+------+
| user  | host |
+-------+------+
| admin | %    |
+-------+------+
1 row in set (0.00 sec)

さて、問題なく追加されたので、Laravel開発環境にて再度、migrateを実行してみます。

$ php artisan migrate
Migrating: 2020_05_09_130409_create_sample_table
Migrated:  2020_05_09_130409_create_sample_table (0.01 seconds)

問題なく実行できました。

 

最後に

初期状態だと、localhostからのみのユーザしか作成されて居ないので、外部から接続できるユーザを作成する必要があります。

ここでちょっとはまりました。

 

タイトルとURLをコピーしました