-1

I am trying to connect node js to MySQL database which is up and running according to the MySQL Workbench. However the usual create connection code template isn't working.

Any ideas as to why this is?

I have tried putting a function and error around the connection.connect() part to see if it actually connects. But the terminal window comes up with the same error nonetheless:

Marcs-MacBook-Pro:npm-global marcwatts$ node index.js
Error while performing Query.

Do we think that it is connecting ok or does it not connect in the first place?

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '<hidden from stack overflow>',
  database : 'songdata'

});

connection.connect();


connection.query('SELECT * from songdata2', function(err, rows, fields)       {
  if (!err)
    console.log('The solution is: ', rows);
  else
    console.log('Error while performing Query.');
});

connection.end();

I just want to connect so I can learn how to write the code which takes each song and places its lyrics inside the Watson Tone analyzer and outputs the results in a new table in MySQL. Any advice on this part would be much appreciated too as I am new to web apps!

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
marcuz2007
  • 13
  • 5

1 Answers1

1
The recommended way to establish a connection is this:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});
Rubin bhandari
  • 1,873
  • 15
  • 20
  • thanks for the reply :) . Have just tried it and this is the error: – marcuz2007 Jun 28 '19 at 10:59
  • error connecting: Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client – marcuz2007 Jun 28 '19 at 11:00
  • thats partially solves your problem. Try another library called sequelize – Rubin bhandari Jun 28 '19 at 11:02
  • I'm using that now but it doesn't seem to like the word 'dialect' (as directed by using this http://docs.sequelizejs.com/manual/getting-started.html ). Is this the right way to do it? /Users/marcwatts/Desktop/mxw817/DBTestApp/npm-global/index.js:4 dialect: 'mysql' ^^^^^^^ SyntaxError: Unexpected identifier – marcuz2007 Jun 28 '19 at 11:12