Trouble connecting to mysql in NodeJs on Windows 10 - mysql

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

Related

Node.js "Error: connect ECONNREFUSED 127.0.0.1:3306" when trying to connect to MySQL database

I'm a novice at node.js and I want to create an API which sends data to a MySQL server to store them.
I followed a youtube tutorial to create a connection with a MySQL database, also checked other tutorials just to be sure, but I always get this error message when my code tries to connect to MySQL. I used XAMPP first, and then tried downloading and using MySQL from mysql.com, but got the same error.
/home/broken/mysql_projekt/app.js:24
throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)
--------------------
at Protocol._enqueue (/home/broken/mysql_projekt/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/broken/mysql_projekt/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/broken/mysql_projekt/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/home/broken/mysql_projekt/app.js:22:4)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
I tried the suggested solutions at other questions but I didn't succeed.
Here's what I tried:
Changing localhost to 127.0.0.1 in the connection configuration.
Check in services.msc if the MySQL service is in a running state.
Tried to add firewall rules to allow outbound and inbound access on
port 3306.
Create another user aside from root with a password with all
privileges, and try to connect with that.
Also tried to add "socketPath: '/var/run/mysqld/mysqld.sock'" to my
connection configuration, as someone in another
question
said that it seemed to solve other's problem, but they just got a
different error message, just like me. (unfortunately I couldn't really understand what it does)
Also tried on another computer, but of course the issue still
remained.
Tried to specify the port number to 3306 in the connection
configuration.
Here is my code:
var express = require('express');
var mysql = require('mysql');
var PORT = 3000;
var app = express();
app.listen(
PORT,
() => {console.log(`Server started on port ${PORT}`)
})
//Creating MySQL connection
var db = mysql.createConnection({
host : "localhost",
user : "user",
password : "password"
//socketPath: '/var/run/mysqld/mysqld.sock'
});
//Connecting
db.connect((err) => {
if(err){
throw err;
}
console.log('MySQL connected');
});
If someone has figured it out, could you share the solution? Thank you!

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.

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

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.

Connecting to MySQL through Node.js

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.