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

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.

Related

How to connect azure mysql server with nodeJS

I am trying to connect to a remote mysql server with nodejs
So far I am able to connect with mysql workbench 8.0, so no problem with mysql server. And I can easily connect to my local mysql-server (with "mysql2" module).
Here is something I've been trying to connect with remote mysql server provided by Azure. I followed the guides that is provided.
const mysql = require("mysql");
const fs = require("fs")
var connection = mysql.createConnection({
host: process.env.MYSQL_SERVER_HOST,
user: process.env.MYSQL_SERVER_USER,
password: process.env.MYSQL_SERVER_PASSWORD,
database: process.env.MYSQL_SERVER_DATABASE,
port:3306,
ssl: { ca: fs.readFileSync("./certificates/DigiCertGlobalRootCA.crt.pem")}
});
// This is how I am connecting to my local mysql-server which is working fine
// // const connection = mysql.createConnection({
// host: 'localhost',
// user: 'root',
// password: process.env.LOCAL_MYSQL_SERVER_PASSWORD,
// database: 'db',
// port: '3306'
// });
connection.connect((error) => {
if (error) {
console.log("mysql error")
console.log(error)
}
else {
console.log("Mysql Ready")
}
});
module.exports = connection;
Here I am using mysql package because many articles on web suggested it but I even used mysql2 and nothing happened. I even changed the networking setting and allowed my IP address for access(this wasn't necessary though).
Error logs:
mysql error
Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (path\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (node:events:641:28)
at Socket.emit (node:events:527:28)
at Socket._onTimeout (node:net:516:8)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
--------------------
at Protocol._enqueue (path\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (path\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (path\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (path\packnary\database\mysql.js:26:12)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
I connected Azure SQL database to the nodejs without using code.
I followed below procedure:
I have created azure blank nodeJs webapp in visual studio.
It run successfully. after that I go to overview of the webapp and selected connected services and clicked on a add a service dependency option and searched for azure sql database.
Image for reference:
select the azure sql data and click on next button.
login to azure account and select which database we need to connect.
Image for reference:
Enter the details required in the next window and click on next.
Image for reference:
Select the changes according to the requirements and click on next.
Image for reference:
It is connected successfully.
Image for reference:

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!

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

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.

Getting error when I connect sails js with mysql (node - v7.5.0)

error: A hook (`orm`) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: connect ECONNREFUSED 127.0.0.1:3306
at afterwards (/root/sails/sailsUserApp/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
at /root/sails/sailsUserApp/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Handshake.onConnect [as _callback] (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
at Handshake.Sequence.end (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at Protocol.handleNetworkError (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:364:14)
at PoolConnection.Connection._handleNetworkError (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:421:18)
at emitOne (events.js:96:13)
at Socket.emit (events.js:189:7)
at emitErrorNT (net.js:1280:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
connection.js -
Mysql:
{
adapter: 'sails-mysql',
host: 'localhost',
port: '3306',
user: 'root', //optional
password: 'sipl#1234', //optional
database: 'sails_users' //optional
}
models.js - connection: 'Mysql',
I am unable to figure-out the issue. port: 3306 is already mentioned in the configuration.
The MySQL connection parameters will depend on the MySQL configuration. The configuration file location and default connection access control list will depend on the OS that MySQL is running on and the options that were passed to the installation.
It may be expecting a Unix Socket connection rather than a TCP connection. Review your MySQL configuration file to see which connections and users are allowed. If it is allowing only socket connections there will be a specific unix socket it is expecting to use.
With that information you can set the configuration parameters accordingly.
To use a socket connection the parameters would be:
Mysql:
{
adapter: 'sails-mysql',
socketPath: 'path-to-socket/mysql.sock',
user: 'mysql-user',
password: 'mysql-password',
database: 'mysql-database'
}