Problem join database mysql in virtual machine with express node - mysql

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

Related

Unable to connect to cpanel's phpmyadmin database with node.js

I'm starting to experiment and explore node.js for a project which involves using tables. So far i've created a MySQL user and database within cpanel and have granted the user full access to the database. I've read the documentation for the npm mysql , but am unable to establish a connection, have tried adding socket path but still does not work, any help would be appreciated.
Node.js
var express = require('express');
var app = express();
var mysql = require('mysql');
var con= mysql.createConnection({
connectionLimit : 100,
host : 'localhost',
port : 3306,
user : '*******',
password : '*******',
database : '*******',
});
con.query('SELECT * FROM users', function(err, results) {
if(err){
console.log('Query error: ', err);
return;
}
console.log(results);
});
Error Message
Query error: Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (E:\Pharmabot\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (E:\Pharmabot\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (E:\Pharmabot\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (E:\Pharmabot\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (E:\Pharmabot\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (E:\Pharmabot\database\dbconnect.js:14:5)
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
}
}
there are some great packages, you must try theme first. there are some packages that support all db server language like sql, mysql2, mongodb, postgres etc.

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;

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

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.