Connecting to MySQL through Node.js - mysql

I am trying to connect to MySQL database using Javascript through the code I found online:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "Restaurants",
socketPath: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company
Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Per online suggestions, I have added the following path to the socket:
socketPath: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'
Running this file through the Terminal, I get the following error:
Error: connect ELOOP
/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
at Protocol._enqueue
(/Users/aliaksandrnenartovich/node_modules/mysql
/lib/protocol/Protocol.js:144:48)
at Protocol.handshake
(/Users/aliaksandrnenartovich/node_modules/mysql
/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/aliaksandrnenartovich/node_modules
/mysql/lib/Connection.js:119:18)
at Object.<anonymous>
(/Users/aliaksandrnenartovich/Desktop/JScript/te.js:11:5)
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)
I have XAMPP 5.6.33-0 installed, and I can successfully start the Apache server and MySQL database manually, but when I try to connect through Node.js it does not work. I have researched this error online but there seems to be little information as to what may be going on. I understand that this has something to do with mysql.sock file but I am just not sure how to deal with it.
Any suggestions will be greatly appreciated. Thank you in advance!

Have you successfully connected to the database with those credentials/parameters using some SQL client like MySQL Workbench? Just to be sure they are correct.

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.

Trouble connecting to mysql in NodeJs on Windows 10

I have tried to connect NodeJS to mysql on Windows 10, but continuously failed to.
The following is the code, which from https://www.w3schools.com/nodejs/nodejs_mysql_create_db.asp
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
and the following is the error message when I run code.
if(err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16)
--------------------
at Protocol._enqueue (C:\Users\tete6\NodeJS\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\tete6\NodeJS\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\tete6\NodeJS\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\tete6\NodeJS\demo_db_connection.js:9:5)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11 {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
To solve this error, I tried ...
change "localhost" -> "127.0.0.1", but failed.
add "port: 3306", but failed.
check if the port is available to be connected, and I found that it's never my port, fireWall problem.
My xampp's mysql works well.
Even mongoDB module triggers this same error message.
I gleaned some info that this econnrefused error message is mainly the problem of NodeJS, not of sql module.
Is anyone to show me silver shining from this?
Have you started your XAMPP Server?
connect ECONNREFUSED 127.0.0.1:3306 -- this error came, when you not started your XAMPP server.
At the first you should right install mysql into windows 10
and see this video link how to connect to mysql with node js

Getting ECONNREFUSED 127.0.0.1:3306 error while conencting toa MySQL db using Node.js

I am trying to connect to a MySQL server via node.js but getting ECONNREFUSED 127.0.0.1:3306.
I am running node from my windows 10 desktop and MySQL is running on a remote Linux server.I have admin access to MySQL but just cant reach to it via node.
My Code:
var conn = mysql.createConnection({
hostname: "My mySQL server",
port: 3306,
user: "adminid",
password: "adminpassword"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected");
conn.end();
});
=================================
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
--------------------
at Protocol._enqueue
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
The argument is host not hostname, and since you didn't specify a host argument it's defaulting to 'localhost'.
Change your init:
let conn = mysql.createConnection({
host: "My mySQL server",
port: 3306,
user: "adminid",
password: "adminpassword"
});
Always double-check the documentation when you have issues like this.

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!

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.