Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL 如何更改用户身份验证插件 #26

Open
JasonWu73 opened this issue Feb 2, 2020 · 0 comments
Open

MySQL 如何更改用户身份验证插件 #26

JasonWu73 opened this issue Feb 2, 2020 · 0 comments
Labels
Q & A 解决问题

Comments

@JasonWu73
Copy link
Owner

问题重现

运行一个简单的 Node.js 程序,测试下我们 MySQL 服务器的连通性:

const express = require('express');
const mysql= require('mysql');

const app = express();

const db = mysql.createConnection({
    host: '192.168.2.173',
    port: '3306',
    user: 'root',
    password: 'root123',
    database: 'mysql'
});

db.connect();

app.get('/users', (req, res) => {
    const sql = 'SELECT User, Host, Plugin FROM user';

    db.query(sql, (err, result) => {
        if (err) throw err;
        res.send(result);
    });
});

app.listen(5000, () => console.log('服务已启动'));

如果我们使用的是 MySQL 8.0.4 及以后的版本,且在没有进行额外配置的情况下,不出意外地我们会得到以下的错误:

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

这是因为从 MySQL 8.0.4 开始,MySQL 服务器的默认身份验证插件从 mysql_native_password 更改为了 caching_sha2_password,而我们使用的 MySQL 客户端还不支持新版的身份验证插件。所以一个比较简单的解决方案就是将用户的身份验证插件改回 mysql_native_password

问题解决

查看用户所使用的身份验证插件:

mysql> SELECT User, Host, Plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | Plugin                |
+------------------+-----------+-----------------------+
| root             | %         | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

上面的结果正验证了我们的猜测,下面我们就将 root 用户的身份验证插件修改为 mysql_native_password

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root123';

比较好的做法是创建非 root 用户,并指定相关权限。这里,我为了测试的简单,直接使用 root 用户。

然后再执行我们的 Node.js 代码,访问 http://localhost:5000/users,就可以得到正确结果了。

参考链接

@JasonWu73 JasonWu73 added the Q & A 解决问题 label Feb 2, 2020
@JasonWu73 JasonWu73 changed the title ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client MySQL 8 身份验证插件 Feb 2, 2020
@JasonWu73 JasonWu73 changed the title MySQL 8 身份验证插件 MySQL 如何更改用户身份验证插件 Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Q & A 解决问题
Projects
None yet
Development

No branches or pull requests

1 participant