Always show error when connecting aws mysql via node.js - mysql

I try to connect aws mysql via nodejs
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'xxxx.xxxc.ap-northeast-1.rds.amazonaws.com',
port : '3306',
user : 'dbusername',
password : 'passwords',
database : 'dbname'
})
connection.connect(function(err){
if(!err) {
console.log("connected ... ")
} else {
console.log(err)
}
});
When I run this code, and it will take some seconds, and then shows following error -
{ Error: connect ETIMEDOUT
at Connection._handleTimeoutError (/node_modules/mysql2/lib/connection.js:181:13)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true }
I googled a lot, and couldn't find a right answer, and I try to use node-mysql2, has the same issue. Any ideas?

Check it via connecting your mysql aws rds via any mysql client(mysql workbench, dbninja etc.), if you are able to connect it via any client then must be something wrong with the nodejs setup otherwise it will be issue with the RDS details.
Also check it if RDS has restricted access for particular VPN So your node server should be in same VPN to connect.

By default, network access to AWS RDS instances is turned off.
I highly recommend to:
Test whether your instance with Nodejs is able to connect to the
mysql instance. You can do by:
sudo apt-get install mysql-client (for Ubuntu)
Check security groups configuration of RDS instances. Follow AWS
tutorial.
Follow AWS RDS Security Best
Practices
and configure the security groups appropriately.

Check the following things:
1-RDS security group and make sure your IP address can access to your RDS,
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
2-Check your password

Related

connect to mysql database hosted on ec2 aws from localhost using nodejs

i'm trying to connect to mysql database hosted on ec2 instence on aws, but i'm getting the error:
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '15.188.51.165',
port: 3306,
fatal: true
my code :
const con = mysql.createConnection({
host: "********.com",
user: "root",
password: "******",
database : '***[![enter image description here][1]][1]',
port:'3306',
multipleStatements: true
});
To be able to connect to mysql server in an ec2 from outside you need to verify these points
0. Check if your mysql server is accessible from your ec2
Check if your ec2 OS firewall has connectivity from where you are accessing. Check if firewall allows connection via 3306.
For ubuntu you can refer this
Check if your security group allows connection from where you are accessing. Please open port 3306 from security group.
Verify if your mysql server is in a public or private subnet. If it is in a private subnet, your network configuration for sql client should be such that you are able to access it.
These rules apply for all/most OS. If these points align you should be able to access your server.

How do I establish MYSQL connection to AWS ec2 instance from local machine?

I'm finished with my project and I'm trying to deploy it to AWS. I have an ec2 instance as my webserver with the following configuration details:
NodeJS using port 5000
PM2 (keeping server alive at all times)
NGINX as web server reading from my build file
MySQL within ec2 instance as my database. (using port 3306)
My problem is I'm having trouble establishing a connection from my local machine to my AWS ec2 instance that has the MYSQL db inside of it. I opened up MYSQL workbench and I can connect to it just fine there but when I try and establish a connection string to the DB from node.js it gives me an error.
I was able to successfully connect to the DB within MYSQL workbench but how can I connect to it now from nodejs connection string?
What I already tried was the following:
1) In AWS security group opening up TCP Rule for all incoming traffic at port 5000
2) In AWS security group opening up MYSQL/Aurora Rule for all incoming traffic at port 3306
3) Granting all privileges on . to user and flushing and restarting mysql server.
Error it gives me in the console.
`{ Error: connect ECONNREFUSED 14.54.xxx.xx:3306
at Object._errnoException (util.js:1019:11)
at _exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1175:14)
--------------------
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '14.54.xxx.xxx',
port: 3306,
fatal: true }`
Here is my code trying to establish the connection:
```var mysql = require("mysql");
// Local Works just fine
// var connection = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "xxx",
// database: "devdb",
// charset: "utf8mb4"
// });
// Production Connection to AWS MYSQL instance (stuck on)
var connection = mysql.createConnection({
host: "14.54.xxx.xxx",
port: "3306",
user: "jordan",
password: "xxx",
database: "productiondb"
charset: "utf8mb4"
});
// Test the db connection
connection.connect(function(err) {
if (err) {
console.log(err);
} else {
console.log("Connected!");
}
});
module.exports = connection;
```
I expect to be able to successfully connect to the db instance from my NodeJS
Make sure again, I think your security groups have something wrong, maybe your server listening internally so It's happening. Go your EC2 security group and select Inbound and add rules as type=mysql, proto=tcp, port range=3306, source=0.0.0.0/0,::/0 (allow all)
There are a couple of reasons due to which this might be happening -
Configure MySQL database
#start MySQL server sudo service mysqld start
#run configuration sudo mysql_secure_installation
In the prompt, follow the following steps:
Enter current password for the root account: press Enter key
Set root password? Y
New password: yourpassword
Re-enter new password: yourpassword
Remove anonymous users? Y
Disallow root login remotely? n
Remove test database and access to it? Y
Reload privilege tables now? Y
If you are using RDS then you will have to provide NAT access to the VPC which holds your database. For more info please refer here
Actually I think I just figured it out.
So the default mysql configuration file has the ip binded to 127.0.0.1. However, I changed the binding to my ec2 public ip and also changed the default "mysql" to "jordan" and I saved the configuration file, restarted the mysql service and it now works.
Thank you for the suggestions I'll write this down in my documentation to check for in the future.

