Connection refused when connecting to MySql using NodeJS mysql2 - mysql

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.

Related

MagicMirror SQL connection gets ENOTFOUND error even with an IP address

I am using MagicMirror project that can be found on the Github. I have added the MMM-MysqlQuery module. And i have added the database config to the config file. but still gets ENOTFOUND error.
project : https://github.com/MichMich/MagicMirror.git
I have set up the code in a raspberry pi 2. and the mysql database is in the pi localhost.when I run the project using pi terminal i get the ENOTFOUND error.
config: {
connection: {
host: "http://192.168.8.100",
port: 80,
user: "root",
password: "123456789",
database: "mirror"
}
according to understanding this configurations are passed as parameters to the
MMM-MysqlQuery modules' config.js file. which has the db connection initialization.
var mysql = require("mysql");
var con = mysql.createConnection({
host: process.argv[2],
port: process.argv[3],
user: process.argv[4],
password: process.argv[5],
database: process.argv[6]
});
con.connect(function(err) {
if (err) throw err;
con.query(process.argv[7], function(err, result) {
if (err) throw err;
console.log(result);
});
});
And the error i get is like this
Whoops! There was an uncaught exception...
{ Error: getaddrinfo ENOTFOUND http://192.168.8.100
http://192.168.8.100:80
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
--------------------
at Protocol._enqueue (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_modules/mysql/lib/Connection.js:118:18)
at Class.socketNotificationReceived (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_helper.js:25:17)
at Socket.<anonymous> (/home/pi/MagicMirror/modules/node_modules/node_helper/index.js:113:11)
at emitTwo (events.js:126:13)
at Socket.emit (events.js:214:7)
at /home/pi/MagicMirror/node_modules/socket.io/lib/socket.js:513:12
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'http://192.168.8.100',
host: 'http://192.168.8.100',
port: 80,
fatal: true }
please let me know if the code is not enough. and thank you guys for any help I can get.
http://192.168.8.100 isn't a valid hostname, because it isn't a hostname at all -- it's a URL, and includes the scheme http://, which also isn't appropriate for a database connection.
Use simply host: "192.168.8.100".
The next error you will encounter is a side effect of port: 80 also being incorrect. Port 80 is the standard port for an HTTP (web) server... but you are connecting to a database, not a web server. For MySQL, the correct value would be port: 3306 unless your MySQL installation uses a custom port (which it shouldn't).

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

Node JS remote mysql database connection error

I am relatively new to the Node JS, and I have been trying to connect to a remote mysql server, but I have been unable to do so. I have been looking for the solutions on here but most of them are for localhost. Here is my code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "###.ipagemysql.com",
user: "user",
password: "mypass",
database: "mydb",
debug: true
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I am getting the error below:
Error: connect ECONNREFUSED 103.224.212.250:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
UPDATE:
I tried using a different MySQL server and that works fine. Could it be because iPage is blocking the connection because it is coming from a foreign machine? Is this something that can be configured in phpmyadmin?
Possibly port 3306 is not accessible from the application. You can try telnet to server 103.224.212.250:3306 to see if it is opened or not.

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.

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.