我试图在我的react应用程序中实现express来访问我的数据库表,为此我遇到了this package。我使用server
来实现一个yarn start
文件夹(它通过concurrently
和nodemon
与index.js
脚本一起开始),如下所示:
const express = require('express');
const mysql = require('mysql');
const app = express();
const con = mysql.createConnection({
host: 'xxxxx',
user: 'xxxxx',
password: 'xxxxx',
database: 'xxxxx',
});
con.connect( err => {
if (err) {
console.log(err);
}
})
app.get('/api/fetch-list', (req, res) => {
// code to fetch
});
const PORT = 3001;// process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`App server now listening on port ${PORT}`);
});
然后,在此组件中调用route / path / api / fetch-list:
import React from 'react';
import axios from 'axios';
function renderTable() {
axios.get(`${process.env.PUBLIC_URL}/api/fetch-list`).then(res => {
console.log(res.data);
});
}
export function component() {
return (
<div>
{renderTable()}
</div>
);
}
一旦我转到http:localhost:3000 / table,就会渲染此组件。因此,一旦我运行该项目并转到该地址,我就会在终端控制台中收到以下错误:
{ Error: getaddrinfo ENOTFOUND xxxxx xxxxx:3306
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
--------------------
at Protocol._enqueue (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/Connection.js:119:18)
at Object.<anonymous> (/home/gfcf14/Desktop/greendream-redesign/src/server/index.js:27:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'xxxxx',
host: 'xxxxx',
port: 3306,
fatal: true }
我不确定为什么会这样。我知道我使用的凭据是正确的,因为它可以在我的旧php代码网站上正常工作。有一会儿,我认为这可能与数据库的位置有关,因为我将其托管在1&1(ionos)上,并且它们没有授予我专用的服务器。因此,我将凭据更改为一个齿轮主机空间,在该空间中我有另一个数据库(我仍在使用其凭据),而我收到此错误:
{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'xxxxx'@'myipaddress' (using password: YES)
at Handshake.Sequence._packetToError (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/Connection.js:525:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
--------------------
at Protocol._enqueue (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/gfcf14/Desktop/greendream-redesign/node_modules/mysql/lib/Connection.js:119:18)
at Object.<anonymous> (/home/gfcf14/Desktop/greendream-redesign/src/server/index.js:27:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: 'Access denied for user \'xxxxx\'@\'myipaddress\' (using password: YES)',
sqlState: '28000',
fatal: true }
所以它也不适合那个。我可能必须检查该站点的权限(乍看之下似乎是READ & WRITE
),但是如何获得第一个数据库的连接呢?如果您看到我可能错过的东西,请告诉我