简体   繁体   English

无法使用node.js连接到Google Cloud SQL服务

[英]Unable to connect to google cloud sql service using node.js

I am trying to connect to my google-cloud-sql instance using my node.js mysql module and I am unable to connect to it. 我正在尝试使用我的node.js mysql模块连接到我的google-cloud-sql实例,但无法连接到它。 I referred to the following links mysql docs and google getting started but could not understand what's going on. 我指的是以下链接mysql docsgoogle入门,但是不知道发生了什么。

var connection = mysql.createConnection({
    acquireTimeout: 30000,
    host  : '35.184.77.86',
    user     : 'XXX',
    password : 'XXX',
    database : 'name',
});

connection.connect(function(err){

    if (err) {
        console.error(chalk.red('error connecting: ' + err.stack));
        return;
    }
    console.log(chalk.green('connected as id ' + connection.threadId));
});

connection.query('SELECT * FROM likes', function(err, rows, fields)   
{  
  if (err) {
      console.log(chalk.red(err));
  }else{ 
    console.log(fields); 
  }
});

connection.end();

I am getting the following error while connecting. 连接时出现以下错误。

error connecting: Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout (/Users/narenpalep/Desktop/javascript/Project_Portfolio/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 (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at Connection.connect (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/Connection.js:130:18)
    at Object.<anonymous> (/Users/narenpalep/Desktop/javascript/Project_Portfolio/db/db.js:11:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
Error: connect ETIMEDOUT

Try running with the below code also check the connection and for the right IP. 尝试使用以下代码运行,并检查连接和正确的IP。 There might be internal firewall block between the server and the system 服务器与系统之间可能存在内部防火墙阻止

 con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM