错误:连接超时

5
我正在尝试使用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);
                    });
                                })
                   })

我一直遇到以下错误

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)

描述: 我在本地主机上运行了同样的服务器,并成功地连接了phpMyadmin,没有出现错误,但是当我在VPS中实现该服务器时,它无法与phpMyadmin建立连接,并显示连接超时(connect ETIMEDOUT)。

2个回答

1
由于连接到远程数据库的服务器所导致的错误。如果您想将服务器连接到远程数据库,请确保您的远程数据库提供商已经授权访问远程DB,否则您需要在VPS上安装MySQL。

0

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

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


1
我在本地机器上(使用xamp)运行了该服务器,它成功连接到了phpMyadmin。使用该运行中的服务器,当客户端连接到服务器时,服务器可以成功使用查询将详细信息输入到phpMyadmin表中。 但是,当我在VPS上运行该服务器时,它无法连接到phpMyadmin(即mysql.transip.nl),并且2分钟后会出现错误。 请您再次检查我的问题,因为我已经编辑了我的问题。 - Waleed Amjad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接