skip to Main Content

I have 2 servers, one running the frontend code (Node.js, express) and the other running MySQL with PHPMyAdmin, both are working standalone and I can confirm the MySQL database is running on 3306 but whenever I try to connect via Node.js (code below) I get a connection refused error.

    const conn = mysql.createConnection({
        host: '192.168.1.250',
        user: 'mcd',
        password: '**********',
        database: 'mcd'
    })

    conn.connect(function(err) {
        if (err) throw err;
    })

The IP address I have used for the host is the IP address of the MySQL server. So unsure why it cannot connect since it is running on the default port.

2

Answers


  1. The ECONNREFUSED error indicates that it can’t connect to where the function is hosted (in this case localhost)

    dbConnection.js

    const mysql = require('mysql');
    
    const connection = mysql.createPool({
        connectionLimit: 10, // default = 10
        host: '127.0.0.1',
        user: 'root',
        password: '',
        database: 'dname'
    });
    
    module.exports = connection;
    
    Login or Signup to reply.
  2. Here is my connection to Node.js:

    pool = mysql.createPool({host:"localhost"
                            ,port:"3306"
                        ,database:"db_name"
                            ,user:"user_name"
                        ,password:"password_for_user"
                        ,timezone:"utc"
              ,multipleStatements:true
                             ,max:1000
                             ,min:1
               ,idleTimeoutMillis:defs.QUERY_TIMEOUT});  
     if ( pool && pool.getConnection ) {  
       pool.getConnection(function(errsts, conn) {
        var resp = {};
    
        if ( errsts ) {
          resp['error'] = errsts;
          return;        
        }
        resp['state'] = "connected";
            
        if ( cbRoutine ) {        
          cbRoutine(conn, resp, objParams);
            
          if ( conn != undefined ) {
            conn.release();
          }
        }
      });
    }    
    

    localhost is correct for my usage, you should replace its your name or IP address.
    defs.QUERY_TIMEOUT is defined in my source as:

    var QUERY_TIMEOUT         = 10 * 1000;
    

    In my code cbRoutine is a call back function passed in as a parameter to call on successful connection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search