2
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?

【(Mac) Docker × MySQL × MyBatis】MyBatisと連携させたアプリケーションから、コンテナ内のDBへ接続する(③番外編)

Last updated at Posted at 2025-06-29

はじめに

本記事を作成するに至った経緯、およびDocker Desktopのインストール〜コンテナを作成するまでは「①コンテナ作成編」に、MyBatisと連携させたJavaアプリケーションからコンテナ内のDBへ接続する手順は「②コンテナ接続編」に記載しております。

本編(③番外編)では、コンテナ作成時にログに出力された警告メッセージを紐解いていきます。

【番外編】 ログを確認

①コンテナ作成編で、コンテナとDBが作成されたことは確認できましたが、-dオプションを付与してバックグラウンドで実行したため、念のためログを確認しておきます。

  • docker logs {docker-compose.ymlで指定したコンテナ名}でログを取得
Terminal
## docker logs {コンテナ名}
$ docker logs break_spell_db

いくつかWarning(警告)が出ていたので内容を確認してみます。

log
[Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
[Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
[Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
[Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
[Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.

まずはこれから見てみます。

log
[Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.

内容:
'--skip-host-cache'は非推奨で、将来的に使えなくなるよ。
代わりに'SET GLOBAL host_cache_size=0'を使ってね。

'--skip-hos-cache'はdocker-compose.ymlにもinit.sqlにも記載しておらず、MySQLのコンテナイメージに記載されているようです。
調べたところ、すでにGitHub上では当該Warningに関する修正をcommitしてくださっているようですが、今回使用したイメージには反映されていないのかもしれません。


次はこのふたつ。
log
[Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
[Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'

内容:
・'default_authentication_plugin'は非推奨で、将来的に使えなくなるよ。
 代わりに'authentication_policy'を使ってね。
・'mysql_native_password'は非推奨で、将来的に使えなくなるよ。
 代わりに'caching_sha2_password'を使ってね。

docker-compose.ymlに記載した下記の部分が非推奨だったようです。
今はこのままでも動きますが、将来的に使えなくなるということなので、修正します。

docker-compose.yml
# 非推奨とのことなので削除
command: --default-authentication-plugin=mysql_native_password

'mysql_native_password'と'caching_sha2_password'はいずれもMySQLの認証方法のことで、'mysql_native_password'はMySQL5.7以前のデフォルト認証方式だったようです。
代わりに使ってねと提示された 'caching_sha2_password'はMySQL8.0以降のデフォルトの認証方式のため、明示的に指定する必要はありませんが、指定するとしたらこうでしょうか。

init.sql
SET GLOBAL authentication_policy = 'caching_sha2_password';

上記の構文はMySQL8.2以降でのみ有効です。今回使用したバージョン(8.0.42)では無視されますが、将来的な互換性を考慮して記載しています。


次はこちら。

log
[Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

内容:
パスワードなしでrootが作られちゃうよ!
'--initialize-insecure option'をオフにすることを検討してみては?

MySQLは、起動時にデータディレクトリが存在しない場合、サーバーでディレクトリを作成し、「--initialize-insecure」を使って初期化処理を行う仕様になっているようです。
rootパスワードを指定している場合は、ちゃんとMySQLがdocker-entrypoint.sh内の処理として、よしなにやってくれているようなので問題なさそうです。
[参考]
https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh

最後はこのふたつ。

log
[Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
[Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.

内容:
・証明書が自己署名証明書になってるよ。
・pidファイルディレクトリがOS上の全ユーザーからアクセスできるようになってるよ。
 違うディレクトリを選択してみては?

MySQL8.0ではデータディレクトリに証明書がない場合、自己署名証明書が生成される仕組みになっているため、この警告が出たようです。
自己署名証明書とは、認証局(CA)を介さずに、発行者自身が自分自身を証明するために作成したデジタル証明書のことです。信頼された第三者機関による検証がないため、セキュリティリスクがあると言われています。

今回のプロジェクトは個人開発かつローカル環境で実行するため、いずれも問題なさそうです。

おわりに

以上、「①コンテナ作成編」、「②コンテナ接続編」、「③番外編」と3回にわたり、MyBatisと連携させたアプリケーションから、コンテナ内のDBへ接続する方法について、自身がつまずいたところを中心にまとめました。


最後までお読みいただきありがとうございました!

2
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
2
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?