Search code examples
mysqlnode.jsconnection

connection to a remote mysql database using node


Good mornig to you guys. I want to establish a connection to a remote mysql database using node js. but i am facing this error. I do not know if I wrongly specifies the access path to the db

code

var mysql = require('mysql');

var pool = mysql.createPool({
  host: "http://kamerun-it.com/mysql",
  connectionLimit : 100,
  database: "****",
  user: "****",
  password: "*****",
  multipleStatements: true

});

error

  throw err;
  ^

Error: getaddrinfo ENOTFOUND http://kamerun-it.com/mysql at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) -------------------- at Protocol._enqueue (F:\kamerun it\aspi-api\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Protocol.handshake (F:\kamerun it\aspi-api\node_modules\mysql\lib\protocol\Protocol.js:51:23) at PoolConnection.connect (F:\kamerun it\aspi-api\node_modules\mysql\lib\Connection.js:119:18) at Pool.getConnection (F:\kamerun it\aspi-api\node_modules\mysql\lib\Pool.js:48:16) at Object. (F:\kamerun it\aspi-api\app\model\db.js:16:8) at Module._compile (internal/modules/cjs/loader.js:956:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Module.require (internal/modules/cjs/loader.js:849:19) { errno: 'ENOTFOUND', code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'http://kamerun-it.com/mysql', fatal: true }


Solution

  • You probably have an error in your config with the host URL.

    Here's a working example with a connection to a remote MySQL:

    const mysql = require("mysql");
    
    const connection = mysql.createPool({
      host: "remotemysql.com",
      user: "aKlLAqAfXH",
      password: "PZKuFVGRQD",
      database: "aKlLAqAfXH"
    });
    
    connection.query(
      "SELECT hexcode FROM colours WHERE precedence = 2",
      (err, result) => {
        err ? console.log(err) : console.log(result[0].hexcode);
      }
    );
    

    and here's one with mistaken host parameter:

    const mysql = require("mysql");
    
    const connection = mysql.createPool({
      host: "WRONGremotemysql.com",
      user: "aKlLAqAfXH",
      password: "PZKuFVGRQD",
      database: "aKlLAqAfXH"
    });
    
    connection.query(
      "SELECT hexcode FROM colours WHERE precedence = 2",
      (err, result) => {
        err ? console.log(err) : console.log(result[0].hexcode);
      }
    );
    

    The second one returns the same ENOTFOUND error.

    Check if that's the correct URL, if the database can be accessed remotely and via which port you can use it.