简体   繁体   中英

Error en db: Error: connect ECONNREFUSED 127.0.0.1:8889

I have a database named project with a table named user .

image 1

image 2

I create a backend folder with several files.

image 3

When, I enter the command: npm run serve

I have an error message

image 4

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json  
[nodemon] starting `node server.js`
Error en db:  Error: connect ECONNREFUSED 127.0.0.1:8889
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
    --------------------
    at Protocol._enqueue (C:\Users\attan\Desktop\BACKEND\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\attan\Desktop\BACKEND\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\attan\Desktop\BACKEND\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (C:\Users\attan\Desktop\BACKEND\api\connection\connection.js:11:17)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8889,
  fatal: true
}

In my browser, I enter http://localhost:3000/user , I don't retrieve the datas? I don't understand why?

image 5

connection.js

const mysql = require('mysql');

const mysqlConnection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'project', //nom de la database
    port: '8889'
});

mysqlConnection.connect(err => {
    if (err) {
        console.log('Error en db: ', err);
        return;
    } else {
        console.log('Db ok');
    }
});

module.exports = mysqlConnection;

app.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');

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

app.use(cors());

// ROUTES

const userRoute = require('./api/routes/user');
app.use('/user', userRoute);

module.exports = app;

server.js

const http = require('http');
const app = require('./app');

const port = process.env.PORT || 3000;

const server = http.createServer(app);

server.listen(port);

package.json

{
    "name": "BACKEND",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "serve": "nodemon server.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "cors": "^2.8.5",
        "express": "^4.17.2",
        "jsonwebtoken": "^8.5.1",
        "mysql": "^2.18.1",
        "nodemon": "^2.0.15"
    }
}

user.js

const express = require('express');
const router = express.Router();

const mysqlConnection = require('../connection/connection');

router.get('/', (req, res) => {
    mysqlConnection.query('select * from user', (err, rows, fields) => {
        if (!err) {
            res.json(rows);
        } else {
            console.log(err);
        }
    })
});

module.exports = router;

I think you have set wrong mysql port.

"Port 3306 is the default port for the classic MySQL protocol"

You probably should remove port config and have it like

const mysqlConnection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'project', //nom de la database
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM