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:
Related
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!
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.
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.
I'm trying to create a link between node.js and mysql. I get an error as connect ECONNREFUSED. how to solve ths?
CODE:
var mysql = require('mysql');
var conn = mysql.createConnection({
host : "192.164.0.114",
user : "supriya",
password : "root123#",
database: "student"
}
);
var queryString = "SELECT * FROM studata";
conn.query("someurlhere",queryString, function(error, results)
{
if(error)
{
throw error;
}
else {
console.log(results);
}
})
conn.end();
Error is:
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
--------------------
at Protocol._enqueue (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/supriya/node_modules/mysql/lib/Connection.js:109:18)
at Connection._implyConnect (/home/supriya/node_modules/mysql/lib/Connection.js:387:10)
at Connection.query (/home/supriya/node_modules/mysql/lib/Connection.js:174:8)
at Object.<anonymous> (/home/supriya/node_js/example2.js:19:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I had the same problem. I invoked node server.js, which says
It's Started on PORT 3000 Problem with MySQLError: connect
ECONNREFUSED
By first line, I assumed, intially, it starts mysql server. However, that is not the case. it starts node server, which tries to connect to mysql server and node server says ECONNREFUSED. Clearly, mysql server was not running. I found this, when I logged in, as root, in mysql workbench and tried to execute simple query. There in the corner it said, "..at port 3306, it is not connected" or sth like that.
So, conclusion is starting server.js on some port starts node server and not mysql server.
I have a nodejs (with express) openshift app that has a MySQL database (because that is what I am most familiar with), and I cannot figure out how to connect to it on the local environment. My last project I used a php framework with mamp to connect to the local database. What is the equivalent for node? This is the code I have to connect:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
port : '3307',
database : 'test'
});
connection.connect( function(err){
if (err){
throw err;
}
else {
console.log('Connected');
}
});
Here is the error when trying to connect locally:
Error: connect ETIMEDOUT
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
I am guessing that because I am trying to locally connect to the MySQL db set up by openshift, that I am receiving errors. I just dont know what else to try here. I have tried it without the password as well.
If I switch to localhost for the host, this is the error I receive:
Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''#'localhost' to database 'stembuds'
at Handshake.Sequence._packetToError (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Handshake.js:91:18)
at Protocol._parsePacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:202:24)
at Parser.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:72:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:736:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Sorry for the length of this post, I just have no idea why it isnt working.
EDIT:
I ssh'ed into the openshift shell to look at the mysql settings. When I do the 'show grants\g' command, this is what I receive:
Grants for adminMYXaSuf#127.11.28.130
GRANT ALL PRIVILEGES ON *.* TO 'adminMYXaSuf'#'127.11.28.130' IDENTIFIED BY PASSWORD '*D0CE5FA2AAD801A33C9190D0B615CC92F364BE6B' WITH GRANT OPTION
This password is different than the one openshift provided.
did you read their doc?.
if you have load balancer, your mysql will install on another gear.
you need to use the env variable when you install the cartridge
port
OPENSHIFT_MYSQL_DB_PORT=44331
host
OPENSHIFT_MYSQL_DB_HOST=12345600a4382ec97a10000f7-somehost.rhcloud.com
OPENSHIFT_MYSQL_DB_PASSWORD=somerandompassword
username
OPENSHIFT_MYSQL_DB_USERNAME=yourusername
pass the variable when you start your app as args.
look at the example for node
https://github.com/openshift-quickstart/nodejs-example/blob/master/server.js
just use the env variable like process.env.OPENSHIFT_MYSQL_DB_HOST to replace the string in the database connection.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : process.env.OPENSHIFT_MYSQL_DB_HOST,
user : process.env.OPENSHIFT_MYSQL_DB_USERNAME,
password : process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
port : process.env.OPENSHIFT_MYSQL_DB_PORT,
database : process.env.OPENSHIFT_APP_NAME
});
It looks like you are running this on your local computer? If you want to connect to your mysql database on OpenShift from your local computer, you need to use the rhc port-forward command, and look at the port it gives you for mysql, so then you would set the ip to 127.0.0.1 and the port to something like 3307 or whatever it gives you locally. Keep the port-forward command running while you test the connection. You could also try pushing your code up to openshift and run it with the values that you have (or use the mysql environment variables that you can get with env | grep MYSQL on your gear) and then see if it works.
127.x.x.x are not publicly available ip addresses, they are private to that server, that is why you can't connect to it from your machine, and need to use the rhc port-forward command
This works for me:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
port : '3306',
user : 'admind4DxYD5',
password : 'some_pass',
database : 'developercorey'
});
connection.connect( function(err){
if (err){
throw err;
}
else {
console.log('Connected');
}
});