javascript 错误:连接 ETIMEDOUT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24490120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:54:02  来源:igfitidea点击:

Error: connect ETIMEDOUT

javascriptmysqlnode.js

提问by Waleed Amjad

I am trying to work with Node and Mysql but i keep encountering error when trying to connect with the Database.

我正在尝试使用 Node 和 Mysql,但在尝试连接数据库时一直遇到错误。

 //For mysql server
var mysql = require('mysql');

var connection = mysql.createConnection(
{
  host: 'example.com',
  user: 'abc',
  password: '***',
  database: 'abcd',
  port: 3306,
});
// If there is an error connecting to the database
connection.connect( function(err)
{
  if (err)
  { 
    throw err;
  }
  else 
  {
    console.log('DB connection establish');
  }
});

function check_userid(usersId)
{
var que = connection.query(
  'select * from table where id = '+usersId, function(err, result, fields){
  if(err) throw err;
  console.log('Resultset: ', result);
  console.log('Length of Resultset: ', result.length);

  if(result.length == 0)
  {
    connection.query('insert into table (id, user_status) values ( "' + usersId + '", "' + 'connected' + '")', 
                    function (err, result) 
                    {
                      if (err) throw err;
                      console.log('Updation of table: ',result.insertId);
                    });
  }
  else
  {
    connection.query('update table SET user_status="'+'connected'+'" WHERE id = "' + usersId + '"', 
                    function (err, result) 
                    {
                      if (err) throw err;
                      console.log('Updation of Table: ',result.insertId);
                    });
  }
  });

}
//For websocket
var webSocketServer = new (require('ws')).Server({port: (process.env.PORT || 5000)}),
webSockets = {} // userID: webSocket

// CONNECT /:userID
// wscat -c ws://localhost:5000/1
webSocketServer.on('connection', function (webSocket) 
{
  var userID = parseInt(webSocket.upgradeReq.url.substr(1), 10)
  webSockets[userID] = webSocket
                   console.log('connected: ' + userID + ' in ' + Object.getOwnPropertyNames(webSockets))

                   check_userid(userID);

                   // Forward Message
                   //
                   // Receive               Example
                   // [toUserID, text]      [2, "Hello, World!"]
                   //
                   // Send                  Example
                   // [fromUserID, text]    [1, "Hello, World!"]
                   webSocket.on('message', function(message) {
                                console.log('received from ' + userID + ': ' + message)
                                var messageArray = JSON.parse(message)
                                var toUserWebSocket = webSockets[messageArray[0]]
                                if (toUserWebSocket) {
                                console.log('sent to ' + messageArray[0] + ': ' + JSON.stringify(messageArray))
                                messageArray[0] = userID
                                toUserWebSocket.send(JSON.stringify(messageArray))
                                }
                                })

                   webSocket.on('close', function () {
                                delete webSockets[userID]
                                console.log('deleted: ' + userID)
                                connection.query('update table SET user_status="'+'disconnected'+'" WHERE id = "' + userID + '"', 
                    function (err, result) 
                    {
                      if (err) throw err;
                      console.log('Updation of table: ',result.insertId);
                    });
                                })
                   })

And i keep getting the following Error

我不断收到以下错误

Error: connect ETIMEDOUT
at errnoException (net.js:904:11)
at Object.afterConnect as oncomplete
--------------------
at Handshake.Sequence (/Users/apple/Desktop/js/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)
at new Handshake (/Users/apple/Desktop/js/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
at Protocol.handshake (/Users/apple/Desktop/js/node_modules/mysql/lib/protocol/Protocol.js:44:50)
at Connection.connect (/Users/apple/Desktop/js/node_modules/mysql/lib/Connection.js:38:18)
at Object. (/Users/apple/Desktop/js/server.js:13:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

Description: I run same server in localhost,and succesfully connected with phpMyadmin and I did not got that error but When I implement that server in VPS it cannot create connection with phpMyadmin and say connect ETIMEDOUT.

描述:我在 localhost 中运行相同的服务器,并成功连接到 phpMyadmin 并且我没有收到该错误但是当我在 VPS 中实现该服务器时,它无法创建与 phpMyadmin 的连接并说连接 ETIMEDOUT。

采纳答案by Waleed Amjad

This error occurred due to connect the server with Remote DataBase. If you want to connect your server with remotely database then make sure your remotely database provider have given the access of remote DB otherwise you need to install the mysql in you VPS.

由于将服务器与远程数据库连接而发生此错误。如果您想将您的服务器与远程数据库连接,请确保您的远程数据库提供商已授予远程数据库的访问权限,否则您需要在您的 VPS 中安装 mysql。

回答by mscdex

Check your mysql server configuration. If you're executing this code on the same system as the mysql server, then mysql may not be configured to be listening on TCP port 3306 (it may just be listening on a unix socket).

检查您的 mysql 服务器配置。如果您在与 mysql 服务器相同的系统上执行此代码,则 mysql 可能未配置为侦听 TCP 端口 3306(它可能只是侦听 unix 套接字)。

If you're instead trying to connect from outside the system that the mysql server is running on, then not only may the mysql configuration not be set up to listen on the right network interface on port 3306, but there could be firewall rules you have to set up first in order to get to the mysql server.

如果您尝试从运行 mysql 服务器的系统外部进行连接,那么不仅 mysql 配置可能未设置为在端口 3306 上的正确网络接口上侦听,而且可能存在防火墙规则首先进行设置,以便访问 mysql 服务器。