Why is my Node.js code working when xampp is running mysql, but not when mysql notifier has mysql server running?

I am new to using node.js and mysql, I have a simple script that uses node to log me into a mysql server.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : ''
});
connection.connect(function(err)
{
if (!err)
console.log('Connected');
else
console.log('Error connection');
console.log(err);
});
When my xampp mysql server is running it returns connected, however when xampp is turned off and the mysql server notifier is running it returns this error.
"code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
sqlState: '08004',
fatal: true }"
I used the installer for windows found on the mysql site. I am running windows 10, and it is installed directly into the C drive of my computer.
can you try by executing following command:
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
I didn't know what was MySQL Notifier so I searched on Google. According to the documentation I found here (www.mysql.com), MySQL Notifier only notifies for changes on your system and connection to your MySQL Server.
It means that this utility tool is not a MySQL server, you can't use it like it was. You need a running MySQL server (launched by xamp).
There are many ways to run a MySQL server without xamp:
Like you did: with the standalone MySQL Community Server on your Windows: https://dev.mysql.com/downloads/mysql/
With Docker, if you're familiar with: https://hub.docker.com/_/mysql/
Anyway, your server version mismatches with your client version, which is here your mysql Node.js library. The two versions work with each other but the authentication method seems to have changed.
Which version of it do you use? Did you try to update it? npm update mysql
Best solution maybe here: There is a closed issue on this subject in the mysqljs GitHub repository where Andrey Sidorov advices to
allow to use insecure auth: https://github.com/mysqljs/mysql#connection-options insecureAuth option.
Source: https://github.com/mysqljs/mysql/issues/1574
Option description:
insecureAuth: Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
https://github.com/mysqljs/mysql#connection-options
You can try to set this option to true:
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
insecureAuth: true
});
Just a little thing, "xamp" stands for "LinuX Apache MySQL PHP" (so you're using "wamp" -> "Windows Apache MySQL PHP").
If you are running MySQL 8.0 then you are probably facing the same issue (and will be able to solve it with the same solution) described here.

ECONNREFUSED Node.js and MySQL

I am attempting to have MySQL and Node.js on the same EC2 instance. When trying to hit the IP of the sample I am using, I receive the following error:
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14) ...
My connection code looks like the following:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : '127.0.0.1',
user : 'student',
password: 'default247',
database: 'student'
});
I think my issue is that the database is not linked to my instance of EC2 but I'm not entirely sure. I thought there would be a way to link my RDS to EC2 using the elastic IP, but it is not obvious to me how to do that. Does the above error seem to point to an issue with MySQL connecting to EC2 instance? Or do you think the issue could be with my connection code? Thanks!
EDIT:
I guess I am confused... To clarify, I ran npm install mysql --save on the EC2 instance. Separately, I created an RDS database instance as I thought I would need to associate a database to my EC2 instance and was not aware of a way to create a database instance within EC2. My understanding of how RDS and EC2 are related is flawed. I am trying to find the easiest way to access MySQL through my EC2 instance. What is the recommended way to do this?

How do I use node's mysql library over ssh (on Mac)? After googling, I'm still getting "channel 3: open failed: connect failed: Connection refused"

Starting from scratch, I googled how to connect to a mysql database over ssh using node.js and the mysql library, and I came across this:
Node.js connecting through ssh
So I started a "screen" session, connected with the ssh command, and created the connection in a node script. However, I was getting an error. A comment below the accepted answer had the same issue:
I'm using a mac terminal, I typed 'screen', entered in the information you provided with my domain and password,and succesfully connected into my server via ssh. However, when I run my server.js node file the problem still persists. I'm receiving: { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', fatal: true } Is there a step here that I missed? I'm able to query successfully with this code on servers that don't require ssh.
And the response led me somewhere but did not completely explain what I need to do:
After you connected via ssh you need to connect your node.ja app to localhost. Because with that ssh command in screen you make port 3306 from mysql server available on your local machine
How exactly does one "connect your node.js app to localhost"? I saw that on the remote server side, I was getting channel 3: open failed: connect failed: Connection refused. So some sort of request was getting successfully sent to my remote server. However, something was failing. Googling led me to this answer:
SSH -L connection successful, but localhost port forwarding not working "channel 3: open failed: connect failed: Connection refused"
The simplest explanation for the rejection is that, on server.com, there's nothing listening for connections on localhost port 8783. In other words, the server software that you were trying to tunnel to isn't running, or else it is running but it's not listening on that port.
So now I'm stuck. How does one cause a server to "listen" so that mysql can work over ssh?
Thanks!
FWIW tunneling mysql over ssh can be accomplished in-process with the mysql2 and ssh2 modules. For example:
var mysql = require('mysql2');
var Client = require('ssh2').Client;
var ssh = new Client();
ssh.on('ready', function() {
ssh.forwardOut(
// source address, this can usually be any valid address
'127.0.0.1',
// source port, this can be any valid port number
12345,
// destination address (localhost here refers to the SSH server)
'127.0.0.1',
// destination port
3306,
function (err, stream) {
if (err) throw err;
var sql = mysql.createConnection({
user: 'foo',
database: 'test',
stream: stream // <--- this is an important part
});
// use `sql` connection as usual
});
}).connect({
// ssh connection config ...
});
Also, since there is overhead with creating ssh connections, you might want to create an ssh connection pool for better reuse.