0

我正在尝试将 Loopback 应用程序连接到 Google SQL 数据库,并且我已更改 Datasource.json 文件以匹配凭据。但是,当我在 Loopback API 资源管理器中发出 GET 请求时,出现错误。我没有找到任何关于如何在 Datasource.json 中指定 ssl 凭据的文档,我认为这是导致错误的原因。

我尝试更改 Datasource.json 无果而终,以下是当前状态。我已经更改了隐私的详细信息,但我 100% 确定凭据是正确的,因为我可以与 javascript 成功连接。

{
  "nameOfModel": {
    "name": "db",
    "connector": "mysql",
    "host": "xx.xxx.x.xxx",
    "port": xxxx,
    "user": "user",
    "password": "password",
    "database": "sql_db",
    "ssl": true,
    "ca" : "/server-ca.pem",
    "cert" : "/client-cert.pem",
    "key" : "/client-key.pem"
  }
}

这是我在环回 API 资源管理器上尝试 GET 请求时命令行返回的错误。错误:

5000 毫秒后连接超时”让我相信它没有读取 ssl 凭据。

Unhandled error in GET /edd-sales?filter[offset]=0&filter[limit]=0&filter[skip]=0: 500 TypeError: Cannot read property 'name' of undefined
    at EddDbDataSource.DataSource.queueInvocation.DataSource.ready (D:\WebstormProjects\EDD-Database\edd-api\node_modules\loopback-datasource-juggler\lib\datasource.js:2577:81)

(node:10176) UnhandledPromiseRejectionWarning: Error: Timeout in connecting after 5000 ms
    at Timeout._onTimeout (D:\WebstormProjects\EDD-Database\edd-api\node_modules\loopback-datasource-juggler\lib\datasource.js:2572:10)
    at ontimeout (timers.js:498:11)

(node:10176) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:10176) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
4

2 回答 2

0

您确定 datasource.json 允许您指定“ssl”连接吗?你从哪里得到这些信息的?我检查了他们的文档,他们没有显示您正在使用的“ssl”属性。它也没有在 MySQL 连接器属性上指定。

你有两个选择:

1.- 在不使用 SSL 的情况下创建连接。

2.- 创建您自己的连接器或使用现有的连接器并实施 ssl 选项。请记住,这可能会导致 LoopBack 框架出现问题。

不管您决定使用这两个选项中的哪一个,请记住将您的 IP(您尝试访问数据库实例的 IP)列入白名单,您可以在 Cloud Console 上的“连接”选项卡上执行此操作,在“公共 IP”授权网络下。如果您不这样做,可能会导致您收到超时错误。

于 2019-06-26T13:35:43.820 回答
0

尝试这个。我正在使用lookback3它,它对我很有效。您需要创建datasources.local.js才能正确加载CA文件。

数据源.local.js

const fs = require('fs');

module.exports = {
  nameOfModel: {
    name: 'db',
    connector: 'mysql',
    host: 'xx.xxx.x.xxx',
    port: 'xxxx',
    user: 'user',
    password: 'password',
    database: 'sql_db',
    ssl: {
      ca: fs.readFileSync(`${__dirname}/server-ca.pem`),
      cert: fs.readFileSync(`${__dirname}/client-cert.pem`),
      key: fs.readFileSync(`${__dirname}/client-key.pem`),
    },
  }
}

ssl: true请注意,您需要使用具有这些属性的对象,而不是 using 。

于 2020-03-30T11:22:09.183 回答