2017-04-23 80 views
0

我在Hostinger.com上有一个MySQL数据库设置。我可以很容易地使用PHP访问数据库,但是当我尝试访问相同的node.js代码时,它会抛出错误。连接hostinger DB与node.js

我的Node.js代码:

app.get('/hostinger', function(request, response) { 
    response.send("Hello"); 
    var connection = mySequel.createConnection({ 
     host: "mysql.hostinger.in", 
     user: "u**********j", 
     password: "*****", 
     database: "u**********j" 
    }); 
    connection.connect(); 
    var buery = "select * from testing"; 
    connection.query(buery, function(error, result, fields) { 
     if (error) { 
      console.log(error); 
     } else { 
      response.send(result); 
      console.log(result); 
     } 
    }) 
    connection.end(); 
    // console.log("This is received via a get request in hostinger"); 
}); 

的错误,我得到的是:

"117.193.192.51" dyno=web.1 connect=0ms service=2ms status=304 bytes=149 protocol=https 
2017-04-23T17:59:49.759522+00:00 app[web.1]: { Error: getaddrinfo ENOTFOUND mysql.hostinger.in mysql.hostinger.in:3306 
2017-04-23T17:59:49.759532+00:00 app[web.1]:  at errnoException (dns.js:28:10) 
2017-04-23T17:59:49.759533+00:00 app[web.1]:  at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) 
2017-04-23T17:59:49.759534+00:00 app[web.1]:  -------------------- 
2017-04-23T17:59:49.759537+00:00 app[web.1]:  at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:141:48) 
2017-04-23T17:59:49.759538+00:00 app[web.1]:  at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:52:41) 
2017-04-23T17:59:49.759538+00:00 app[web.1]:  at Connection.connect (/app/node_modules/mysql/lib/Connection.js:130:18) 
2017-04-23T17:59:49.759539+00:00 app[web.1]:  at /app/index.js:29:16 
2017-04-23T17:59:49.759540+00:00 app[web.1]:  at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 
2017-04-23T17:59:49.759540+00:00 app[web.1]:  at next (/app/node_modules/express/lib/router/route.js:137:13) 
2017-04-23T17:59:49.759541+00:00 app[web.1]:  at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3) 
2017-04-23T17:59:49.759542+00:00 app[web.1]:  at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 
2017-04-23T17:59:49.759542+00:00 app[web.1]:  at /app/node_modules/express/lib/router/index.js:281:22 
2017-04-23T17:59:49.759543+00:00 app[web.1]:  at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12) 
2017-04-23T17:59:49.759544+00:00 app[web.1]: code: 'ENOTFOUND', 
2017-04-23T17:59:49.759544+00:00 app[web.1]: errno: 'ENOTFOUND', 
2017-04-23T17:59:49.759545+00:00 app[web.1]: syscall: 'getaddrinfo', 
2017-04-23T17:59:49.759545+00:00 app[web.1]: hostname: 'mysql.hostinger.in', 
2017-04-23T17:59:49.759546+00:00 app[web.1]: host: 'mysql.hostinger.in', 
2017-04-23T17:59:49.759547+00:00 app[web.1]: port: 3306, 
2017-04-23T17:59:49.759548+00:00 app[web.1]: fatal: true } 

从错误日志来看,我可以看到主机不能妥善解决,当我在PHP文件中给出相同的凭据并执行它,我可以无任何错误地运行它。

我的代码可能存在什么问题? 另外我在00webhost中读到node.js不能用于访问数据库。 Hostinger网站没有提到这样的事情。如果是这样的话,那么是否有任何提供MySQL数据库的PaaS服务可以方便地与node.js连接?

回答

0

您是否在账户Databases>Remote MySQL部分设置了远程MySQL连接? mysql.hostinger.in url用于本地连接。如果您使用node.js应用程序,以便从外部源连接。