1

I have started node-js recently and i was trying to connect my nodejs server with mysql. The problem is i am getting an error, i really don't know why, i am using phpmyadmin.

Phpmyadmin details

user: root

host: localhost

password is not set

This is the image of my phpmyadmin database

This is the settings of my phpmyadmin console

This is the terminal where it is showing error connecting to DB

index.js

    var express = require("express");
    var app = express();
    var mysql = require('mysql');

     var port = process.env.PORT || 3000;

     var connection = mysql.createConnection({
     host: "localhost",
     user: "root",
     database: "learning",
     });

   connection.connect(function(err){
    if(err) {
   console.log('Error connecting to Db');
    return;
    }

  console.log('Connection established');
  console.log('3');

  connection.end(function(err) {
  console.log('Connection closed');
  console.log('4');

  process.exit();
  });
  });

  app.listen(port,function(err1){
  console.log("Listening on the port 3000");
  });
13
  • If there is no password try removing the password key/value Commented Jun 9, 2018 at 20:23
  • Also - just don't leave it unprotected in the long term!! Commented Jun 9, 2018 at 20:23
  • tried that, still getting the same error Commented Jun 9, 2018 at 20:23
  • 1
    change that to console.log('Error connecting to Db', err) and see where it is failing. Commented Jun 9, 2018 at 20:28
  • Error connecting to Db { Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client Commented Jun 9, 2018 at 20:29

2 Answers 2

2

connection to mysql from node js, you are use to mysql from node js connection

/config
  /database.js
/server.js
/.env

const http = require('http');
const app = require('express')();
require('./config/database.js');
const bodyParser = require('body-parser');
const server = http.createServer(app);

server.listen(process.env.ServerPort, '0.0.0.0', () => {
  logger.info(`Express server listening on port ${process.env.ServerPort}`);
});

When you run this:

node server.js

database.js file

const My = require('jm-ez-mysql');

// Init DB Connection
const connection = My.init({
  host: process.env.DBHOST,
  user: process.env.DBUSER,
  password: process.env.DBPASSWORD,
  database: process.env.DATABASE,
  dateStrings: true,
  charset: 'utf8mb4',
  timezone: 'utc',
  multipleStatements: true,
  connectTimeout: 100 * 60 * 1000,
  acquireTimeout: 100 * 60 * 1000,
  timeout: 100 * 60 * 1000,
});

module.exports = {
  connection,
};
Sign up to request clarification or add additional context in comments.

Comments

0

I had changed the port from default 3306 of phpmyadmin mysql to 3308

therefore i added port: 3308 and it started working.

 var connection = mysql.createConnection({
 host: "localhost",
 user: "root",
 database: "learning",
 port: 3308
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.