-1

I am trying to connect mysql with nodejs, but there is a problem in my code, please help!

var express    = require("express");
 var mysql      = require('mysql');
 var connection = mysql.createConnection({
   host     : 'localhost',
   user     : 'root',
   password : 'admin',
   database : 'dbname',
 });  
 var app = express();

 connection.connect(function(err){
 if(!err) {
     console.log("Database is connected ... \n\n");  
 } else {
     console.log("Error connecting database ... \n\n");  
 }
 });
app.listen(3000);

in terminal i am getting this output:

voldemort@Lenovo:~/Documents/cloudprint$ node server.js Error connecting database ...

  • Please provide the full error output. But probably you have to add port settings as well, check if your database port differs from the default 3306 – Yuriy Chachora Feb 17 '19 at 07:44
  • It might help if you also displayed the actual error. The error messages usually give a lot of information and aren’t just a flag that an error happened. – Sami Kuhmonen Feb 17 '19 at 07:44
  • add a `console.log(err))`. What does it say? – molamk Feb 17 '19 at 07:49
  • @molamk where do i add it? – Saurabh Feb 17 '19 at 08:07
  • before `console.log("Error connecting database ... \n\n");` – molamk Feb 17 '19 at 08:08
  • @molamk now i am getting a big error.(not able to paste it completely)node server.js { Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client at Handshake.Sequence._packetToError (/home/voldemort/Documents/cloudprint/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14) at Handshake.ErrorPacket (/home/voldemort/Documents/cloudprint/node_modules/mysql/lib/protocol/sequences/Handshake.js:124:18) at Protocol._parsePacket – Saurabh Feb 17 '19 at 08:18
  • If you are using MySQL 8.0, the issue and possible fixes are described [here](https://stackoverflow.com/a/50377944/3235909). – ruiquelhas Feb 20 '19 at 12:29

1 Answers1

0

The MySQL client establishes a connection to the server using secure authentication by default.

insecureAuth: Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false) 1

Looking at the error traceback in your comment, it looks like your server isn't currently configured to allow secure authentication. 2

If your MySQL server version is <= 5.6.4 then secure auth is off and you may want to enable the old auth protocol by enabling insecureAuth when making the connection in the client.

 var connection = mysql.createConnection({
   host     : 'localhost',
   user     : 'root',
   password : 'admin',
   database : 'dbname',
   insecureAuth: true
 });

However, I advise updating the passwords for your users to use the more secure password hashing function if your MySQL server version is >= 5.6.5. There is step-by-step documentation to accomplish this for user accounts.

You may also want to update your MySQL server if you find that you're running an older version that doesn't support the secure auth protocol.

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81