Home > Blockchain >  What DB is my NodeJS application connecting to if I don't a MySQL service running?
What DB is my NodeJS application connecting to if I don't a MySQL service running?

Time:10-01

I'm using a Mac, and I know I don't have a MySQL service running by...

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

or

$ mysql -u root -h 127.0.0.1
ERROR 2003 Y(HY000): Can't connect to MySQL server on '127.0.0.1' (61)

I have the following NodeJS application, I only show the pertinent part.

const express = require("express");
const mysql = require("mysql");

// Create connection
const db = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});

// Connect to MySQL
db.connect((err) => {
//  if (err) {
//    throw err;
//  }
  console.log("MySql Connected");
});

// Since we are using the Express module to create the web server,
// we create a variable named app that behaves as an object of
// the express module.
const app = express();

// Listen on port 3000
app.listen("3000", () => {
  console.log("Server started on port 3000");
});

Yet when I run it, I see

$ node index.js 
Server started on port 3000
MySql Connected

So what MySQL service is it connecting to? TIA.

CodePudding user response:

For what I can see, you are printing a console.log regardless of it having errors of not.

Your condition of

//  if (err) {
//    throw err;
//  }

is commented out, so you can try erasing the comments.

So your code should look like this:

db.connect((err) => {
  if (err) {
     throw err;
  }
  console.log("MySql Connected");
});
  • Related