I'm using node.js with mysql database.
I'm using the config like this:
var mysql = require('mysql');
var client = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'java1234',
port: '3306',
database: 'sample'
});
and then I get these error messages:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user
'root'#'localhost' (using password: NO)
at Handshake.Sequence._packetToError (C:\node\test11_users\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (C:\node\test11_users\node_modules\mysql\lib\protocol\sequences\Handshake.js:124:18)
at Protocol._parsePacket (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\node\test11_users\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket. (C:\node\test11_users\node_modules\mysql\lib\Connection.js:91:28)
at Socket. (C:\node\test11_users\node_modules\mysql\lib\Connection.js:502:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
--------------------
at Protocol._enqueue (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\node\test11_users\node_modules\mysql\lib\Connection.js:118:18)
at Connection._implyConnect (C:\node\test11_users\node_modules\mysql\lib\Connection.js:453:10)
at Connection.query (C:\node\test11_users\node_modules\mysql\lib\Connection.js:198:8)
at C:\node\test11_users\routes\users.js:26:10
at Layer.handle [as handle_request] (C:\node\test11_users\node_modules\express\lib\router\layer.js:95:5)
at next (C:\node\test11_users\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\node\test11_users\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\node\test11_users\node_modules\express\lib\router\layer.js:95:5)
but I can't solve this problem.
thanks for your explain easily.
Remove port from connection options and try
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'java1234',
database : 'sample'
});
connection.connect();
Related
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 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've read all the similar/duplicate questions, but none of them seem to help.
I am using node with the mysql npm package.
I am able to log into mysql with no problem from the terminal...
sudo mysql -u root -p
...but unable to do via node.
Here is the error.
if (error) throw error;
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/Connection.js:525:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
--------------------
at Protocol._enqueue (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/Connection.js:119:18)
at Object.<anonymous> (/mnt/c/ivan/lab/posto/server/database/createDatabase.js:11:12)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
The database connection.
rdbms: {
server: 'localhost',
user: 'root',
password: 'root',
port: 3306
}
The code I'm trying to execute.
let mysql = require('mysql');
let rdbms = require("../config").rdbms;
let connection = mysql.createConnection(rdbms);
connection.connect();
let query = `
DROP DATABASE IF EXISTS dbname;
CREATE DATABASE dbname;
`
connection.query(query, function (error, results, fields) {
if (error) throw error;
console.log("Database created!");
});
connection.end();
My question is kind of similar to this one: Cant connect to localhost database with Sequelize (nodejs), but it has some difference and I couldn't use the answer provided.
I am trying to access mysql server from docker container. The access is provided via mapping mysqld.sock. The basic connection is verified. For example, inside container I can run the command as below.
# mysql --host=localhost --user=root --password=...
However, I cannot run using "127.0.0.1" as host. This might be something to look for regarding what I need to do next.
Then I try to verify mysql access using a nodejs program based on Sequelize as below.
var Sequelize = require('sequelize')
var sequelize = new Sequelize('mysql', 'root', '...', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
},
});
sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});
When I run it, it reports connection refused.
Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
at Handshake._callback (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:80:20)
at Handshake.Sequence.end (/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
at Protocol.handleNetworkError (/node_modules/mysql/lib/protocol/Protocol.js:364:14)
at Connection._handleNetworkError (/node_modules/mysql/lib/Connection.js:428:18)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'SequelizeConnectionRefusedError',
message: 'connect ECONNREFUSED 127.0.0.1:3306',
parent:
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
--------------------
at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/node_modules/mysql/lib/Connection.js:130:18)
at /node_modules/sequelize/lib/dialects/mysql/connection-manager.js:75:16
at Promise._execute (/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/node_modules/bluebird/js/release/promise.js:77:14)
at ConnectionManager.connect (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:55:10)
at ConnectionManager.<anonymous> (/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:247:43)
at ConnectionManager.tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/node_modules/bluebird/js/release/async.js:143:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true },
original:
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
--------------------
at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/node_modules/mysql/lib/Connection.js:130:18)
at /node_modules/sequelize/lib/dialects/mysql/connection-manager.js:75:16
at Promise._execute (/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/node_modules/bluebird/js/release/promise.js:77:14)
at ConnectionManager.connect (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:55:10)
at ConnectionManager.<anonymous> (/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:247:43)
at ConnectionManager.tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/node_modules/bluebird/js/release/async.js:143:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true } }
I suspect the problem is Sequelize tries to translate "localhost" which is given in the program to "127.0.0.1", and accessing mysql via 127.0.0.1 does not work inside container.
Thanks for the help.
solved (well, in a way), using dialectOptions
dialectOptions: {
socketPath: '/var/run/mysqld/mysqld.sock',
...
}
please i'm using expressjs in nodejs. Am actually new to nodejs.
Am trying to connect to mysql with the my usual credentials but i get the following errors.
Please help me out. Thank you.
"C:\Program Files (x86)\JetBrains\IntelliJ IDEA 15.0\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" bin\www
Tue, 08 Dec 2015 21:39:57 GMT NodeApp:server Listening on port 3306
c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\Parser.js:82
throw err;
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: YES)
at Handshake.Sequence._packetToError (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\Protocol.js:274:23)
at Parser.write (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\Connection.js:96:28)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
--------------------
at Protocol._enqueue (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\Protocol.js:135:48)
at Protocol.handshake (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (c:\Users\John\IdeaProjects\NodeApp\node_modules\mysql\lib\Connection.js:123:18)
at Object.<anonymous> (c:\Users\John\IdeaProjects\NodeApp\routes\about.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.require (module.js:366:17)
at require (module.js:385:17)
Process finished with exit code 1
As shown in the readme you should use user instead of username:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'db_name'
})
connection.connect();
for user : and password: please write them all in small letter if any of the letter of the mentioned captions( user and password) becomes uppercase it will display the error
'ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: YES)'
on the contrary, Host and database is not case sensitive
Host : 'localhost',
user : 'user_username',// if you create a user otherwise use 'root'
Database : 'the database',
password : 'your password',