Connect to AWS MySQL database via Node JS - mysql

I have an instance on AWS and MySQL database working on it.
I was able to connect to the instance via workbench on my local machine.
However, I am not able to connect to the database via the node js code.
Below is the snippet :
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'ec2-52-33-41-xxx.us-west-2.compute.amazonaws.com',
port : 3306,
user : 'ec2-user',
password : 'root',
database : 'FAMILY_GIVING_TREE'
});
var app = express();
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... ");
} else {
console.log("Error connecting database ... ");
}
});
THE ERROR is :
{ [Error: connect ECONNREFUSED 52.33.xx.84:3306]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '52.33.41.84',
port: 3306,
fatal: true }
The node js code is w=not written over the AWS instance, I am writing it on my machine and yes the port 3306 is enabled in security groups.
I get the database connection issue.
Any idea where am I going wrong?
Thanks in advance.

There is a syntax error in your node.js code. Replace port : 3306, with below you are missing the ''
port : '3306',

First, you must do the settings, that make Mysql accessible remotely.
https://mariolurig.com/coding/connect-remotely-mysql-database-amazon-ec2-server/
Second, in my case, I had to use my ec2 public IP address instead of public DNS.
So:
var connection = mysql.createConnection({
host : '52.33.41.xx',
port : '3306',
user : 'remote',
password : 'password',
database : 'FAMILY_GIVING_TREE'
});
Third, keep in mind that 'remote' is the user you grant all privilege on your DB for localhost and '%' (from all IP ).
GoodLuck

Related

MySQL connection node

I'm running my node app on a linux vps where I have installed apache2 and phpmyadmin. I have my mysql database on the server there which I can connect to user the mysql -uusername -ppassword command, but when running my node app with this code:
const mysql = require('mysql');
const connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '*******',
database : 'db_name',
});
connection.connect(function(e) {
if(e) {
console.log('Database didn\'t connect');
} else {
console.log('Database connected successfully');
}
});
It says "database didn't connect". the user, password and db fields are all correct for sure.
When console.log(e) I'm getting:
I'm getting:
error code: 'ER_NOT_SUPPORTED_AUTH_MODE'
errorno: 1251
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client'.
anybody knows why ?
You can change the connection to use the unix socket or check the mysqld is binding on 0.0.0.0 or on the ip of the ethernet card of the server it's running on.
According to the mysqld version you're using, the option can be "skip-networking" or "bind-address".

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.

MySQL Connection on Node.JS

I have a problem when I try to connect to a MySQL database hosted by OVH on NodeJS server. Here is the code :
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'my_ip',
port : '3306',
user : 'my_user',
password : 'my_pass',
connectTimeout : 10000
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected');
});
But I get :
error connecting: Error: connect ETIMEDOUT
Everytime, not matter what I do, like changing the timeout, remove the port or anything else. Any idea ? I'm running this on ArchLinux x86_64
I finally found the answer : OVH doesn't allow customers to use their MySQL Database out of their services which means that if you want to run code using MySQL OVH database, you have to run the code into a OVH server.
This looks like a timeout error.
Please try taking the port number out of the quote marks. Hopefully this will fix the issue!
var connection = mysql.createConnection({
host : 'my_ip',
port : 3306,
user : 'my_user',
password : 'my_pass',
connectTimeout : 10000
});

connect nodeJS to MySQL when computer is offline

When the computer is offline, then my Electron app can't connect to the local MySQL.
When the computer connected to the internet, everything works fine.
Tested on Windows only.
The same happens in nodeJS (command prompt) and in Electron.
code:
s = {database: "test", user: "test", password: "test", host: "localhost"}
var mysql = require('./mysql');
var mysqlc = mysql.createConnection(settings);
mysqlc.connect(function(err) { console.log(err); });
The error code is:
{ [Error: getaddrinfo ENOENT localhost:3306]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'getaddrinfo',
hostname: 'localhost',
host: 'localhost',
port: 3306,
fatal: true }
Why ?
What can I do about it ?
Windows has an issue with resolving localhost to a physical IP address when it is not connected to a network. Apparently what happens is that on Windows is when you give the address localhost it passes it to a full DNS resolver which requires a connection to the internet to work properly.
Found a good answer about a possible why here: Windows 7: “localhost name resolution is handled within DNS itself”. Why?
Try using the IP address itself:
s = {database: "test", user: "test", password: "test", host: "127.0.0.1"}
var mysql = require('./mysql');
var mysqlc = mysql.createConnection(settings);
mysqlc.connect(function(err) { console.log(err); });

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