I’m trying to connect to MySQL via nodeJS app using express for server, ive read the mysql npm documentation in order to begin connection but it seems like i get the error from the callback function i wrote inside createConnection.connect() and im using phpmyadmin just to see results and charts
here is my code:
const express = require('express');
const app = express();
const mysql = require('mysql');
require('dotenv').config();
const workers = require('./routes/workers');
app.use('/', workers);
const DB = mysql.createConnection({
host : 'localhost',
user : 'root',
charset: 'utf8_general_ci',
password: '',
});
DB.connect((err) => {
throw `error: ${err}`
});
app.listen(process.env._PORT, () => {
console.log(`Server is running on ${process.env._PORT}`);
});
2
Answers
Install sequelizejs , mysql ,mysql2 libararies
You can use the .authenticate() function to test if the connection is OK:
You are not checking error after connection. You have to check is there any error while database connection like following.