nodejs mysql ER_NOT_SUPPORTED_AUTH_MODE

1. 问题描述

本地使用docker-compose启动了一个mysql服务, 默认使用image tag 是latest, yaml文件以下:html

## 本地开发所需资源文件
version: '3'
services:
  ### mysql (https://hub.docker.com/_/mysql/)
  db_mysql:
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1qaz
      TZ: Asia/Shanghai
    image: mysql
    ports:
      - 3306:3306
    restart: always

nodejs 程序使用typeorm和以下配置连接本地mysql:node

DB_TYPE=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=1qaz
DB_DATABASE=test
DB_SYNC=true

启动程序报告错误:mysql

{ Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    at Handshake.Sequence._packetToError (/usr/src/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
... ...

问题定义: mysql client 连接 mysql server 出现权限验证方面错误git

2. 问题调查

根本缘由: mysql的client驱动当前仅支持 mysql_native_password/mysql_old_password 鉴权模式, 而5.7后默认的鉴权模式是: plugin, mysql提供两个插件:安全

  • sha256_password: Implements basic SHA-256 authentication
  • caching_sha2_password: Implements SHA-256 authentication (like sha256_password), but uses caching on the server side for better performance and has additional features for wider applicability

mysql8.0默认用的是: caching_sha2_password而不是mysql_native_password, 咱们的解决方案则是修改root用户鉴权模式从caching_sha2_password为传统的mysql_native_password.app

经过查询用户信息:socket

mysql> select Host,User,plugin,authentication_string from user where User='root'\G
*************************** 1. row ***************************
                 Host: localhost
                 User: root
               plugin: auth_socket
authentication_string: 
1 row in set (0.00 sec)

本质上应该使得client driver支持auth_socket.

3. 解决方案

方案一: 连接到mysql server, 执行以下命令:

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

方案二: 设置docker启动命令, 采用native password

db_mysql:
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1qaz
      TZ: Asia/Shanghai
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - 3306:3306
    restart: always

重启nodejs服务没有错误了, 问题解决!

4 .参考资料

相关文章
相关标签/搜索