我在Hostinger.com上安装了MySQL数据库。我可以使用PHP轻松访问数据库,但是当我尝试使用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网站上没有提到这样的事情。如果是这样,那么是否有任何PaaS服务提供可以与node.js轻松连接的MySQL数据库?
答案 0 :(得分:0)
您是否在帐户Databases>Remote MySQL
部分设置了远程MySQL连接?
mysql.hostinger.in
url用于本地连接。如果您使用node.js应用程序,那么您将从外部源连接。