0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:MySQLエラー「Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server」について

Last updated at Posted at 2025-03-31

はじめに

MySQLに外部から接続しようとした際、アクセスが拒否される「Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server」というエラーが表示されることがあります。

個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

本記事では、その原因と対処法について、自分用に整理します。


書こうと思ったきっかけ

Docker環境や外部ネットワークからMySQLにアクセスしようとしたときに、何度かこのエラーに遭遇。毎回ググって解決するのが手間だったため、手順とポイントを備忘録としてまとめておくことにしました。


実際のエラーメッセージ

mysql.connector.errors.DatabaseError: 1130: Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server

このエラーは、指定したホスト(IPアドレス)からの接続がMySQLサーバーに許可されていないことを意味します。


解決方法

方法1: MySQL にホストを許可する

MySQLユーザーに特定のホストからの接続を許可するSQLコマンドを実行します。

-- 'myuser' というユーザーに外部ホストからの接続を許可
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'xxx.xxx.xxx.xxx' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;

すべてのホストを許可する場合(非推奨)

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;

※ セキュリティ上、'%'での全開放は避け、必要なIPだけを許可することが望ましいです。


方法2: MySQL の設定ファイルの確認

my.cnfbind-address を確認。

[mysqld]
bind-address = 0.0.0.0  # 全てのアドレスからの接続を許可

設定変更後は、MySQLを再起動します:

sudo systemctl restart mysql

方法3: Docker環境での接続確認

Docker Composeで構築している場合、ホスト名にはコンテナのサービス名を指定する必要があります。

mysql.connector.connect(
    host='db',  # サービス名で指定
    user='myuser',
    password='your_password',
    database='mydatabase'
)

Dockerネットワークでは、サービス名がDNS名として使用されます。


セキュリティの注意点

  • % での全開放はなるべく避け、IP制限を活用する
  • パスワードは強力なものを使う
  • 不要なユーザー・権限は削除しておく

まとめ

MySQLの1130エラーは、接続元IPや設定ファイル、ユーザー定義など複数の要因が絡み合って発生します。焦らず、順を追って設定を確認することでスムーズに解決できます。

本記事を自分用のチェックリストとして活用し、今後の環境構築の効率化に役立てていきます!

0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?