gpt4 book ai didi

mysql - ETIMEOUT 错误 |带有 NodeJS 的 Google Cloud SQL 数据库

转载 作者:搜寻专家 更新时间:2023-11-01 00:20:52 27 4
gpt4 key购买 nike

我在谷歌云上创建了一个 mysql 数据库,我想从一个单独的 Node 网络应用程序(也在谷歌云上运行)访问它。我首先在我的计算机上本地测试连接,当我在本地运行以下代码时,我可以成功建立与我的数据库的连接并查看其中的数据。

'use strict';

// [START app]
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const mysql = require('mysql');

var connection = mysql.createConnection({
host : 'Cloud SQL IP',
user : 'username',
password : 'password',
database : 'db_name'
});



// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// Make globals.js accessible
app.use(express.static(__dirname + '/'));


app.get('/', (req, res) => {
connection.connect();

connection.query('SELECT * FROM Users', function (error, results, fields) {
if (error) throw error;
console.log(results);
});

connection.end();
res.status(200).send('Hello World!');
});

app.get('/login', (req, res) => {
res.status(200).send();
});

// [START server]
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END app]

然而,当在我的谷歌应用引擎中运行相同的代码时(在端口 8080 上调试和在 https://myapp.appspot.com 上完全部署)我收到以下超时错误:

{ Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/home/megan_cooper2900/project/node_modules/mysql/lib/Connection.js:419:13)
at Socket.g (events.js:292:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:338:8)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
--------------------
at Protocol._enqueue (/home/megan_cooper2900/project/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/megan_cooper2900/project/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/megan_cooper2900/project/node_modules/mysql/lib/Connection.js:130:18)
at app.get (/home/megan_cooper2900/journeyma/app.js:31:13)
at Layer.handle [as handle_request] (/home/megan_cooper2900/project/node_modules/express/lib/router/layer.js:95:5)
at next (/home/megan_cooper2900/project/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/megan_cooper2900/project/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/megan_cooper2900/project/node_modules/express/lib/router/layer.js:95:5)
at /home/megan_cooper2900/project/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/megan_cooper2900/project/node_modules/express/lib/router/index.js:335:12)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true }

为什么这不适用于 Google App Engine 应用程序?

最佳答案

在您的 mysql 连接配置中,host 在 App Engine 上不起作用。您必须使用 socketPath 。 socketPath 是要连接的 unix 域套接字的路径。使用时主机和端口将被忽略。 (从在 App Engine flex 上使用 Loopback 转移知识。这让我撞了好几天的脑袋,哈哈)。它的值是您的 Cloud SQL 实例连接名称

所以在你的情况下,它应该是这样的:/cloudsql/my-project-12345:us-central1:mydatabase

var connection = mysql.createConnection({
socketPath : '/cloudsql/my-project-12345:us-central1:mydatabase',
user : 'username',
password : 'password',
database : 'db_name'
});

如果您在 GCloud 上使用 Postgres,这是一个类似的过程,答案是 here

关于mysql - ETIMEOUT 错误 |带有 NodeJS 的 Google Cloud SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46994701/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com