LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

Mysql2::Error: Plugin caching_sha2_password could not be loaded: Error loading shared library lib/mariadb/plugin/caching_sha2_password.so: No such file or directory

Posted at

Dockerをmysql8系で構築しようとしたときにハマったメモ

※注意 まだ解決していません(解決次第修正します)

目的

Dockerでmysql8のエラー を解決する

Mysql2::Error: Plugin caching_sha2_password could not be loaded: Error loading shared library lib/mariadb/plugin/caching_sha2_password.so: No such file or directory

現状

mysqlも設定し認証方法を変更、5系であれば(ウシジマさんのアプリは)動くことを確認済み。
dockerにもcommand: --default-authentication-plugin=mysql_native_password
を追記済みで手詰まりなっている。

原因

ggると、どうやらmysqlの認証問題っぽい。

MySQL5.7までの認証プラグインには mysql_native_password がデフォルトで使用されていましたが、MySQL8.0より新たに追加された caching_sha2_password に変更されました。
SHA-256を使用した、より安全なパスワードの暗号化を提供するとともに、キャッシュを使用して同一ユーザの認証処理を高速化しようという、MySQL推奨の認証プラグインです。

MySQL8.0新機能 (caching_sha2_password 認証プラグイン)

既存のmsqlの認証方法を変更する(mysql側)

mysqlの認証方法を

caching_sha2_password から mysql_native_passwordに変更して上げれば良い。

mysqlにログイン

mysql --version
バージョン確認後、ログイン

mysql  Ver 8.0.18 for osx10.15 on x86_64 (Homebrew)
mysql -u root
mysql> SELECT user, host, plugin FROM mysql.user;

+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
4 rows in set (0.03 sec)

ほうほう、caching_sha2_passwordになっている。

認証方法変更

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '自分のmysqlパスワード';
Query OK, 0 rows affected (0.08 sec)

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

変更されたことが確認できる。この調子で、caching_sha2_password になっているものはmysql_native_password に変更する。

最後に、全部変わってることを確認する。

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | mysql_native_password |
| mysql.session    | localhost | mysql_native_password |
| mysql.sys        | localhost | mysql_native_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.01 sec)

新規ユーザー作成時の認証方式を変更

taiga@Taiga / % cd usr/local/etc
taiga@Taiga etc % ls
ImageMagick-7       gitconfig       my.cnf.default      openssl@1.1     redis-sentinel.conf unbound
bash_completion.d   my.cnf          openssl         pkcs11          redis.conf      wgetrc
taiga@Taiga etc % vim my.cnf
taiga@Taiga etc % pwd
/usr/local/etc

vimで入って、

[mysqld]
default_authentication_plugin=mysql_native_password

参考
MySQL8.0 認証方式を変更する(Laravel5)

dockerでの設定を変更する

注目スべきは、command: --default-authentication-plugin=mysql_native_password

docker-compose.yml
# docker-compose.yml
# 開発環境用のdocker-compose

#composefileのバージョン指定
version: '3.2'


services:
  # ズラッと各要素を並べる
  database:
    # databaseの内容を定義
    restart: always
    image: mysql:8.0.18
    ports:
      - 4306:3306
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-datavolume:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root

  app:
    # appの要素を定義
    build:
      context: .
      dockerfile: Dockerfile
    command: >
      bash -c "
        yarn install --check-files &&
        rm -f tmp/pids/server.pid &&
        bundle install --quiet &&
        bundle exec rails db:migrate:reset &&
        bundle exec rails s -p 4000 -b '0.0.0.0'
      "
    ports:
      - "4000:4000"
    volumes:
      - .:/runteq-basic
      - "bundle:/usr/local/bundle"
    depends_on:
      - database


volumes:
  # 指定したパスのディレクトリをコンテナ内に設置され、Dockerコンテナ内でローカルにあるファイルを参照することができる
  bundle:
    driver: local
    # volumeに使用するドライバ(動かすための接続先)の指定するためのkeyになり、そのvalueとしてlocalを指定することで、マウント機能を実現
  mysql-datavolume:
    driver: local

# Dockerfile
# docker_example_for_rails(Rails/MySQL)

# alpine 余計なライブラリが入っていないため、軽量
FROM  ruby:2.6.4-alpine

# Dockerfileのメタデータを記述、メンテナンスを誰がするかの意味合い
LABEL maintainer="Busitora<register@busitora.com>"

