0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DockerでMySQLに接続する時に対応したエラー

Last updated at Posted at 2021-10-26

#問題1

###エラー1

Mysql2::Error: Host '172.22.0.1' is not allowed to connect to this MySQL server

#対処方法

ユーザーに、外部からのアクセス権を付与する 

###手順

1、ユーザーのhostを確認

select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| user              | localhost |
+------------------+-----------+

2、hostをlocalhost→%(ワイルドカード)に変更

CREATE USER 'user' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'user'@'%' WITH GRANT OPTION;

3、再度hostの状態を確認して%になっていたらOK

select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| user             | %         |
+------------------+-----------+

#問題2

###エラー2

Unable to connect to host 127.0.0.1, or the request timed out.

Be sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently 10 seconds).

MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found

#対処方法

caching_sha2_password -> mysql_native_password に変更

###手順

1、認証プラグインを確認

SELECT host, user, plugin FROM mysql.user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | user                 | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+

2、pluginをmysql_native_passwordに変更

ALTER USER 'user'@"localhost" IDENTIFIED WITH mysql_native_password BY 'password';

3、再度pluginの状態を確認してmysql_native_passwordになっていたらOK

SELECT host, user, plugin FROM mysql.user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | user                 | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+

#問題3

###エラー3

SQLSTATE[HY000] [2002] Connection refused

#対処方法

hostをコンテナ内のIPアドレスにする

同じDocker内の通信なのでlocalhost(127.0.0.1)でいいと思っていたのですが、コンテナでIPアドレスが異なるみたいです

###手順

1、DBコンテナに接続してIPアドレスを確認

hostname -i
root@b3179d7e8745:/# hostname -i
172.19.0.4

2、1で確認したhostを設定する

$db = new PDO('mysql:host=172.19.0.4;dbname=db_name', $user, $pass);
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?