LoginSignup
0
0

More than 1 year has passed since last update.

Node.js 【MySQL 8.0 に接続できない。】

Posted at

Node.js 【MySQL 8.0 に接続できない。】

DBからデータ取得ができずに詰まったので備忘録。

起きたこと

  • node.jsのパッケージ使って、MySQL8.0に接続しようとするとエラー発生
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
エラー:ER_NOT_SUPPORTED_AUTH_MODE:クライアントはサーバーから要求された認証プロトコルをサポートしていません。 MySQLクライアントのアップグレードを検討してください

(Google翻訳)

原因

  • MySQL8.0のパスワード認証形式の違いが原因。
  • MySQL8.0の認証プラグイン
    • caching_sha2_password
  • (MySQL5.7までは、mysql_native_password
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.00 sec)

解決策

  • root(user), localhost(host)の plugin を変更。
    • caching_sha2_password → mysql_native_password
plugin変更
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
特権を更新
flush privileges;
確認
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)

その後、DBからデータ取得が取得できるようになりました。

おわりに。

こちらの記事
mysql2 をインストールするという手もあるようです。

npm install mysql2

caching_sha2_password の状態でも接続できるようです。

0
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
0
0