# 文字コードの設定 \ だと 改行で定義できるので便利。一つずつ定義すると記述が冗長になる
ENV LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    LC_CTYPE="utf-8"
# 環境変数
ENV APP="/runteq-basic" \
    CONTAINER_ROOT="./" \
    NOKOGIRI_OPTION="--use-system-libraries \
                    --with-xml2-config=/usr/bin/xml2-config \
                    --with-xslt-config=/usr/bin/xslt-config" \
    MYSQL_PORT=4306 \
    SERVER_PORT=4000

# ライブラリのインストール(追加したい場合は以下に追記)
# RUN ビルド実行コマンドで、外部からインストール
RUN apk update \
 && apk upgrade --no-cache \
 && apk add --update --no-cache \
        alpine-sdk \
      build-base \
        bash \
        imagemagick \
        jq \
        less \
        libgcrypt-dev \
        libxml2-dev \
      libxslt-dev \
        mariadb-dev \
        mysql-client \
        nodejs \
        tzdata \
        wget \
      xvfb \
        yaml-dev \
      yarn \
      zlib-dev \
 # 重めのgemを予めインストールしておく
 && gem install -q -N bundler \
 && gem install -q -N pkg-config \
 && gem install -q -N rails -v 5.2.3 \
 && gem install -q -N nokogiri -v 1.10.1 -- $NOKOGIRI_OPTION \
 && gem install -q -N mysql2 -v 0.4.10

# 実行するディレクトリの指定
WORKDIR $APP
COPY Gemfile Gemfile.lock $CONTAINER_ROOT
RUN bundle install --jobs=4 --retry=3
# RAILS_SERVE_STATIC_FILES=trueにすることで、rails serverを起動時にpublic/assetsを読み込む
ENV RAILS_SERVE_STATIC_FILES=true \
    PORT=$SERVER_PORT \
    TERM=xterm
EXPOSE $SERVER_PORT
EXPOSE $MYSQL_PORT

試してみる

docker-compose build
は成功

docker-compose up

更に試したこと

① dockerのimageやmysqlを削除して、入れ直す
docker images
で確認し、対象を docker rm で削除

しかし、エラー文は変わらず。
Mysql2::Error: Plugin caching_sha2_password could not be loaded: Error loading shared library lib/mariadb/plugin/caching_sha2_password.so: No such file or directoryで止まってしまう。

database_1  | 2020-02-06 03:15:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
database_1  | 2020-02-06 03:15:07+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
database_1  | 2020-02-06 03:15:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
database_1  | 2020-02-06T03:15:07.759576Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1  | 2020-02-06T03:15:07.759742Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 1
database_1  | 2020-02-06T03:15:08.198634Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
database_1  | 2020-02-06T03:15:08.203265Z 0 [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.
database_1  | 2020-02-06T03:15:08.241105Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
app_1       | yarn install v1.16.0
database_1  | 2020-02-06T03:15:08.397138Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
app_1       | [1/4] Resolving packages...
app_1       | success Already up-to-date.
app_1       | Done in 3.91s.
app_1       | rails aborted!
app_1       | Mysql2::Error: Plugin caching_sha2_password could not be loaded: Error loading shared library lib/mariadb/plugin/caching_sha2_password.so: No such file or directory
app_1       | /usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
app_1       | /usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:855:in `checkout_new_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:834:in `try_to_checkout_new_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:795:in `acquire_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:523:in `checkout'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1014:in `retrieve_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:57:in `check_protected_environments!'
app_1       | /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:13:in `block (2 levels) in <main>'
app_1       | /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
app_1       | /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:20:in `perform'
app_1       | /usr/local/bundle/gems/railties-5.2.3/lib/rails/command.rb:48:in `invoke'
app_1       | /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<main>'
app_1       | /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
app_1       | /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
app_1       | /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
app_1       | /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
app_1       | /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
app_1       | /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
app_1       | /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
app_1       | /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
app_1       | /runteq-basic/bin/rails:9:in `<top (required)>'
app_1       | /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
app_1       | /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
app_1       | /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
app_1       | /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
app_1       | /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
app_1       | /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
app_1       | /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
app_1       | /runteq-basic/bin/spring:15:in `require'
app_1       | /runteq-basic/bin/spring:15:in `<top (required)>'
app_1       | bin/rails:3:in `load'
app_1       | bin/rails:3:in `<main>'
app_1       | Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
app_1       | (See full trace by running task with --trace)
2_sakamototaiga_app_1 exited with code 1

仮説

ggると、ほぼ上記の内容になるので、vim.confの設定が間違っている?(一旦コメントアウトしてるのは戻せるようにするため)

vim.conf
# Default Homebrew MySQL server config
# [mysqld]
# Only allow connections from localhost
# bind-address = 127.0.0.1
# default_authentication_plugin=mysql_native_password
[mysqld]
default_authentication_plugin=mysql_native_password
1

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