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?
Related
I was deploying my Django Project on AWS (this is my first time doing this) after a lot of first time learning i got stuck at an error :
django.db.utils.OperationalError: (2005, "Unknown MySQL server host '****.*****.us-east-2.rds.amazonaws.com' (0)")
When i tried migrating my Django Project.I tried multiple solutions but none worked. I tried command mysql -u username -p password -h ****.********.us-east-2.rds.amazonaws.com but this also returned and error.
ERROR 2005 (HY000): Unknown MySQL server host '*****.*********.us-east-2.rds.amazonaws.com' (2)
UPDATE 2: I Tried using command on AWS Documentation mysql -h mysql–instance1.123456789012.us-east-1.rds.amazonaws.com -P 3306 -u mymasteruser -p which have -h before using -u and -p and an input for Port this changed the error which seems like now the connection is not not able to reach the server which seems a reasonable and a better error than before.
ERROR 2003 (HY000): Can't connect to MySQL server on '******.******l2la.us-east-2.rds.amazonaws.com' (10060)
I read in question this about this,they are talking about binding the instance to an IP.How can i do that if that could be the problem?
UPDATE1:
Screenshots:
FIRST RDS INFORMATION
EC2 instance Information
SGs information
If you require any log or information please feel free to ask in the comments.
Thanks
The security group of Database should hold a Value in Inbound Rules :
This Rule should allow connection from your EC2 Instance.Either set the IP Address or set the SG of EC2 Instance.
You can use the Private IP of your instance rather than the Public IP also.
(Not sure about Security Concerns or Advantages.)
Update After a Year: Well you should use private IP and not the same security
group.DB should only be accessible to the EC2 and not to public.
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.
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
I am trying to use R-studio and R script to connect to mysql database which is on an Ubuntu system. The error is:
Error in .local(drv, ...) : Failed to connect to database: Error: Can't connect to MySQL server on 'x.x.x.x' (0)
I tried using telnet to connect, but I get an error saying:
Could not open connection to the host, on port 3306: Connect failed
I tried using putty, and I am able to connect to the ubuntu system with proper credentials. But I am unable to connect thru R. Windows firewall has been disabled. Password and IP address are fine. I tried using localhost but no help.
Any suggestions would be greatly appreciated.
I figured out. there were some internal issues, otherwise, everything as fine. the port was open and the password was right..
I have created an aws account and need to create a connection from RStudio to the aws database in order to write data and read in subsets of the data.
I tried the example here https://shiny.rstudio.com/articles/pool-dplyr.html and it works fine. However, when trying to connect to my own instance I receive the following error:
Error in .local(drv, ...) :
Failed to connect to database: Error: Access denied for user 'Administrator'#'cpc75655-alde5-2-0-cust376.6-2.cable.virginm.net' (using password: YES) .
I have created and assigned my instance to a Security group with Administrator access. What should I try to fix this?
I am working on Ubuntu 16.04.