如何使用React.js不连接到本地MySQL数据库(PHPMyAdmin)?

o2rvlv0m  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(166)

我是Node.js新手,我已经将现有的数据库上传到Dreamhost,其中包含数据库PhpMyAdmin。我已经创建了新的React应用程序,并通过使用我的服务器文件夹尝试连接到该数据库。我使用的是Windows 10,运行速度为http://127.0.0.1:5000/。以下是我的代码:

const express = require("express");
const bodyParser = require("body-parser");
const mysql = require("mysql");

const app = express()
const port = process.env.PORT || 5000

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

// MySQL

const pool = mysql.createPool({
    connectionLimit: 10,
    user: "root",
    host: '127.0.0.1',
    password: "password",
    database: "testdb_org"
})

// Get all info

app.get('/', (req, res) => {
    pool.getConnection((err, connection) => {

    res.send('TEST '+ JSON.stringify(err))
        /*if (err) throw err
        console.log(`Connected as id ${connection.threadId}`)

        connection.query('SELECT * from users', (err, rows) => {
            connection.release() // Return the connection to pool

            if (!err) {
                res.send(rows)
            } else {
                console.log(err)
            }
        })*/
    })
})

app.listen(port, () => console.log(`Listen on port ${port}`))
  • res.send* 显示以下错误:
{"code":"ER_ACCESS_DENIED_ERROR","errno":1045,"sqlMessage":"Access denied for user 'root'@'localhost' (using password: YES)","sqlState":"28000","fatal":true}

当我打开注解括号时,它显示以下控制台错误:

if (err) throw err
                 ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
    --------------------
    at Protocol._enqueue (C:\Users\*\Documents\project\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\*\Documents\project\server\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at PoolConnection.connect (C:\Users\*\Documents\project\server\node_modules\mysql\lib\Connection.js:116:18)
    at Pool.getConnection (C:\Users\*\Documents\project\server\node_modules\mysql\lib\Pool.js:48:16)
    at C:\Users\*\Documents\project\server\app.js:24:10
    at Layer.handle [as handle_request] (C:\Users\*\Documents\project\server\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\*\Documents\project\server\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\*\Documents\project\server\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\*\Documents\project\server\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\*\Documents\project\server\node_modules\express\lib\router\index.js:284:15 {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}
qyswt5oh

qyswt5oh1#

好吧,问题是它不可能与在线数据库和localhost的React.js.连接我必须下载.sql数据库,插入并运行它与XAMPP控制面板本地.

相关问题