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.
Related
I am trying to connect to a mysql database over a remote connection with Node using the packages 'ssh2' and 'mysql2'. My code for connecting looks like below:
ssh.on('ready', () => {
var db_connection = mysql.createConnection({
host: '127.0.0.1',
port: 3306,
user: 'myDBuser',
password: 'myDBusersPassword'
});
db_connection.connect();
// The SSH connection is not the issue, so assume correct configuration and working connection
}).connect(sshConfig);
When I use the exact same credentials in MySql workbench, I am able to connect to the remote sql server just fine. However, when running this code, I am met with the following error:
Error: connect ECONNREFUSED 127.0.0.1:3306
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
Does anyone know why Node would have issues with connecting to the remote SQL server? I assume that it is not even passing my credentials to the server and is outright rejecting the connection from the start.
I have a Bastion host set up to be able to SSH into my RDS instance. I'm using Navicat 12 to make the connection to the database, and I'm reaching the SSH server on both Navicat as well as PuTTY, but I'm not able to actually reach the RDS instance and I'm getting the following error:
2013 - Lost connection to MySQL server at 'reading initial
communication packet', system error: 0 "Internal error/check (Not
system error)"
Here are the settings I'm using to connect:
SSH settings:
Host: my bastion public DNS
Port: 22
Username: ec2-user
Authentication Method: public key
Private key: my key path
Passphrase: none
Connection Settings:
Endpoint: my rds endpoint
Port: 3306
Username: my rds username
Password: my rds password
(I've also tried root with no password on this to test)
Security Group Info:
Bastion-
SSH/TCP/22/my local IP
RDS-
MYSQL/Aurora / TCP/3306/anywhere
If anyone can offer any sort of guidance or troubleshooting, I would super appreciate it!
AWS do not allow direct SSH access onto their RDS instances, I couldn't find where they state it in there documentation but there is this Forum where they say:
https://forums.aws.amazon.com/thread.jspa?threadID=93277
However - If you need to run scripts directly against the RDS you can install psql (or equivalent) on the bastion and connect to it through that.
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'm running a server on c9.io using Node.js and trying to connect to Mysql
I guess I get this error:
Error: getaddrinfo ENOTFOUND
because the connection to the db is wrong.
I'm using this:
var connection = mysql.createConnection({
host: "REMOTE_ADDR",
user: "MYUSERNAME", // this is replaced by my username
database: "c9",
port: 3306
});
Any idea what's wrong?
Thanks!
After one hour, and I don't know why, I found the issue.
In my case I replaced
host: 'localhost'
by
host: '127.0.0.1'
I know it's been a while since this was asked but I spent the better part of today trying to fix this. What to check for:
Within your nodejs source:
Try it without the 'port' option unless on your db VM, MySQL is
listening on a port besides 3306. I had this option specified but had
connection problems until I removed it. And 3306 is already the
default value for the option anyway.
Try it with the actual host IP in the host 'option'. I was using an actual domain name and, more often than not, node-mysql would throw
the same exact "errorError: getaddrinfo ENOTFOUND" When I used the IP
address, no more errors. Also, I did not get this error while on
Ubuntu instances in AWS. As soon as I switched over to Ubuntu VMs in
Azure, I got the problem.
Set the "debug" connection option to true which will give you a verbose trace of what's happening during the connection
On your db VM/Box/Instance:
Ensure that MySQL is listening on the right port
If you're seeing the error when trying to make concurrent connections using a pool, check that your max_connections and max_user_connections haven't been changed from the default settings in my.conf
Monitor "SHOW PROCESSLIST;" in MySQL to see if you see any issues there
This code works for me.
var mysql = require('mysql');
var con = mysql.createConnection({
host: '127.0.0.1',
port: '3306',
user: 'yourUsername',
password: '**********'
});
con.connect(function(err){
if(err) throw err;
console.log('connected!');
});
the host and port name find them in your mysql server.
in-place of '127.0.0.1' you can use 'localhost'
I was facing this issue. I did fix it simply you have to change your configuration of a connection string like if you are running on local machine try
host:'localhost' or host:'127.0.0.1'
...and set your user name and if you want this to publish your code on server then give the host according to server if in docker container give it name host:'db'.
I almost spend two hours to understand what is the problem with the mysql database. I am using Windows cmd.
I tried multiple ways e.g
npm install mysql2
Changing localhost to 127.0.0.1
Refreshing node
so in order to resolve this issue
[Error: getaddrinfo ENOTFOUND 127.0.0.1:3306]
I open XAMPP and start the MySQL database then manually check the credentials and works fine for me.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'test',
password: 'test123',
});