gpt4 book ai didi

javascript - 将 Hostinger DB 与 Node.js 连接

转载 作者:行者123 更新时间:2023-11-29 19:05:00 26 4
gpt4 key购买 nike

我在 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数据库?

最佳答案

您是否在帐户数据库>远程 MySQL 部分设置了远程 MySQL 连接?mysql.hostinger.in url 用于本地连接。如果您使用 node.js 应用程序,那么您将从外部源连接。

关于javascript - 将 Hostinger DB 与 Node.js 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com