connect nodeJS to MySQL when computer is offline - mysql

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); });

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.

Connect to RDS MySQL programatically (NodeJs, Java) - ER_ACCESS_DENIED_ERROR: Access denied for user

I am trying to connect to my RDS (AWS MySQL database) using NodeJs mysql library.
const mysql = require('mysql');
var connection;
connection = mysql.createConnection({
host: 'xxx.us-east-1.rds.amazonaws.com',
port: 3306,
user: "username",
password: "MyPassword",
database: "DbName",
});
I was also trying mysql2 and sequelize. All of them got me the same result:
{
error: ER_ACCESS_DENIED_ERROR: Access denied for user 'username'#'myhost,
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000'
}
However, this remote database is set to public and I am able to connect to it with mysql command:
mysql -u username -pMyPassowrd -h xxx.us-east-1.rds.amazonaws.com
I am also able to connect to it with MySQL Workbench.
Also, the problem is not in NodeJs, because I am able to connect to my local database:
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'test',
});
In conclusion, my AWS database is publicly available and I can connect to it, just not using NodeJs for some reason.
I have not found a useful answer yet. Did anyone encounter this problem ?
UPDATE:
Trying to connect in Java with simple Connection returns the same result as well:
final Connection con = DriverManager.getConnection("jdbc:mysql:/xxx.us-east-1.rds.amazonaws.com:3306/DbName", "username", "MyPassword");
UPDATE 2:
I was missing one slash for Java code. I needed to use jdbc:mysql:// instead of jdbc:mysql:/. But NodeJs implementation is still one big mystery.
The answer was rather simple, just add an ssl option with true value
connection = mysql.createConnection({
host: 'xxx.us-east-1.rds.amazonaws.com',
port: 3306,
user: "username",
password: "MyPassword",
database: "DbName",
ssl: true //this does the trick
});

error : connect ECONNREFUSED

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.

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

node.js-mysql connection failed on windows7

I am just getting started with nodejs.
My question is that i get an Error:
connect ECONNREFUSED
npm install mysql;
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
port: '3306'
});
connection.connect(function(err) {
if(err) {
console.log(err);
}
});
connection.end();
Finally
1.I don't know the "user" and "password";
2."In mysql.conf, comment skip-networking.", I can't find mysql.conf;
3.I have tried this and it's not working, my platform is windows 7.
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});
It's clear you have not install mysql server in your windows yet. So you should go to http://www.mysql.com and download the server, install and set up, you can set your username and password and grant privilege to it, so you will know the user and password, and mysql.conf will be inside your installation. And if you are in windows, generally you would not use a UNIX socket to build the connection. So "_socket: '/var/run/mysqld/mysqld.sock'" should not be here.