2

Goal: Do a simple query to the database.

Expected results: "please print something!" and the results from the query are printed on the terminal.

Actual results: Nothing is printed on the terminal.

Errors: No error message.

Here is the db.js file:

var mysql = require('mysql');
    
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'todoDB'
});

connection.connect();

connection.query('SELECT * FROM categories', function (err, res, fields) {
    console.log("please print something!")
    if (err) throw err;
    console.log(res);
});

connection.end();

I execute this file using:

node db.js

On the mysql cli, I am able to do this query without any problem with the database name, credentials, and query given above.

I know that connection.connect() works since when I'm inputting the code below, the terminal prints "Database is connected!" I think the problem occurs at connection.query, but I am not sure why.

connection.connect(function(err){
    if(!err){
        console.log("Database is connected");
    } else {
        console.log("Error while connecting with database");
        console.log(err);
    }
});

I've looked through all the related questions on stackoverflow and tried them, but none of the solutions seems to resolve the problem that I have.

14
  • A connection is implicitly established by invoking a query, so you don't need connection.connect(); and you don't need connection.end();. Remove them both and try again. Since you are not running each step in your example asynchronously it is possible you're ending your connection before your query has completed depending on a number of variables such as: network speed, app resources, and the size of results generated by your query. Commented Jul 2, 2021 at 17:38
  • @dusthaines I removed the connection.connect() and connection.end(). There is still nothing that is printed on the terminal. Commented Jul 2, 2021 at 17:48
  • Try changing if (err) throw err; to... if (err) console.log(err); You can also add debug: true to your mysql.createConnection configs which will provide verbose details on all incoming and outgoing packets on stdout. Commented Jul 2, 2021 at 18:36
  • @dusthaines I changed the code and pasted the output of when I run node db.js here Commented Jul 2, 2021 at 18:50
  • Worth noting that his exact code works fine on my environment. Commented Jul 2, 2021 at 19:05

1 Answer 1

3

So it looks like mySQL V8 uses caching_sha2_password as the new authentication standard, which the nodeJS plugin does not support. Connect to your db and try creating a new user that uses the native password auth type.

CREATE USER 'foo'@'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';

Sign up to request clarification or add additional context in comments.

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.