node.js Mysql connection error - mysql

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.

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!

how do i connect to a remote mysql database using node?

i have a node application that is duplicated both on the server (digital ocean) as well as my local machine. im trying to configure the local version so that i can use it as a 'sandbox' environment to make changes and edits before i push the update to the live application on the server.
the databaseconnection.js file contains the following.
const mysql = require('mysql');
function getConnection() {
return mysql.createPool({
connectionLimit: 100,
host: 'ipaddress',
port: 3306,
user: 'forge',
password: 'password',
database: 'mydatabase',
multipleStatements: true
});
}
module.exports = getConnection();
when i access the node application that is stored on the DO server, everything run fine, however, when i try to run it from my localhost environment, i get the following error.
Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:48:16)
at Pool.query (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:202:8)
at Strategy.passport.use.OutlookStrategy [as _verify] (C:\MAMP\htdocs\WIGHTcloudDEV\config\passport-setup.js:58:18)
at C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-oauth2\lib\strategy.js:202:24
at Request._callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-outlook\lib\strategy.js:157:5)
at Request.self.callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
i slightly remember doing something with either ssh2 or tunnel forwarder in the past that pertained to this issue but i cant find any of my old files nor can i find anything online that clearly shows what im missing.

Trouble connecting to mysql in NodeJs on Windows 10

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

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.

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).