I cannot connect to mysql database using node.js - mysql

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;

Related

im trying to connect mysql with nodejs but have error

im trying to connect mysql with nodejs but have error
i think its a mysql problem not nodejs .
this is the error
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
--------------------
at Protocol._enqueue (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\Owner\Desktop\MySQL\server.js:23:12)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47 {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
also i dont have mysql in my task manager
i think i did something but i got error before it disapeared
my js code is
const mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : 3306,
user : 'root',
password : 'philipthe1',
database : 'todo'
});
connection.connect();
connection.query("select * from items",(error,res,fields)=>{
if(error){
console.error(error);
}
else{
console.log(res)
}
})
connection.end();
It is a mysql error, cuz it says you cannot connect to db.
Can you add the code in which you're trying to connect?
i was able to restart mysql by doing windows key and r on keyboard then services.msc
then it gave option start server
the docs for it is in mysql docs under restarting server

issue connecting to Mysql through java script

I am trying to authenticate and user by connecting to mysql DB where i have information of candidate id and there passwords. As watched in demos I have created in "server.js" file, in which I am trying to create a new connection and printing if connection is established.
const connection = mysql.createConnection({
host: '51.51.x.x',
port: 3306,
user: 'intrinsic',
password: 'mypassword',
database : 'practisework'
});
connection.connect(function(err){
(err)? console.log(err): console.log(connection);
});
and when i run the code with node server command i got the following error.
code: 'ER_ACCESS_DENIED_ERROR',
at Protocol._enqueue (C:\Users\ISL Lohitha\Documents\CareerGuidance\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\ISL Lohitha\Documents\CareerGuidance\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\ISL Lohitha\Documents\CareerGuidance\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\ISL Lohitha\Documents\CareerGuidance\server.js:19:12)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: "Access denied for user 'intrinsic'#'175.101.99.33' (using password: YES)",
sqlState: '28000',
fatal: true
}
It is routing to some unknown IP because the ip that i gave is not 175.101.99.33 which not even my local ip (192.10.65.39). I tried access the server database from my PC through MySQL workbench and it had no issues. Any help required on how to connect to database.

Problem join database mysql in virtual machine with express node

I have a virtual machine on which is install ubuntu server with mysql and phpmyadmin I try to connect through express node js.
If I type 192.168.11.120/phpmyadmin (virtual machine address in the network) on the physical machine's browser, I can access it.
After doing a npm install mysql, it's my code
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.11.120',
user : 'test',
password : 'test'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields)
{
if (err) {console.log(err)} ;
});
connection.end();
The console shows me the following error after doing a "node index.js".
{ Error: connect ECONNREFUSED 192.168.11.120:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
--------------------
at Protocol._enqueue (C:\Users\Boubacar\Desktop\ISTA\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Boubacar\Desktop\ISTA\server\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Boubacar\Desktop\ISTA\server\node_modules\mysql\lib\Connection.js:119:18)
at Object.<anonymous> (C:\Users\Boubacar\Desktop\ISTA\server\index.js:8:12)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.11.120',
port: 3306,
fatal: true }
I can not find out if it's a problem related to virtualbox or it's the code that is not good

node.js MySQL connection not established - only when using XAMPP

I'm trying to establish a MySQL connection with my node.js server, but when I do, I get the following error:
F:\Programmering\nodejs-server>node dbConnection.js
F:\Programmering\nodejs-server\dbConnection.js:10
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
--------------------
at Protocol._enqueue (F:\Programmering\nodejs-server\node_modules\mysql\lib\
protocol\Protocol.js:145:48)
at Protocol.handshake (F:\Programmering\nodejs-server\node_modules\mysql\lib
\protocol\Protocol.js:52:23)
at Connection.connect (F:\Programmering\nodejs-server\node_modules\mysql\lib
\Connection.js:130:18)
at Object.<anonymous> (F:\Programmering\nodejs-server\dbConnection.js:9:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
I've tried using other ports, but still same error message. When I use XAMPP, and start MySQL service, it connects fine. These are the messages when I install mysql using npm:
F:\Programmering\nodejs-server>npm install mysql
npm WARN nodejs-server#1.0.0 No repository field.
+ mysql#2.15.0
added 11 packages in 7.809s
I have been using the w3schools tutorial. This is my code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "myusername",
password: "mypassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
What am I doing wrong? Please help! Thank you!

Why is node script failing a localhost connect to MySQL

I can't figure out why a script to is failing to connect to the MySQL instance.
I can connect to the database from the mysql client on the command line, with the exact same credentials.
I can connect to the database from a remote client, using the same credentials.
The database connect script works fine if I set the parameters to a remote database.
Neither host or socket parameters works through the script.
I make dumb mistakes at times but if this is another one, I can't see it.
An outside observer would help me here.
==================== DB Connect code
var db = require('./config');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: db.db_parms.host,
port: db.db_parms.port,
user: db.db_parms.user,
password: db.db_parms.password,
database: db.db_parms.context,
debug:true
});
connection.connect(function(err) {
if(err){
console.log("SQL CONNECT ERROR: " + err);
} else {
console.log("SQL CONNECT SUCCESSFUL.");
}
});
connection.query("SELECT now()", function(error,results,fields) {
console.log("Results:" + results);
console.log("Fields:" + fields);
});
exports.connection = connection;
=== Error Stack returned:
SQL CONNECT ERROR: Error: connect ECONNREFUSED 127.0.0.1:3307
at Object.exports._errnoException (util.js:1036:11)
at exports._exceptionWithHostPort (util.js:1059:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
========= config.js with host = 'localhost':
var db_parms = {
host: 'localhost',
port: 3307,
user:'foo',
password:'bar',
database:'thisone'
}
exports.db_parms = db_parms;
========= config.js with host = socket:
host: '/var/run/mysqld/mysqld.sock',
=== Error Stack returned:
SQL CONNECT ERROR: Error: getaddrinfo ENOTFOUND /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock:3307
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
I'm not sure if the error from the socket is a permissions issue or not and if that is relevant.
As it turns out, a check of the my.conf shows that the bind-addresss was set to the IP of the server and not to localhost.
Setting host: '<ip-address>' fixed the issue.