5
1

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でcaching_sha2_passwordのエラーが出る

Last updated at Posted at 2019-08-14

一年前に開発したプログラムを改修するため、新しい開発環境にMySQLを入れてソースを実行した。するとDB接続ができず、以下のエラーが出た。
Authentication method 'caching_sha2_password' not supported by any of the available plugins.
MySQLの最新版を導入したため、(MySQL 8.0以降)デフォルトの認証方式が変わっていた。ソースは古い方式で認証しようとしているため、エラーが起こったらしい。
結論としては、MySQLの認証設定を変更し、解決した。phpでも同様の問題が起こるが、MySQL側の設定を変えるという解決方法は同じ。

環境

  • OS:Windows10 64bit
  • IDE:Visual Studio Community 2019 v16.2.1
    .NET Framework v4.7.03056
  • DB:MySQL Community Server v8.0.17

問題の状況

以下が今回のソース。Connection.Openにおいてコケてしまうことが確認できる。

dao.cs

using MySql.Data.MySqlClient;

namespace TEST
{
    class MySQLDao
    {
        MySqlCommand cmd;

        internal void Test()
        {
            using (var conn = new MySqlConnection("userid=user; password=hoge; database=test; Host=localhost"))
            {
                cmd = new MySqlCommand()
                {
                    Connection = conn
                };
                try
                {
                    cmd.Connection.Open();
                }
                catch(Exception ex)
                {

                }
            }
        }
    }
}

ライブラリの更新では解決せず

対策としては、ソース側のMySQLへの接続方式を新しいものにするか、DB側の認証を旧方式にするかの二通りが考えられる。
まずは前者の対策を試みる。MySQLライブラリを更新してみた。Visual Studio のNuGetパッケージマネージャーから使用しているライブラリを調べると、バージョン8.0.17がリリースされているので、導入する。今回の問題が発生したMySQLのバージョンが8.0なので、ライブラリのバージョン名からすると対応されているように思えるが…。
hoge.PNG
新しいライブラリに必要な.NET Frameworkのバージョンは4.5.2以上。既存のプログラムは4.5を対象にしていたため、プロジェクトのプロパティから4.5.2に変更する。
hoge2.PNG
しかし、上記の結果、エラーは解消しなかった。

MySQL認証設定の変更

そこで、DB側の認証を旧方式にすることにした。MySQL WorkbenchのAdministrationタブからOption Fileを選択すると、Securityタブのなかに『default_authentication_plugin』という項目があり、『cashing_sha2_password』が設定されていた。これを『mysql_native_password』に変更する。Applyをクリックすると確認ダイアログが出るのでYesを選択する。
hoge3.PNG
なお、MySQLの設定ファイルはWindowsの場合、以下のようなインストール先フォルダ内にある。
C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
GUIを使わない場合は、default_authentication_pluginの値を書き換えればOK。

my.ini
# The default authentication plugin to be used when connecting to the server
default_authentication_plugin=mysql_native_password

新しくユーザーを作り直し、Authentication Typeを『Standard』にする。
hoge4.PNG
これでエラーは出なくなる。

5
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?