Wednesday, 31 May 2017

Error: ER_HOST_NOT_PRIVILEGED Accessing a MySQL Database using SOCKS Proxy in Node.js with Heroku

I have configured my code as per the instructions on heroku here.

Initially it looks like everything is good to go. As you can see the test works:

Result:  [ TextRow { test1: 2 } ]

However I am still getting the following error when I try to access my mySQL database through the socks proxy:

 Unhandled rejection Error: ER_HOST_NOT_PRIVILEGED: Host 'ec2-54-234-17-229.compute-1.amazonaws.com' is not allowed to connect to this MySQL server

It must be noted that the Host here changes regularly as is the norm with Heroku. But the Quota Guard add-on is supposed to provide a static IP for TCP connections.

Here is my set up code:

var mysql = require('mysql2')
var url = require("url")
var SocksConnection = require('socksjs')

var remote_options = {
 host:'I put the ip address to my database here',
 port: 3306
};

var proxy = url.parse(process.env.QUOTAGUARDSTATIC_URL)
var auth = proxy.auth;
var username = auth.split(":")[0]
var pass = auth.split(":")[1]

var sock_options = {
host: proxy.hostname,
port: 1080,
user: username,
pass: pass
}

var sockConn = new SocksConnection(remote_options, sock_options)

var dbConnection = mysql.createConnection({
  user: 'I put my user here',
  database: 'I put my database name here',
  password: 'I put my password here',
  stream: sockConn
})

dbConnection.query('SELECT 1+1 as test1;', function(err, rows, fields)     {
    if (err) throw err;

    console.log('Result: ', rows)
    sockConn.dispose()

})

dbConnection.end();

And here is the code I am using to try and connect. I have also tried this from within the dbConnection.query and had the exact same result.

app.post('/addContact', function (req, res) {

  payload = req.body.payload.person
  console.log('full name: ', payload.full_name)
  console.log("id: ", payload.id)

  knex('contacts').insert({
    contact_name: payload.full_name,
    email: payload.email

  }).then(function(data, err){
    if(err) {
      console.log('error message: ', err)
    } else {
    console.log( data, payload.full_name, 'entered into SQL Database')
      }
    })
   });




app.listen(port)

Hoping someone can help! Cheers :)



via LaShark

No comments:

Post a Comment