简体   繁体   English

无法使用 Sequelize ORM 连接到 AWS RDS

[英]Unable to connect to AWS RDS using Sequelize ORM

I am working on an application which uses the Sequelize ORM to connect to AWS RDS.我正在开发一个使用 Sequelize ORM 连接到 AWS RDS 的应用程序。 I have my connection set up as such:我的连接设置如下:

Connection联系

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:',
});

I also have defined a model to represent the database table which is defined as such:我还定义了一个model来表示定义如下的数据库表:

Model 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();
}

and exported as such:并像这样导出:

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


export const V0MODELS = [ FeedItem ];

Then within my server.ts I import my sequelize connection and model and attempt to connect to my AWS RDS as such:然后在我的server.ts中导入我的 sequelize 连接和 model 并尝试连接到我的 AWS RDS:

server.ts服务器.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` );
  } );
})();

When I run the program no connection is established, and the server fails to start.当我运行程序时,没有建立连接,服务器无法启动。 When I remove the sequelize.sync method, the server will start but my tables are not created.当我删除sequelize.sync方法时,服务器将启动,但未创建我的表。 No error is caught by the catch block so I do not suspect there is an error. catch块没有捕获到错误,所以我不怀疑有错误。 Currently I do believe this is connection issue dealing with postgres and AWS, but I cannot seem to pinned it down.目前我确实认为这是处理 postgres 和 AWS 的连接问题,但我似乎无法确定它。 All feedback and direction are appreciated.感谢所有反馈和指导。

I have found the issue.我发现了这个问题。 The problem was due to the node version I was using, which at the time was 14.15.3.问题是由于我使用的节点版本,当时是 14.15.3。 This version is not compatible with my current version of postgres 13.1 so I used nvm and downgraded to node 11.15.0 and now my sequelize commands are working.这个版本与我当前的 postgres 13.1 版本不兼容,所以我使用 nvm 并降级到节点 11.15.0,现在我的 sequelize 命令正在工作。

My node version is 16 and I am guessing Postgres 13 is not compatible with it hence, I had to downgrade my node version to 11.15.0.我的节点版本是 16,我猜 Postgres 13 与它不兼容,因此我不得不将我的节点版本降级到 11.15.0。

Downgrade your node version to 11.15.0 by downloading nvm from here通过从此处下载 nvm 将您的节点版本降级到 11.15.0

Unzip the downloaded zip file and install.解压下载的 zip 文件并安装。 Click next and accept all default settings -- Do not customize the settings.单击下一步并接受所有默认设置——不要自定义设置。 If you have node already installed it might detect your current node version and ask to take control of it click yes.如果您已经安装了节点,它可能会检测到您当前的节点版本并要求控制它,请单击“是”。

After the installation process is done, open CMD or Powershell and type安装过程完成后,打开 CMD 或 Powershell 并输入

nvm list非虚拟机列表

This is will show you the list of node versions installed, then type这将向您显示已安装的节点版本列表,然后键入

nvm install 11.15.0非虚拟机安装 11.15.0

After installing the node 11.15.0 type安装节点 11.15.0 类型后

nvm list非虚拟机列表

To list the newly installed node version along with your previously installed node and then type列出新安装的节点版本以及之前安装的节点,然后键入

nvm use 11.15.0非虚拟机使用 11.15.0

Then go back to your vscode and run your project again.然后 go 回到你的 vscode 并再次运行你的项目。

#EDIT #编辑

If you're from ALX-Cloud Developer Program I suggest you downgrade your node version to 12 because Angular CLI version is only compatible with node v12 and above(at the time of writing this).如果您来自 ALX-Cloud Developer Program,我建议您将 node 版本降级到 12,因为 Angular CLI 版本仅与 node v12 及更高版本兼容(在撰写本文时)。 Good luck!!!祝你好运!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM