mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.31k stars 2.53k forks source link

Error: getaddrinfo ENOTFOUND when trying to connect to WordPress database with correct credentials #2489

Closed ecsmagic16 closed 3 years ago

ecsmagic16 commented 3 years ago

I am trying to set up a connection between my WordPress database and my React Web Application. I am getting an error and I don't know how to fix it. Below is my code.

const express = require("express");
const app = express();
const mysql = require('mysql');

const connection = mysql.createConnection({
    host    : 'xxxxxx',
    user    : 'xxxxxx',
    password: 'xxxxxx',
    database: 'xxxxxx',       
});

connection.connect((err) => {
    if(err) throw err;
    console.log('Connected to MySQL Server!');
});

app.get("/",(req,res) => {
    connection.query('SELECT * from users LIMIT 1', (err, rows) => {
        if(err) throw err;
        console.log('The data from users table are: \n', rows);
        connection.end();
    });
});

app.listen(3000, () => {
    console.log('Server is running at port 3000');
});

Error display in cmd:

Error: getaddrinfo ENOTFOUND xxxxx
...
...
...
 errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'xxxxx',
  fatal: true
dougwilson commented 3 years ago

Hi @ecsmagic16 sorry you are having trouble. Unfortunately the error you are getting is coming from Node.js itself, and not this module. This module is just passing the error up to you for reference.

You can find more about Node.js error codes at https://nodejs.org/dist/latest-v16.x/docs/api/errors.html#errors_common_system_errors

ENOTFOUND (DNS lookup failed): Indicates a DNS failure of either EAI_NODATA or EAI_NONAME. This is not a standard POSIX error.