error : connect ECONNREFUSED - mysql

I am running a node app with mysql as my database(also using sequelize as ORM). Whenever I run the "app.js" file with "node" command, I get an error:
{ [Error: connect ECONNREFUSED 127.0.0.1:3306]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true }
my code in the app.js file:
var mysql = require("mysql");
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "openshare"
});
connection.connect(function(err){
if(err){
console.log(err);
} else {
console.log("no errors");
}
});

I probably should have mentioned before hand, but I am using cloud9 to for my node environment. Cloud9 has some things setup for you already:
Your "host" to connect to will just be "localhost".
"user" will be your cloud9 username.
"password will be left blank.
"database" will be "c9"
var connection = mysql.createConnection({
host: "localhost",
user: "seth40047",
password: "",
database: "c9"
});
https://community.c9.io/t/setting-up-mysql/1718
This article is very helpful for anyone who is also having troubles setting up MySql on a cloud9 node environment.

I had the same issue trying to connect node to a database in Cloud9. he fix was to
run the database and node in the same Cloud9 workspace.
Though this might be obvious to more experienced coders, I wrongly thought that I had the two workspaces communicating.

Related

Connection refused when connecting to MySql using NodeJS mysql2

I am running a NodeJS app hosted on a (linux) dedicated Plesk server, under a subdomain.
Trying to connect to a MariaDB mysql server.
The NodeJS code:
const mysql = require('mysql2');
const con = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
database: "myDatabase"
});
global.MYSQL_CONNECTION = con;
function Connect(onSuccess){
con.connect(function(err) {
if (err) throw err;
console.log("MySQL connected successfully.");
onSuccess(con);
});
}
When I run this I get the following error:
Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3306,
fatal: true
}
However, when I run mysql -hlocalhost -uusername -ppassword on my bash terminal it connects fine, meaning the credentials are correct and the user has permissions.
What could be causing this?
Figured out the solution to this.
All I had to do was to change the host to 127.0.0.1 :
const con = mysql.createConnection({
host: "127.0.0.1",
user: "username",
password: "password",
database: "myDatabase"
});
I am unsure of the reason. It most probably has something to do with the fact that this runs on a subdomain. Would be great if anyone could specify the reason.

Is there any solution to the timeout error in the connection of mysql with node in aws?

I am new to deploying in general and I am trying to deploy my back-end node project in aws. the mysql database is successfully deployed and works locally, meanwhile when I try to start the project in production it gives me a timeout error.
Error: connect ETIMEDOUT
at Connection._handleTimeoutError (/home/ubuntu/gmd-server/node_modules/mysql2/lib/connection.js:178:17)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
I tried to extend the connection time in the node project but nothing changed.
My connection string:
const connection = mysql.createConnection({
host: 'XXXX',
user: 'XXXX',
password: 'XXXX',
database: 'XXXX',
});
How can I fix this problem?
Thanks in advance.

connect nodeJS to MySQL when computer is offline

When the computer is offline, then my Electron app can't connect to the local MySQL.
When the computer connected to the internet, everything works fine.
Tested on Windows only.
The same happens in nodeJS (command prompt) and in Electron.
code:
s = {database: "test", user: "test", password: "test", host: "localhost"}
var mysql = require('./mysql');
var mysqlc = mysql.createConnection(settings);
mysqlc.connect(function(err) { console.log(err); });
The error code is:
{ [Error: getaddrinfo ENOENT localhost:3306]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'getaddrinfo',
hostname: 'localhost',
host: 'localhost',
port: 3306,
fatal: true }
Why ?
What can I do about it ?
Windows has an issue with resolving localhost to a physical IP address when it is not connected to a network. Apparently what happens is that on Windows is when you give the address localhost it passes it to a full DNS resolver which requires a connection to the internet to work properly.
Found a good answer about a possible why here: Windows 7: “localhost name resolution is handled within DNS itself”. Why?
Try using the IP address itself:
s = {database: "test", user: "test", password: "test", host: "127.0.0.1"}
var mysql = require('./mysql');
var mysqlc = mysql.createConnection(settings);
mysqlc.connect(function(err) { console.log(err); });

Connect to AWS MySQL database via Node JS

I have an instance on AWS and MySQL database working on it.
I was able to connect to the instance via workbench on my local machine.
However, I am not able to connect to the database via the node js code.
Below is the snippet :
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'ec2-52-33-41-xxx.us-west-2.compute.amazonaws.com',
port : 3306,
user : 'ec2-user',
password : 'root',
database : 'FAMILY_GIVING_TREE'
});
var app = express();
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... ");
} else {
console.log("Error connecting database ... ");
}
});
THE ERROR is :
{ [Error: connect ECONNREFUSED 52.33.xx.84:3306]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '52.33.41.84',
port: 3306,
fatal: true }
The node js code is w=not written over the AWS instance, I am writing it on my machine and yes the port 3306 is enabled in security groups.
I get the database connection issue.
Any idea where am I going wrong?
Thanks in advance.
There is a syntax error in your node.js code. Replace port : 3306, with below you are missing the ''
port : '3306',
First, you must do the settings, that make Mysql accessible remotely.
https://mariolurig.com/coding/connect-remotely-mysql-database-amazon-ec2-server/
Second, in my case, I had to use my ec2 public IP address instead of public DNS.
So:
var connection = mysql.createConnection({
host : '52.33.41.xx',
port : '3306',
user : 'remote',
password : 'password',
database : 'FAMILY_GIVING_TREE'
});
Third, keep in mind that 'remote' is the user you grant all privilege on your DB for localhost and '%' (from all IP ).
GoodLuck

Why do I have to connect to Internet when I am using MySQL in NodeJS?

I have a strange issue when using MySQL in NodeJS. When I connect to the Internet, my MySQL database works, however, when I'm offline, querying the MySQL database gives me an error. I'm using an Express connection.
My connection:
app.use(
connection( mysql, {
host: dbConfig.host,
user: dbConfig.user,
password: dbConfig.password,
database: dbConfig.database
}, 'pool' )
);
Error message:
{
[Error: getaddrinfo ENOENT localhost:3306]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'getaddrinfo',
hostname: 'localhost',
host: 'localhost',
port: 3306,
fatal: true
}
And:
connection.query(
^
TypeError: Cannot read property 'query' of undefined
Most likely you're missing a localhost entry in your /etc/hosts file.