Connection to MySql with Node JS and Sequelize - mysql

When I try to connect from node to my MySql database that runs on the same machine (debian), with a specific user and password I get an "Access denied" error.
If I try the same, with the same user and set no password in the database and node, everything works.
Also connection from the terminal WITH password works fine.
I already tried to delete user and database and recreated them. But nothing works.
This is my node code:
var http = require('http');
var fs = require('fs');
var Sequelize = require('sequelize')
var sequelize = new Sequelize('mysql://user:password#localhost:3306/databasename');
http.createServer(function (req, res) {
console.dir(req.url);
sequelize.query("SELECT ##version;").spread(function(results, metadata) {
console.log(results);
})
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('ok');
}).listen(8080);
What I get on the console is this:
Possibly unhandled Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'#'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/var/www/html/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/var/www/html/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:67:18)
at Protocol._parsePacket (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:197:24)
at Parser.write (/var/www/html/node/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (_stream_readable.js:555:20)
at emitOne (events.js:101:20)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/var/www/html/node/node_modules/mysql/lib/Connection.js:81:18)
at ConnectorManager.connect (/var/www/html/node/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:314:16)
at Object.create (/var/www/html/node/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:138:19)
at createResource (/var/www/html/node/node_modules/generic-pool/lib/generic-pool.js:258:13)
at dispense (/var/www/html/node/node_modules/generic-pool/lib/generic-pool.js:250:9)
at Object.me.acquire (/var/www/html/node/node_modules/generic-pool/lib/generic-pool.js:316:5)
at CustomEventEmitter.fct (/var/www/html/node/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:248:19)
at CustomEventEmitter.<anonymous> (/var/www/html/node/node_modules/sequelize/lib/emitters/custom-event-emitter.js:24:18)
Any clues?

Ok, the problem was, package.json did not contain the dependency (mysql or sequelize). I still don't know, why I could connect to the database without password though.
But now it works.

Related

Having issues with Node JS MySQL connection

I'm creating a discord bot that pulls from a pre-established database of patch note news I have, it's my first time dealing with NodeJS and MySQL. I've got the default connection code in the file right now to test the connection and it looks like this:
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const mysql = require('mysql');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const con = mysql.createConnection({
host: process.env.HOST,
port: process.env.PORT,
user: process.env.MYSQLUSER,
db: process.env.DATABSE,
password: process.env.MYSQLPASSWORD,
});
con.connect((err) => {
if (err) throw err;
console.log("Connected");
})
I've tried hosting the application local on my machine with a remote connection to the SQL database and on the actual server that it lives on, so far this is all I'm getting:
/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'niemer5_discordbot'#'localhost' (using password: NO)
at Handshake.Sequence._packetToError (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:315:12)
--------------------
at Protocol._enqueue (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/home/niemer5/news-bot/index.js:22:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: "Access denied for user 'niemer5_discordbot'#'localhost' (using password: NO)",
sqlState: '28000',
fatal: true
}
The user exists, it has ALL privileges, I set everything up in cPanel and also tried granting privs in SQL CLI and so far no luck it just errors out with a 1045 access denied error and I have no clue what is happening. Looking for a little guidance here!

"Error: ER_NO_SUCH_TABLE: Table 'managementapp.medicament' doesn't exist"

