Node JS remote mysql database connection error - mysql

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.

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.

mariadb for node.js ER_ACCESS_DENIED_ERROR Access denied for user

I am using the following code to establish connection to a MariaDB 10 server:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "local_ip",
user: "username",
password: "my_password",
port: 3307,
database: "name_of_db",
});
con.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
The SQL-Server is running on a machine within a local network.
The SQL-User is granted connections from all hosts (%).
I am able to establish a connection with a SQL GUI tool, with exact the same user.
Seems to me like a bug inside the mysql node package, anybody encountered such a problem?

node.js Mysql connection error

I'm trying to connect a Node.js app with a MySQL database which works for another app really well on a remote server with this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "www.<dnw>.com",
port: <correct port number>,
user: "<correct Username>",
password: "<correct password>",
database: "<correct databasename>"
});
con.connect(function(err) {
if (err)
{
throw err;
}
});
con.end();
On execution I get this as a error message
/home/akiku/node/dnwbot/main.js:49
throw err;
^
Error: connect ECONNREFUSED 85.25.34.68: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 (/home/akiku/node/dnwbot/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/akiku/node/dnwbot/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/akiku/node/dnwbot/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/akiku/node/dnwbot/main.js:46:9)
at promises.push.Promise (/home/akiku/node/dnwbot/node_modules/telebot/lib/telebot.js:439:29)
at new Promise (<anonymous>)
at TeleBot.event (/home/akiku/node/dnwbot/node_modules/telebot/lib/telebot.js:432:32)
at promise.then (/home/akiku/node/dnwbot/node_modules/telebot/lib/updates.js:92:33)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
What I thought it could be:
I checked the port and the host multiple times via SQL Query in phpmyAdmin. They are correct.
And I enabled in Plesk that ANY host can access the database.
The username and password is also correct.
What it should do:
Obviously just connect to the database. Nothing more.
Do you habe any clues what it could be?
Check you have access to remote server database.Try to connect using workbench or any other tool
I finally found the problem: The serveradmin blocked the port on a deeper server level which can't be accessed by Plesk.

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.

connect ECONNREFUSED - node js , sql

I have the next code in a js file:
var mysql = require('mysql');
var TEST_DATABASE = 'nodejs_mysql_test';
var TEST_TABLE = 'test';
var client = mysql.createClient({
user: 'root',
password: 'root',
});
client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
throw err;
}
});
But I get this error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:632:11)
at Object.afterConnect [as oncomplete] (net.js:623:18)
As I understand it, this is a connection problem - but how do I solve it?
( I'm working on windows 7)
Thanks!!
I know two ways to solve it:
In mysql.conf, comment skip-networking.
Try to set the socket like this:
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});
I got this error when MySQL Server was not running.
I changed my configuration via Initialize Database in MySQL.PrefPane, the System Preferences tool for MySQL on OS X - to Use Legacy Password Encryption - to fix ER_NOT_SUPPORTED_AUTH_MODE.
This config change stopped MySQL Server and then I got ECONNREFUSED when I tried to connect to MySQL from node.js.
Fixed by restarting MySQL Server from the MySQL System Preferences tool.
Try to fix your defined port in mysql and your Node.js script.
You can define the mysqld port in the *.cnf file inside the mysql directory,
and you can define that port when you connect to MySQL in your Node.js script.
Something like this in the cnf file
port = 3306