如何解决数据库连接错误[SequelizeConnectionError]? [英] How to solve the database connection error [SequelizeConnectionError]?

查看:692
本文介绍了如何解决数据库连接错误[SequelizeConnectionError]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为数据库连接出错而苦苦挣扎:

无法连接数据库:ConnectionError [SequelizeConnectionError]:自签名证书

这里有我使用的代码 - 被注释的部分不起作用,错误仍然出现:

<预><代码>//const pg = require('pg');//pg.defaults.ssl = true;//const fs = require('fs');//const rdsCa = fs.readFileSync(__dirname + '/rds-combined-ca-bundle.pem');const sequelize = new Sequelize('xxxx', 'xxxxxx', 'xxxxxxxx',{主机:'xxxxxx.eu-west-1.compute.amazonaws.com',方言:'postgres',ssl:是的,协议:postgres",日志记录:真实,方言选项:{ssl: 真的}});续集.认证().then(() => {console.log('连接已成功建立.');}).catch(错误 => {console.error('无法连接数据库:', err);});

数据库建立在 Heroku 之上.

你能帮我解决这个问题吗?

解决方案

这是由于 node-postgres 版本 8 中的(意外)重大更改(请参阅 这个 GitHub 问题).

解决方案是将 rejectUnauthorized: false 传递给 sequelize 连接参数,如 由 GitHub 用户 jsanta 在这里描述:

const sequelize = new Sequelize('xxxx', 'xxxxxx', 'xxxxxxxx',{主机:'xxxxxx.eu-west-1.compute.amazonaws.com',方言:'postgres',ssl:是的,协议:postgres",日志记录:真实,方言选项:{ssl:{要求:真实,拒绝未授权:假//<<<<<<<<<<<你需要这个}}});

我在此处提交了相同的答案,但我无法将此问题标记为重复并指向因为它还没有在那里被投票/接受.

I am struggling with the database connection getting error:

Unable to connect to the database: ConnectionError [SequelizeConnectionError]: self signed certificate

Here you have the code which I use - the part which is commented does not work, the error still appears:


// const pg = require('pg');
// pg.defaults.ssl = true;

// const fs = require('fs');
// const rdsCa = fs.readFileSync(__dirname + '/rds-combined-ca-bundle.pem');


const sequelize = new Sequelize('xxxx', 'xxxxxx', 'xxxxxxxx',{
    host: 'xxxxxx.eu-west-1.compute.amazonaws.com',
    dialect: 'postgres',
    ssl: true,
    protocol: "postgres",

    logging: true,
        dialectOptions: {
        ssl: true
    }
});
sequelize
    .authenticate()
    .then(() => {
        console.log('Connection has been established successfully.');
    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

The database is built on Heroku.

Can you please help me how to solve the issue?

解决方案

This is due to an (accidental) breaking change in node-postgres version 8 (see this GitHub issue).

The solution is to pass rejectUnauthorized: false to the sequelize connection parameters, as described here by GitHub user jsanta:

const sequelize = new Sequelize('xxxx', 'xxxxxx', 'xxxxxxxx',{
    host: 'xxxxxx.eu-west-1.compute.amazonaws.com',
    dialect: 'postgres',
    ssl: true,
    protocol: "postgres",

    logging: true,
    dialectOptions: {
        ssl: {
            require: true,
            rejectUnauthorized: false // <<<<<< YOU NEED THIS
        }
    }
});

I have submitted the same answer here but I can't mark this question as duplicate and point to it because it hasn't been upvoted/accepted there yet.

这篇关于如何解决数据库连接错误[SequelizeConnectionError]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