I'm working in simple CRUD application using react, node and express.. so i connected to my mysql data base "managementapp" and i tried to insert information in my db, but im always getting the error "table doesn't exist while i tested locally and it works properly in mysql wrokbensh and im so sure that im spelling the name of my table correctly. so this the code of App.js for react :
function App() {
const [name,setName]=useState("");
const [description,setDescription]=useState("");
const [number,setNumbre]=useState(0);
const addMedicament = () => {
console.log(name);
Axios.post("http://localhost:3001/create",
{name:name,
description:description,
number:number,
}).then(()=>{
console.log("succes");
});
};
and this my index.js for backend part
const db=mysql.createConnection({
user:"root",
host:"localhost",
password:"",
database:"managementapp",
});
app.post("/create", (req, res) => {
console.log(req.body);
const name=req.body.name;
const description=req.body.description;
const number=req.body.number;
db.query(
"insert into medicament (name,description,number) VALUES (?,?,?);",
[name,description,number],
(err,result) =>{
if (err)
{ console.log(err);
}
else { res.send("Values inserted !! ");
}
}
);
});
so im always getting this error
{ name: 'erty', description: 'rtyuk', number: '86' }
Error: ER_NO_SUCH_TABLE: Table 'managementapp.medicament' doesn't exist
at Query.Sequence._packetToError (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\Users\Chahine\Management\server\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (C:\Users\Chahine\Management\server\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:290:12)
--------------------
at Protocol._enqueue (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\Chahine\Management\server\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\Chahine\Management\server\index.js:25:8
at Layer.handle [as handle_request] (C:\Users\Chahine\Management\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Chahine\Management\server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Chahine\Management\server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Chahine\Management\server\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Chahine\Management\server\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Chahine\Management\server\node_modules\express\lib\router\index.js:341:12)
at next (C:\Users\Chahine\Management\server\node_modules\express\lib\router\index.js:275:10)
{
code: 'ER_NO_SUCH_TABLE',
errno: 1146,
sqlMessage: "Table 'managementapp.medicament' doesn't exist",
sqlState: '42S02',
index: 0,
sql: "insert into medicament (name,description,number) VALUES ('erty','rtyuk','86');"
}
i was searching since last night and i found that the problem was with my mysql installation .. so i uninstalled it and reinstall it then i changed the port from 3306 to another cause it was already in use .. then i had the error about
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol
requested by server; consider upgrading MySQL client
so i just get into mysql and run a query saying root is fine using old mysql_native_password method for authentication:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';
and everything went right. thanks to Aidin's explanation.

Connecting to external Mysql database from AWS lambda (back end of Alexa skill) returns "Access denied for user" message

I am writing an Alexa lambda function in Javascript to connect to my external database. I have successfully accessed that database with the same username and password. I have uploaded the mysql code from the noded_modules folder on my laptop. Here is the Javascript code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "www.oryxtech.net",
user: "",
password: ""
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
and this is the error message from the lambda console:
2020-01-03T23:08:10.885Z 1100009f-24bf-4980-a2e1-27044988eb9d Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'oryxtech_sideE'#'ec2-3-85-146-115.compute-1.amazonaws.com' (using password: YES)
at Handshake.Sequence._packetToError (/var/task/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/var/task/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/var/task/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/var/task/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/var/task/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/var/task/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/var/task/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/var/task/mysql/lib/Connection.js:525:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
--------------------
at Protocol._enqueue (/var/task/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/var/task/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/var/task/mysql/lib/Connection.js:119:18)
at mysqlDB.init (/var/task/mysqlDB.js:17:13)
at getWelcomeResponse (/var/task/index.js:64:19)
at onLaunch (/var/task/index.js:263:5)
at exports.handler (/var/task/index.js:328:13)
Am I doing something wrong, or is there a step I have omitted? Thanks in advance for any help.
It looks like the user oryxtech_sideE is not granted permission to access the database from host ec2-3-85-146-115.compute-1.amazonaws.com. Please confirm the same.
You can grant the user to access the host
GRANT ALL ON *.* to 'oryxtech_sideE'#'ec2-3-85-146-115.compute-1.amazonaws.com' IDENTIFIED BY 'password';
or any host:
GRANT ALL ON *.* to 'oryxtech_sideE'#'%' IDENTIFIED BY 'password';
Reference:
Allow all remote connections, MySQL
Hope this helps.

Unknown column "something" in `Field list`, What's wrong?

I'm writing a server at port 3000 and its job is to get the data from "Signup.ejs" input's values and then inserting them into the database.
All is fine but there's a problem, it says to me that TechnoBoy is an unknown column in Field list
I tried inserting "" or even `` around ${} but it didn't work well since a new error has appeared.
This is my Connection.js file that I use it to create a connection and then export it to connect it in my app's files:
let mysql = require("mysql");
let connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "something",
database: "test"
});
module.exports = connection;
And this is my Server.js that I use it to create a server and connect to the database insert the data into it:
let connection = require("./Connection.js");
let bodyParser = require("body-parser");
let express = require("express");
let app = express();
let urlencodedParser = bodyParser.urlencoded({ extended: false });
app.listen(3000, function() {
console.log("The server at port 3000 is open!");
});
app.set("view engine", "ejs");
app.get("/Signup", (req, res) => {
res.render("Register");
});
connection.connect();
app.post("/Signup", urlencodedParser, function(req, res) {
console.log(req.body);
res.render("Register");
connection.query(
`insert into users (Username, User_Password) values (${
req.body.Username
}, ${req.body.Password})`
);
});
Error I receive when I insert some data into input fields:
Error: ER_BAD_FIELD_ERROR: Unknown column 'TechnoBoy' in 'field list'
at Query.Sequence._packetToError (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\Users\ha\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\Users\ha\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (C:\Users\ha\node_modules\mysql\lib\Connection.js:502:10)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
--------------------
at Protocol._enqueue (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\ha\node_modules\mysql\lib\Connection.js:200:25)
at C:\Users\ha\OneDrive\Images\Documents\Projects\UI-Server\Server.js:17:14
at Layer.handle [as handle_request] (C:\Users\ha\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\ha\node_modules\express\lib\router\route.js:137:13)
at C:\Users\ha\node_modules\body-parser\lib\read.js:130:5
at invokeCallback (C:\Users\ha\node_modules\raw-body\index.js:224:16)
at done (C:\Users\ha\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\ha\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
Emitted 'error' event at:
at Connection._handleProtocolError (C:\Users\ha\node_modules\mysql\lib\Connection.js:425:8)
at Protocol.emit (events.js:182:13)
at Protocol._delegateError (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:390:10)
at Query.<anonymous> (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:153:12)
at Query.emit (events.js:182:13)
at Query.Sequence.end (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Sequence.js:78:12)
at Query.ErrorPacket (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Query.js:90:8)
at Protocol._parsePacket (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\Users\ha\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:38:16)
The query I used(which is in connection.query btw):
insert into users (Username, User_Password) values (${
req.body.Username
}, ${req.body.Password})
Excuse me if this is a stupid question or something, I'm very new to backend and this one is my first try on it.
I'm an idiot. I configured Password field to be int so it can't enter strings but I passed it a string, that's why when I was passing it, it was saying wrong syntax and stuff.
Oh and also, I forgot to wrap the username with "" although I still don't know how it is a problem when the property is a string itself.
Problem got solved, it can be closed now.

node-webkit MySQL connection Error - ER_HANDSHAKE_ERROR: Bad handshake

I am using Node-Webkit for creating desktop application using NodeJS.
In this i need to get data from MySQL database. For this am using node-mysql module and the following code is used to connect MySQL server using node-mysql.
var mysql_con = require('mysql');
var connection = mysql_con.createConnection({
host : input.host,
user : input.username,
password : input.password,
database : input.database
});
connection.connect(function(err) {
if (err) {
console.log('Error : '+err.stack);
}
console.log('Success');
});
if i run in the command line of my Windows machine, connection has been established and Success has been printed in console. But if i pack and run the code in the node-webkit's nw.exe its giving the following error in web console
Error : Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:270:23)
at Parser.write (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:104:17)
at readableAddChunk (_stream_readable.js:156:16)
at Socket.Readable.push (_stream_readable.js:123:10)
at TCP.onread (net.js:520:20)
--------------------
at Protocol._enqueue (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:135:48)
at Protocol.handshake (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\Connection.js:108:18)
at Object.mysql.testConnection
Kindly suggest me solution to establish the MySQL connection through node-webkit.
i had the same problem... and downgrading node webkit to the version 0.8.6 did the trick...
i hope this help you...