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

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!

Related

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

ECONNREFUSED Node.js server failing to connect to aws mysql instance

I am trying to connect to a mysql database hosted by aws. I have no problem connecting with Mysql workbench, and I have allowed all traffic to the database in order to test, but I am still unable to connect with the server.
I have reproduced my error with this code:
var mysql = require('mysql');
var db_config = require('../config/database')
var pool = mysql.createConnection(db_config);
pool.query("SELECT * FROM users", function (error, results, fields) {
if (error) {
throw error;
}
})
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
--------------------
at Protocol._enqueue (/Users/Amin/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/Amin/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/Amin/node_modules/mysql/lib/Connection.js:118:18)
at Connection._implyConnect (/Users/Amin/node_modules/mysql/lib/Connection.js:453:10)
at Connection.query (/Users/Amin/node_modules/mysql/lib/Connection.js:198:8)
at Object.<anonymous> (/Users/Amin/Projects/Relation-Quest/backend/tests/db-test.js:7:6)
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)
Any debug ideas?

Establishing a connection between Node.js and MySQL

I'm a complete newbie to Node.js, but I do have some experience with mysql and some other languages.
I am trying to establish a database connection between Node.js and MySQL, however, my terminal keeps on giving me errors. It failed, so I tried to debug it by using "error.stack" as stated from the documentation, that failed also so I'm totally lost now. Can someone please help me to figure out the bug.
Also, can anyone also tell me how to tell Node.js to print out the reason of why it failed to connect to the database like the function "mysql_error()" from PHP.
My original Node.js code:
var MySQL = require('mysql');
var DatabaseConnection = MySQL.createConnection({
host: '10.0.0.25',
user: 'Admin',
password: 'Password'
});
DatabaseConnection.connect(function(error) {
if(!error) {
console.log("Database is connected");
return;
} else {
console.log("Database is not connected: ");
console.log(error.stack);
}
});
My result:
Database is not connected:
Error: connect ECONNREFUSED 10.0.0.25:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
--------------------
at Protocol._enqueue (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/marc/Documents/JSON_MySQL_Database_Test/Database_Test.js:8:20)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)

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.

Error as connect ECONNREFUSED when using node.js and MySQL

I'm trying to create a link between node.js and mysql. I get an error as connect ECONNREFUSED. how to solve ths?
CODE:
var mysql = require('mysql');
var conn = mysql.createConnection({
host : "192.164.0.114",
user : "supriya",
password : "root123#",
database: "student"
}
);
var queryString = "SELECT * FROM studata";
conn.query("someurlhere",queryString, function(error, results)
{
if(error)
{
throw error;
}
else {
console.log(results);
}
})
conn.end();
Error is:
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
--------------------
at Protocol._enqueue (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/supriya/node_modules/mysql/lib/Connection.js:109:18)
at Connection._implyConnect (/home/supriya/node_modules/mysql/lib/Connection.js:387:10)
at Connection.query (/home/supriya/node_modules/mysql/lib/Connection.js:174:8)
at Object.<anonymous> (/home/supriya/node_js/example2.js:19:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I had the same problem. I invoked node server.js, which says
It's Started on PORT 3000 Problem with MySQLError: connect
ECONNREFUSED
By first line, I assumed, intially, it starts mysql server. However, that is not the case. it starts node server, which tries to connect to mysql server and node server says ECONNREFUSED. Clearly, mysql server was not running. I found this, when I logged in, as root, in mysql workbench and tried to execute simple query. There in the corner it said, "..at port 3306, it is not connected" or sth like that.
So, conclusion is starting server.js on some port starts node server and not mysql server.