LoginSignup
24
22

More than 3 years have passed since last update.

【docker】db:createすると、Plugin caching_sha2_password could not be loaded...のエラーハマった話

Posted at

背景

railsで作成中のアプリに、開発環境にDockerを導入していました。

DBをcreateしようとするも下記エラー

コンテナのビルドは問題なく出来たので、次にDBを作成するべくこのコマンドを実行したら、下記のエラーに遭遇しました。

$ docker-compose run web bundle exec rake db:create


Creating network "golfscore_default" with the default driver
Creating golfscore_db_1 ... done
Creating golfscore_web_run ... done
Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
Couldn't create 'golfscore_development' database. Please check your configuration.
rake aborted!
Mysql2::Error::ConnectionError: Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory

原因と対策

Mysql 8以降、認証プラグインの仕様が変わったためのようです。
ユーザーのプラグインをmysql_native_passwordに変更していきます。

docker内のmysqlにログインし、plugin変更

まずはコンテナ内のMysqlにログインするため、コンテナIDを調べます。

$ docker ps                                            
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
bcc91696ca70   mysql:8.0.23   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   33060/tcp, 0.0.0.0:4306->3306/tcp   golfscore_db_1

コンテナIDがわかったので、入ります。

$ docker exec -it bcc91696ca70 bash

入れました。Mysqlに入ります。

root@bcc91696ca70:/# mysql -uroot -p

Mysqlのパスワードを入力します。

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

このコマンドを実行して、現在のプラグインの状況を確認します。

mysql> SELECT user, host, plugin FROM mysql.user; 

やはり、caching_sha2_passwordになっています。今回はこれが悪さしているようですので、変更していきます。
スクリーンショット 2021-03-29 5.53.45.png

下記、2コマンド実行して、root user部分の2箇所を変更します。
('password'部分は各自DBのパスワードを入力)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.01 sec)

変更できたか確認してみます。

mysql> SELECT user, host, plugin FROM mysql.user;

無事、mysql_native_passwordに変更されています。
スクリーンショット 2021-03-29 5.53.35.png

再度、DBを作成し、無事に成功しました。

$ docker-compose run web bundle exec rake db:create    
Creating golfscore_web_run ... done
Created database 'golfscore_development'
Created database 'golfscore_test'

参考

24
22
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
24
22