Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

479
Vistas
Why does my connection.query with mysql in node not work?

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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';

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda