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

时间:2017-10-15 03:34:34

标签: mysql node.js google-cloud-sql

我正在尝试使用我的node.js mysql模块连接到我的google-cloud-sql实例,但我无法连接到它。我提到了以下链接mysql docsgoogle getting started,但无法理解发生了什么。

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();

连接时出现以下错误。

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

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码运行也检查连接和正确的IP。服务器和系统之间可能存在内部防火墙块

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