尝试从云函数连接到云 sql sql 服务器时出现连接错误



我正在使用Firebase函数连接到cloud-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' }

我认为云函数无法访问SQL服务器。我还尝试添加服务帐户以拥有 cloudsql 权限。

我以为我的云功能做错了什么,并尝试了云函数中的cloudsql-postgresql。我能够从云功能成功连接到ClouSQL-postgresql。

例如,在cloudsql-postgresql的情况下,主机使用unix域套接字获取类似于以下内容的值:

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

在Cloudsql-mssql的情况下,我提供了服务器的IP地址。我不知道我错过了什么。是否有我需要处理的防火墙问题?

根据文档:

Cloud Functions 不支持连接到 Cloud SQL for SQL 公共 IP 上的服务器。改用私有 IP

相反,您应该使用实例的私有 IP 地址和端口 1433 直接连接。

相关内容

  • 没有找到相关文章

最新更新