2017-10-28 1 views
1

별도의 노드 웹 응용 프로그램 (Google 클라우드에서도 실행)에서 액세스하려는 Google 클라우드에 mysql 데이터베이스를 만들었습니다. 내 컴퓨터에서 로컬로 연결을 테스트하고 있는데 다음 코드를 로컬에서 실행할 때 데이터베이스에 대한 연결을 성공적으로 설정하고 그 데이터를 볼 수 있습니다. 왜이 작동하지 않습니다ETIMEOUT 오류 | NodeJS가 포함 된 Google Cloud SQL 데이터베이스

{ 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 } 

: 나는 다음 시간 제한 오류 (포트 8080에 모두 디버깅을 위해 완전히 https://myapp.appspot.com에 배포) 내 Google 앱 엔진에서이 같은 코드를 실행하지만

'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] 

Google App Engine 애플리케이션에서

+0

호스트 속성에 인스턴스 연결 이름을 추가하십시오. – Fran

+0

호스트 값은 대개 URL 형식이 아닙니까? – Megan

+0

NodeJS에서 연결하는 것과 관련된 많은 기존 질문을 살펴보십시오. tl; dr을 사용하려면 IP로 연결하지 않고 App Engine의 연결 지원을 사용해야합니다. 예 : https://stackoverflow.com/questions/46749817/cloud-sql-instance-connection-working-locally-but-not-on-app-engine. 그 질문은 포스트 그레스에 관한 것이지만 해결책은 같습니다. – Vadim

답변

0

mysql 연결 설정에서 host은 App Engine에서 작동하지 않습니다. socketPath을 사용해야합니다. socketPath는 연결할 unix 도메인 소켓에 대한 경로입니다. 사용할 때 호스트와 포트는 무시됩니다. (App Engine에서 Loopback을 사용하여 지식을 이전했습니다. flex를 사용하면 하루 동안 내 머리를 쾅쾅 대고 말았습니다.)/cloudsql/내 프로젝트-12345 : 그것은 그것 같이한다, 값이 클라우드 SQL 인스턴스 연결 이름

귀하의 경우 너무

입니다입니다 우리를-센트럴 1 : 그것은이 비슷

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

을 MYDATABASE 당신이 응답 한 Gcloud에서 Postgres를 사용하고있는 경우 프로세스 here