试图从 cloud-function 连接到 Cloud-sql sql-server 时出现连接错误。

问题描述 投票:0回答:1

我正在使用firebase函数连接到云SQL(Micorsoft SQL Server)。我尝试了以下方法通过公共IP连接到SQL Server。

/*Tedious Start*/
var TediousConnection = require('tedious').Connection;
var TediousRequest = require('tedious').Request;
var TediousTypes = require('tedious').TYPES;
const config = {
  user: 'admin',
  password: 'password',
  server: '12.345.5.678',
  database: 'mma',
  //host: 'myproject:us-central1:my-db'
};
app.get('/mssql/', async(req, res) => {
  var sql = require('mssql');
  sql
    .connect(config)
    .then(function() {
      //6.
      var request = new sql.Request();
      //7.
      request
        .query('SET NOCOUNT ON;select * from student')
        .then(function(recordset) {
          // send records as a response
          res.send(recordset.recordset);
          sql.close();
        })
        .catch(function(err) {
          //8.
          console.log(err);
          sql.close();
        });
      res.end();
    })
    .catch(function(err) {
      //9.
      console.log(err);
    });
});

我得到以下错误信息:

{ ConnectionError: Failed to connect to 12.345.5.678:1433 in 15000ms
    at Connection.tedious.once.err (/srv/node_modules/mssql/lib/tedious/connection-pool.js:68:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection.connectTimeout (/srv/node_modules/mssql/node_modules/tedious/lib/connection.js:1195:10)
    at Timeout.connectTimer.setTimeout [as _onTimeout] (/srv/node_modules/mssql/node_modules/tedious/lib/connection.js:1157:12)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
  code: 'ETIMEOUT',
  originalError: 
   { ConnectionError: Failed to connect to 12.345.5.678:1433 in 15000ms
    at ConnectionError (/srv/node_modules/mssql/node_modules/tedious/lib/errors.js:13:12)
    at Connection.connectTimeout (/srv/node_modules/mssql/node_modules/tedious/lib/connection.js:1195:54)
    at Timeout.connectTimer.setTimeout [as _onTimeout] (/srv/node_modules/mssql/node_modules/tedious/lib/connection.js:1157:12)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
     message: 'Failed to connect to 12.345.5.678:1433 in 15000ms',
     code: 'ETIMEOUT' },
  name: 'ConnectionError' }

我认为cloudfunction无法到达sql服务器。我也试着添加服务账号来拥有cloudsql的权限。

我以为是我的云函数出了问题,于是尝试了云函数的cloudsql-postgresql。我能够成功地从云函数连接到ClouSQL-postgresql。

例如,在cloudsql-postgresql的情况下,主机使用unix域套接字取一个类似于下面的值。

config.host = `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`;

在Cloudsql-mssql的情况下,我提供了服务器的ipaddress。我不知道我缺少什么。是否有防火墙的问题需要我去处理?

sql-server google-cloud-functions google-cloud-sql sql-server-2017
1个回答
0
投票

根据 文件:

Cloud Functions不支持通过公共IP连接到Cloud SQL for SQL Server。请使用私人IP。

而不是直接使用私有IP地址和1433端口来连接你的实例。

© www.soinside.com 2019 - 2024. All rights reserved.