以下代码用于通过Firebase Cloud Functions发出mysql查询。
该代码在本地计算机上运行时有效。 ($ firebase serve
)
但是在Google Cloud Platform上运行时,会发生错误ETIMEDOUT
。 ($ firebase deploy
)
我想让你告诉我可能的原因。
注意
-Cloud Functions运行时配置为Node.js 8
,我的本地环境也是Node.js 8.0.0
代码:
const mysql = require('mysql')
const pool = mysql.createPool({
host: '10.20.30.40',
user: 'user',
password: 'pass',
database: 'db',
connectTimeout : 10000
})
pool.query('SELECT * FROM table', (error, rows, fields) => {
if (error) {
console.error(error)
throw error
}
console.log('The solution is: ', rows)
pool.end()
})
错误:
{ Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (/srv/node_modules/mysql/lib/Connection.js:411:13)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Socket.emit (events.js:208:7)
at Socket._onTimeout (net.js:420:8)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
--------------------
at Protocol._enqueue (/srv/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/srv/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/srv/node_modules/mysql/lib/Connection.js:118:18)
at Pool.getConnection (/srv/node_modules/mysql/lib/Pool.js:48:16)
at Pool.query (/srv/node_modules/mysql/lib/Pool.js:202:8)
at Promise (/srv/lib/modules/myCellar.js:32:14)
at new Promise (<anonymous>)
at Object.exports.get.uid [as get] (/srv/lib/modules/myCellar.js:31:12)
at Object.<anonymous> (/srv/lib/index.js:14:36)
at Generator.next (<anonymous>)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true }