Search code examples
mysqlreactjsphpmyadmindreamhost

How to connect not to local MySQL db (PHPMyAdmin) by using React.js?


I am pretty new at Node.js, and I have existing database uploaded to Dreamhost, which has database PhpMyAdmin. I have created new React application, and by using my server folder I am trying to connect to that database. I am using Windows 10, and I run at http://127.0.0.1:5000/. This is my code:

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 gives me following error:

{"code":"ER_ACCESS_DENIED_ERROR","errno":1045,"sqlMessage":"Access denied for user 'root'@'localhost' (using password: YES)","sqlState":"28000","fatal":true}

And when I open comment brackets it gives me following console error:

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
}

Solution

  • Ok, the problem that it's not possible with online database and localhost of React.js. For connection I had to download .sql database, insert and run it locally with XAMPP control panel.