错误:ER_NOT_SUPPORTED_AUTH_MODE:客户端不支持服务器请求的认证协议;考虑升级MySQL客户端。

3

我正在尝试使用以下代码连接到MySQL数据库。

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'pass',
    database : 'my_db',
    insecureAuth : true
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
    if (error) throw error;
    console.log('The solution is: ', results[0].solution);
});

connection.end();

但是我得到了以下错误:

错误:ER_NOT_SUPPORTED_AUTH_MODE:客户端不支持服务器请求的认证协议;考虑升级MySQL客户端

我在本地安装了MySQL-installer-community-8.0.11.0。

我需要安装其他连接器才能连接到数据库吗?

6个回答

5

我正在使用https://hub.docker.com/r/mysql/mysql-server/

文档中说:

MYSQL_ONETIME_PASSWORD:当变量为true(这是默认状态,除非设置了MYSQL_ROOT_PASSWORD或将MYSQL_ALLOW_EMPTY_PASSWORD设置为true),则root用户的密码将被设置为过期,必须在MySQL正常使用之前进行更改。此变量仅支持MySQL 5.6及更高版本。

尝试为您的docker环境设置MYSQL_ROOT_PASSWORD。您还可能需要更改用户的认证模式。

> mysql -u root -p

ALTER USER 'username'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

这对我有用。

编辑: 要检查设置了什么身份验证:

SELECT user, plugin FROM mysql.user WHERE user = 'username';

4
我昨天遇到了与我的Docker容器中的MySQL + Node.js相同的问题。 到目前为止,我还没有找到解决方法,但这与新的MySQL版本(8.X)有关。 在我重新安装了版本为5的MySQL之后,一切都正常工作了。

可以确认。我也是。 - steven35

4

如果我们在MySQL(8.x)安装期间选择MySQL(5.x)的传统密码加密方式,则MySQL(8.x)连接正常。但是,如果我在安装期间选择MySQL(8.x)的强密码加密方式,则无法正常工作。


你可能需要在这里修正一下语法。在第二个语句中,你是不是想说如果选择MySQL(5.x)的传统密码加密方式就无法工作? - Alwaysblue
如果我们选择MySQL(5.x)的传统密码加密方式,它是可以工作的。 - Manoj

2
我也曾经遇到过这个问题,以下解决方案对我很有效:

我使用了 "最初的回答",并成功解决了这个问题。

  1. Go to your MySQL workbench and click on your current database.
  2. In the Navigator window (on the left), click Users and Privileges.
  3. Click Add Account at the bottom of the user list.
  4. Give it another login name (e.g. node_test).
  5. Set the Authentication type to Standard.
  6. Set Limit to Host Matching to localhost.
  7. Set a new password (e.g. coding-is-awesome).
  8. Here's what your resulting code should look like:

    var mysql = require("mysql");
    
    var con = mysql.createConnection({
      host: "localhost",
      user: "node_test",
      password: "coding-is-awesome"
    });
    
    con.connect(function(err) {
      if (err) {
        console.log("Failed to connect");
        throw err;
      } else {
        console.log("Connected!");
      }
    });
    

0

尝试使用MySQL2 npm包

import mysql2 from 'mysql2';


0

我建议您尝试删除 insecureAuth : true 这一行,然后再试一次。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接