LoginSignup
5
5

More than 5 years have passed since last update.

MySQL8.0新機能 (caching_sha2_password 認証プラグイン)を回避したい。

Posted at

MySQL8.0の新機能追加のおしらせ。

MySQL8.0で新規追加された認証プラグイン(caching_sha2_password)について確認します。

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

MySQL8.0を導入して最初につまずくのがこれです。

環境確認

caching_sha2_password 認証プラグインの確認方法
デフォルトに設定されている認証プラグインを確認します。

mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name                 | Value                 |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.00 sec)

この設定があると、Mysql8.0未満のMySQLクライアントからの接続に失敗したりします。

回避方法

1.デフォルト「mysql_native_password」に書き換える。
2.MySQLクライアントを8.0以上にする。
このあたりになりますが、プログラムの接続の場合、そもそも「caching_sha2_password」に対応してなかったりします。
Python3.6とかそうでした。(2019/02現在)

今回は「デフォルト「mysql_native_password」に書き換える。」についての手順を書きます。

my.cnfを設定する。ただしどこにあるかは環境によって異なるかもしれないため、下記を実行して設置候補から探す。

$ mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 

llコマンドでファイルを探していく

$ ll /etc/my.cnf
-rw-r--r-- 1 root root 1297 Nov 29 10:12 /etc/my.cnf
$ ll /etc/mysql/my.cnf
ls: cannot access /etc/mysql/my.conf: No such file or directory
$ ll /usr/etc/my.cnf
ls: cannot access /usr/etc/my.cnf: No such file or directory

みつかった、my.cnfを修正

$ sudo vi /etc/mysql/my.cnf
default_authentication_plugin= mysql_native_password
#上記を追加する。コメントアウトされている可能性大。

MySQLを再起動する。

$ sudo systemctl restart mysqld

これで設定は完了。

確認

mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name                 | Value                 |
+-------------------------------+-----------------------+
| default_authentication_plugin | mysql_native_password |
+-------------------------------+-----------------------+
1 row in set (0.00 sec)

無事、caching_sha2_password 認証をすることができました。

おまけ

Python3での接続確認コード。

connect_to_DB.py
import mysql.connector as db

def connect_to_DB():
    try:
        mydb = db.connect(
        host = "ec2-52-11-217-26.us-west-2.compute.amazonaws.com",
        user = "seifuen",
        passwd = "ibs3050",
        database = "seifuen",
        port = 3306,
        auth_plugin = 'mysql_native_password')

        print('DBへの通信に成功しました。')

    except Exception as e:
        print('[ERROR: catch_loop_video] DBへの通信に失敗しました {}'.format(e))
        return
    return mydb


connect_to_DB()
5
5
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
5
5