【问题标题】:Unable to connect to AWS RDS using Sequelize ORM无法使用 Sequelize ORM 连接到 AWS RDS
【发布时间】:2021-06-24 10:41:32
【问题描述】:

我正在开发一个使用 Sequelize ORM 连接到 AWS RDS 的应用程序。我的连接设置如下:

连接

import {Sequelize} from 'sequelize-typescript';



// Instantiate new Sequelize instance!
export const sequelize = new Sequelize({
  "username": "AWS RDS USER",
  "password": "AWS RDS PASS",
  "database": "postgres",
  "host":     "******.******.us-east-1.rds.amazonaws.com",

  dialect: 'postgres',
  storage: ':memory:',
});

我还定义了一个 model 来表示定义如下的数据库表:

型号

import {Table, Column, Model, CreatedAt, UpdatedAt} from 'sequelize-typescript';

@Table
export class FeedItem extends Model<FeedItem> {
  @Column
  public caption!: string;

  @Column
  public url!: string;

  @Column
  @CreatedAt
  public createdAt: Date = new Date();

  @Column
  @UpdatedAt
  public updatedAt: Date = new Date();
}

并像这样导出:

import { FeedItem } from './feed/models/FeedItem';


export const V0MODELS = [ FeedItem ];

然后在我的 server.ts 中,我导入我的 sequelize 连接和模型,并尝试像这样连接到我的 AWS RDS:

server.ts

import express from 'express';
import { sequelize } from './sequelize';

import { IndexRouter } from './controllers/v0/index.router';

import { V0MODELS } from './controllers/v0/model.index';

(async () => {
  
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
    await sequelize.addModels(V0MODELS);
    await sequelize.sync({ force: true, logging: console.log });
  } catch (error) {
    console.error(error);
  }
  
  
  const app = express();
  const port = process.env.PORT || 8080; // default port to listen
  
  app.use(express.json());

  //CORS Should be restricted
  app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://localhost:8100");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    next();
  });

  app.use('/api/v0/', IndexRouter)

  // Root URI call
  app.get( "/", async ( req, res ) => {
    res.send( "/api/v0/" );
  } );
  

  // Start the Server
  app.listen( port, () => {
      console.log( `server running http://localhost:${ port }` );
      console.log( `press CTRL+C to stop server` );
  } );
})();

当我运行程序时,没有建立连接,服务器无法启动。当我删除sequelize.sync 方法时,服务器将启动,但我的表没有创建。 catch 块没有发现错误,所以我不怀疑有错误。目前我确实相信这是处理 postgres 和 AWS 的连接问题,但我似乎无法确定它。感谢所有反馈和指导。

【问题讨论】:

  • 您是否尝试过使用更简单的方式进行连接,例如 knex 或只是简单的 pg 模块?从基础开始。另外,您写“服务器无法启动”-这是什么意思?您是否收到错误消息,或者进程是否挂起而没有任何可见的进展?
  • 您好罗伯特,感谢您的帮助。如果可能的话,我宁愿坚持使用 sequelize,因为我正在遵循课程计划。当我说 sever 无法启动 时,我的意思是在 sequelize 语句到位时服务器根本没有启动。删除后,我收到控制台消息“服务器正在端口 8080 上运行”
  • 当然 - 我建议尝试连接 something else 只是为了查看它是否有效、凭据是否正常等。“根本无法启动”的声音就像它正在运行但没有产生任何输出,对吧?听起来确实是连接问题。

标签: node.js postgresql amazon-web-services sequelize.js


【解决方案1】:

我找到了问题。问题是由于我使用的节点版本,当时是 14.15.3。此版本与我当前的 postgres 13.1 版本不兼容,因此我使用 nvm 并降级到节点 11.15.0,现在我的 sequelize 命令正在运行。

【讨论】:

    猜你喜欢
    • 2018-10-18
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2016-12-13
    • 2017-12-13
    相关资源
    最近更新 更多