Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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");
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading