使用sequelize连接mysql数据库出错

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sequelize是一个Node.js的ORM(Object-Relational Mapping)库,用于在Node.js环境中连接和操作关系数据库,其中也包括连接MySQL数据库。下面是使用Sequelize连接MySQL数据库的步骤: 1. 首先,需要确保已经在Node.js项目中安装了Sequelize库。可以通过在项目文件夹中运行以下命令来安装Sequelize: ``` npm install sequelize ``` 2. 接下来,在项目文件夹中创建一个用于连接数据库的配置文件,例如config.js。在该文件中,需要提供MySQL数据库的相关配置信息,包括数据库的名称、用户名、密码、主机和端口号等。以下是一个示例配置文件的代码: ```javascript module.exports = { development: { username: 'root', password: 'password', database: 'database', host: 'localhost', port: 3306, dialect: 'mysql' } }; ``` 3. 在项目的入口文件或者需要连接数据库的文件中,引入Sequelize库和先前创建的配置文件。然后,使用Sequelize构造函数创建一个数据库实例: ```javascript const Sequelize = require('sequelize'); const config = require('./config'); const sequelize = new Sequelize( config.development.database, config.development.username, config.development.password, { host: config.development.host, port: config.development.port, dialect: config.development.dialect } ); ``` 4. 现在,可以使用sequelize对象进行数据库操作,如定义模型、创建表格和查询数据等。以下是一个创建示例模型并同步数据库的代码片段: ```javascript const User = sequelize.define('User', { firstName: { type: Sequelize.STRING, allowNull: false }, lastName: { type: Sequelize.STRING, allowNull: false } }); sequelize.sync({ force: true }) // force: true会删除已存在的表 .then(() => { console.log('Database synchronized'); }) .catch((error) => { console.error('Unable to synchronize database:', error); }); ``` 在上述代码中,定义了一个名为User的模型,包含了firstName和lastName两个字段。然后,使用sequelize.sync()方法同步数据库并创建对应的表格。 通过以上步骤,就可以成功使用Sequelize连接MySQL数据库并进行相关操作。当然,这只是一个简单的示例,Sequelize还提供了更多强大的功能,例如数据关联、查询过滤和事务管理等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值