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?

【MySQL】レストア時に「Access denied for user 'root'@'localhost'」となる場合

Posted at

概要

Dockerコンテナで起動しているMysql8.0のDBをDBEAVERで開いて、レストアすると以下のエラーになりました。
一応解決できたので紹介します。

/opt/homebrew/Cellar/mysql/8.1.0/bin/mysql -u root --host=localhost --port=3304 -v sampledb Task 'MySQL restore' started at Thu Sep 05 14:49:35 JST 2024 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

原因と対策

DBeaverでDBにはアクセスできるし、rootユーザーの権限を確認したが問題なし。
どうしてレストア時のみ失敗するのだろう?

まず同じコマンドをターミナルからやってみる。

/opt/homebrew/Cellar/mysql/8.1.0/bin/mysql -u root --host=localhost --port=3304 -v sampledb -p

同じようにAccess deniedと出てきました。
普通にmysqlコマンドで入れば正常にログインできたので、おそらくレストア時に使っているローカルクライアントの/opt/homebrew/Cellar/mysql/8.1.0/bin/mysqlを使っていることで影響が出ているらしいと推測。

調べていると、localhostではなくて127.0.0.1に変更するとTCP/IPを明示的に使用できることがある、というのを見つけたので、127.0.0.1にして以下実行。

/opt/homebrew/Cellar/mysql/8.1.0/bin/mysql -u root --host=127.0.0.1 --port=3304 -v sampledb -p

するとmysqlにログインできました!

DBEAVERでも同様にDB設定でlocalhostから127.0.0.1としたら、レストアが正常に実行されるようになりました。

image.png

しかし、127.0.0.1にすることで解決したのはなぜなのだろう...

localhostはUNIXドメインソケットを使用して接続し、127.0.0.1(ループバックアドレスとも呼ばれる)はTCP/IPを使用して接続する、という違いがあるよう。

ローカルのMySQLの設定ファイルは以下のようになっていました。localhostを指定した場合でも、実際には127.0.0.1にリダイレクトされるため、TCP/IP接続が使用される...はず?

/opt/homebrew/etc/my.cnf
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
character-set-server = utf8mb4

[client]
default-character-set = utf8mb4

socket=/tmp/mysql.sockを追加してみましたが同じでした。
ソケットファイルあたりに原因があったのだろうか...?

真因がわかったら更新します。

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?