使用sequelize连接mysql数据库出错

本文介绍了一种常见的Sequelize连接MySQL时遇到的认证错误及其解决方案,包括更改root用户的认证方式并重新配置Sequelize连接设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bug背景:使用sequelize连接mysql数据库

错误信息如下:大概意思是数据库验证未通过

Unhandled rejection SequelizeConnectionError: Client does not support authentication protocol requested by server; consider upgrading MySQL client

C:\Users\Administrator>mysql -u root -p

Enter password: ******

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 18

Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use db

Database changed

mysql> alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1

23456' ;

Query OK, 0 rows affected (0.06 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.03 sec)

 

 

done!

贴js

const Sequelize = require('sequelize');
const sequelize = new Sequelize('db', 'root', '123456', {
  host: 'localhost',
  dialect: 'mysql',

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },

  // SQLite only
  //storage: 'path/to/database.sqlite',

  // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
  operatorsAliases: false
});

const User = sequelize.define('user', {
  username: Sequelize.STRING,
  birthday: Sequelize.DATE
});

sequelize.sync()
  .then(() => User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  }))
  .then(jane => {
    console.log(jane.toJSON());
  });
F:\node_wp\express_wp\myapp\db>node test.js
Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER NOT NULL auto_increment , `username` VARCHAR(255), `birthday` DATETIME, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `users`
Executing (default): INSERT INTO `users` (`id`,`username`,`birthday`,`createdAt`,`updatedAt`) VALUES
(DEFAULT,'janedoe','1980-07-19 16:00:00','2018-07-25 05:31:33','2018-07-25 05:31:33');
{ id: 1,
  username: 'janedoe',
  birthday: 1980-07-19T16:00:00.000Z,
  updatedAt: 2018-07-25T05:31:33.750Z,
  createdAt: 2018-07-25T05:31:33.750Z }

 

转载于:https://my.oschina.net/quattro/blog/1862783

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值