如何通过 SSL 连接以续集数据库 [英] How to connect via SSL to sequelize DB

查看:34
本文介绍了如何通过 SSL 连接以续集数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到有关如何使用 CA.crt 来连接到位于远程服务器上的数据库的 SEQUELIZE.JS 的任何文档.

I can't seem to find any documentation for SEQUELIZE.JS on how to use a CA.crt in order to enable connection to my database sitting on a remote server.

我在选项中找到了一些东西,但我似乎无法弄清楚

I figure its something in the options but I can't seem to figure it out

我试过了

{
 'ssl': true
 'dialectOptions':{
   ssl: {
     ca: 'path/to/ca'
   }
 }     
}

还有其他一些东西,但似乎对我不起作用.

and a few other things but nothing seem to work for me.

有人可以帮我吗?

这是我在使用 ca 时遇到的错误

Here is an error i get when using the ca thing

error connecting to db { Error: unable to verify the first certificate
at TLSSocket.<anonymous>

推荐答案

由于您没有提到后端数据库的选择,我将提供一个 mysql 示例以及我如何建议您去吧.

As you don't mention the backend DB of choice, I'll give a mysql sample and how I'd suggest you go about it.

首先,直接使用dialect确认连接,所以mysql2根据需要提供变量:

First, confirm the connection using the dialect directly, so for mysql2 supplying variables as necessary:

const connection = mysql.createConnection({
  host: dbVars.host,
  user: dbVars.user,
  database: dbVars.database,
  password: dbVars.password,
  ssl: {
    key: cKey,
    cert: cCert,
    ca: cCA
  }
});

确认连接后,将其移动到 Sequelize 为:

Once that connection is confirmed, move it to Sequelize as:

const sequelize = new Sequelize(dbVars.database, dbVars.user, dbVars.password, {
  host: dbVars.host,
  dialect: 'mysql',
  dialectOptions: {
    ssl: {
      key: cKey,
      cert: cCert,
      ca: cCA
    }
  }
});

注意:正确加载证书是一个学习曲线,需要使用 raw-loader 直接import.示例:

Note: loading the certs properly was a learning curve and required a direct import using a raw-loader. Example:

import cKey from 'raw-loader!../certs/client-key.pem'; 

这篇关于如何通过 SSL 连接以续集数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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