Problem with connecting Node.js with MySQL - mysql

I am currently working on c9 IDE and when I run the code below, it gives me errors. How do I get around this Issue?
app.js
var mysql = require("mysql");
var connection = mysql.createConnection({
host: 'localhost',
user: 'poream3387',
database: 'join_us'
});
var q = 'SELECT CURTIME() as time, CURDATE as date, NOW() as now';
connection.query(q, function (error, results, fields) {
if (error) throw error;
console.log(results);
});
connection.end();
command:
node app.js
error:
/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Parser.js:80
throw err; // Rethrow non-MySQL errors
^
Error: ER_BAD_FIELD_ERROR: Unknown column 'CURDATE' in 'field list'
at Query.Sequence._packetToError (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Protocol.js:278:23)
at Parser.write (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket. (/home/ubuntu/workspace/node_modules/mysql/lib/Connection.js:91:28)
at Socket. (/home/ubuntu/workspace/node_modules/mysql/lib/Connection.js:502:10)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
--------------------
at Protocol._enqueue (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/home/ubuntu/workspace/node_modules/mysql/lib/Connection.js:200:25)
at Object. (/home/ubuntu/workspace/app.js:12:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)

I think, the issue is with not making CURDATE a function call by adding parenthesis, which is the same thing pointed out by #Jamie D

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!

I cannot connect to mysql database using node.js

I am total beginner to database.
I am using localhost(127.0.0.1) with port 9090.
I successfully created mysql database, ran/used it in windows 10 command prompt and MySQL Workbench.
But when I try to run it with node.js, I fail to connect to server.
Below is index.js file I tried to run in windows command line.
var mysql = require('mysql');
var client = mysql.createConnection({
user:'root',
password:'password',
});
client.query('USE mydb');
client.query('SELECT * FROM PRODUCTS',function(error, result, fields){
if(error){
console.log(error);
} else{
console.log(result);
}
});
And it results in this error.
D:\Coding\VSCode\nodejs>node index.js
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
I am at the stage not knowing what I am not knowing.
I tried to change port in createConnection() function like this
var client = mysql.createConnection({
user:'root',
password:'password',
port: '9090'
});
Then I get this error.
D:\Coding\VSCode\nodejs>node index.js
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by
server; consider upgrading MySQL client
at Handshake.Sequence._packetToError
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading
MySQL client',
sqlState: '08004',
fatal: true
}
1.Open your windows terminal and login :
mysql -uroot -p
( then type you password )
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
4 Restart you mysql db : sudo service mysql restart
Doing this in MySQL Workbench solved the problem.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password
BY 'YourRootPassword';
FLUSH PRIVILEGES;

I'm unable to connect to mysql from nodejs.I get the following error

/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Parser.js:80
throw err; // Rethrow non-MySQL errors
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/sequences/Handshake.js:130:18)
at Protocol._parsePacket (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/gayathri/jsonhbs/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/gayathri/jsonhbs/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/gayathri/jsonhbs/db1.js:8:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
In your error message, you can clearly see that you did not send the correct user field with the mysql connection call.
Ensure that you send the user you would like to connect as.
Which mysql library are you using? In felix-node-mysql, you must pass the following object:
mysql.createConnection({host:"localhost",user:"root",password:"secretpasshere"},function(...){...})

protocol packets out of order

I recently uploaded my node.js app on A2 Hosting and I get this error :
{ Error: Packets out of order. Got: 80 Expected: 0
at Parser.write (/home/westudec/public_html/myapp/node_modules/mysql/lib/protocol/Parser.js:42:19)
at Protocol.write (/home/westudec/public_html/myapp/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/westudec/public_html/myapp/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
--------------------
at Protocol._enqueue (/home/westudec/public_html/myapp/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/westudec/public_html/myapp/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/westudec/public_html/myapp/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/westudec/public_html/myapp/app2.js:22:7)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10) code: 'PROTOCOL_PACKETS_OUT_OF_ORDER', fatal: true }
I get that error from this code :
var connection = mysql.createConnection(connDB);
connection.connect();
connection.query(`SELECT * FROM action`, function(err, data){
if(err) return console.error(err);
else {
console.log("data : ", data);
}
})
On my localhost it works perfectly but on the server I get that error. The documentation about this error is very poor.
Does someone have an idea ?
Thank you
You need to increase mysql max_allowed_packet parameter in mysql configuration file (my.cnf) by the next way:
max_allowed_packet=500M

Connecting to Azure Database for MySQL in npm/mysql

I am getting an error when connecting to Azure Database for MySQL with the mysql npm module (version 2.13.0):
Error:
/Users/sascha/Desktop/azure-mysql/index.js:19
if (error) throw error;
^
Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/sequences/Handshake.js:103:18)
at Protocol._parsePacket (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at TLSSocket.ondata (_stream_readable.js:528:20)
at emitOne (events.js:77:13)
at TLSSocket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at TLSSocket.Readable.push (_stream_readable.js:110:10)
--------------------
at Protocol._enqueue (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/sascha/Desktop/azure-mysql/index.js:16:12)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.runMain [as _onTimeout] (module.js:467:10)
at Timer.listOnTimeout (timers.js:92:15)
Code:
var mysql = require("mysql");
var fs = require("fs");
var connection = mysql.createConnection({
host: "host",
user: "user#db",
password: "pw",
database: "scheme",
port: 3306,
ssl: {
ca: fs.readFileSync(__dirname + "/cert.pem")
},
insecureAuth: true
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
Connecting to the database via MySQL Workbench works fine.
This is a known issue as our engineers have a disscusion at https://social.msdn.microsoft.com/Forums/en-US/c8fedbcc-909d-41ce-8c72-0374f76fdf82/cannot-connect-from-nodejs?forum=AzureDatabaseforMySQL.
And currently, I have test the solution provided by #Bradley Grainger at Cannot connect Azure Web App - NodeJS to Azure Mysql. We also can see the gist at https://github.com/mysqljs/mysql/commit/e3e123e9af7c0829a6c1417d911572a75b4a5f95.
Currently, please modify the package.json as:
"dependencies": {
"mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
},
then, run npm install to install this version.
It should work as expected.
Additionally, please wait for the maintainer merges this PR.