I am getting error SequelizeConnectionError: connect ETIMEDOUT when trying to connect to a remote mysql db with sequelize.
Connection can be established successfully when I try to connect to my local mysql db.
I'm using sequelize's default db connection code new Sequelize(...) contained within models/index.js, with the following config (filled up with the correct values):
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
I tried connecting to the remote db with a simple php script and it worked (so we can rule out issues on the remote db server side)
Any ideas?
For me, I have to:
1) Define exact information of my database at the local server (Xamp/ Mamp). It means I must have the existing database to connect, user name and password is a privileged account in your database.
2) Xamp/ Mamp must be online (of course). Default ports will be taken by this local server, so try a different port for mysql 8889 instead of 3306.
And this is what I tried:
sequelize = new Sequelize('wohui', 'root', 'root', {
dialect: 'mysql',
host: 'localhost',
port: 8889
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch((err) => {
console.log('Unable to connect to the database:', err);
});
I had the same problem, in my case it happened because I forgot to open the connection to mysql port 3306 in the inbound rules at my cloud provider
This error usually appears when the connection to the sql server is not established. Some things to take care of are :
Ensure mysql server is running in the host you are trying to connect to.
Ensure the host ip is correct.
Ensure that the port entered is correct.
Ensure that the firewall rules are defined correctly.
There could be couple of reasons for this, Listing out a few I have faced,
Remote root access not granted by the mysql server.
The configs are filtered by the environment, Which ideally is done by using the NODE_ENV variable, can you try running your server locally with prod config. In my case, I would do something like NODE_ENV=production node server.js. Assuming server.js is the start file. You can try logging the value's before new Sequelize(...), that might give a better idea as to what's going in.
I was facing the same issue. While in my case I gave a wrong port number (I didn't update my port number for the production database)
I had the same issue and my problem was in the ports. MySql had ports 3306 and in the config, i wrote port 3000 :D
Thanks for helped
Related
I am experiencing problems with MySQL connection since making a few changes, and exhausted all suggestions, found here and the net as well as official troubleshooting docs, I come here in the hope of help.
The problem.
When trying to connect to MySQL DB via Node.JS (VSC)
Error: listen EADDRINUSE: address already in use :::3306
Node works on all Ports as requested, apart from any port that MySQL uses. Also MySQL connection fails if either the port number is edited or a new instance created.
A little history of the problem:
Worked perfectly with Node.JS app + MySQL DB Workbench 8.0 (MWB). Could connect and webpage populated with data from DB with no issues until i hooked it up to AWS Elastic Beanstalk, which I have since delete but problem persists even though I'm back at the beginning.
3306 is the port MySQL listens on. Your NodeJS app should not try to also listen on that port.
You should be specifying port 3306 here:
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "root12345",
});
Not here:
app.listen(PORT, () => {
console.log(`listening to port: ${PORT}`);
});
Your app should probably be listing on port 80 or 443 or whatever is appropriate for whatever your NodeJS app is trying to do. It may not need to listen on any port at all.
Also, you are using local MySQL here, you aren't making a connection to RDS at all, you are making a connection to the MySQL software running on the same server as the NodeJS app.
I have issue when i try to connect to the database on remote server.
My code:
const mysql = require('mysql');
const database = mysql.createPool({
host: 'localhost',
user: 'user',
password: 'pass',
database: 'db'
});
database.getConnection(function (err, connection) {
if (!err) {
console.log('Database is connected ...');
} else {
console.log('Error connecting database ...');
}
});
The credentials for connection in code is faked. With the right credentials I have, I login successfully on phpMyAdmin on remote server, on datebase that I want to connect. Credentials is good.
When I run script, return this error:
view error
Also, when I input credentials for connection with my local database, everything work perfect.
As pointed out by Luuk, you need to replace the localhost with the actual IP address of the remote database server and the port on which the database server is running.
For example -
const database = mysql.createPool({
host: '123.234.121.234',
port : '3306',
user: 'user',
password: 'pass',
database: 'db'
});
Also, make sure the port is whitelisted and can be accessed over the network. Heres a tiny little diagram for explanation.
phpmyadmin runs on the same machine as your MySQL server, so it can connect to the server using the generic host name localhost.
I guess, from your question, that your nodejs program runs on some other machine (your personal machine, maybe?). That takes some special-purpose setup to do.
You must use the server's actual hostname in your host: property, not localhost.
MySQL login credentials aren't just username/password. They are host/username/password. You may need to create a new set of credentials for remote access so your nodejs program can get in. Read this: https://webmasters.stackexchange.com/questions/2242/how-to-create-separate-users-in-phpmyadmin-each-one-cant-see-others-databases
If your MySQL server runs on a rented server at some cloud or hosting service, you may need to open up a firewall to allow your machine to connect. If you're on a hosting service, ask their customer support krewe about that. On a cloud service, you want to open port 3306. Look up how to do that in their documentation. (It may be a gnarly configuration task).
Your easiest way of troubleshooting this is to use some MyQSL client program (like MySQL Workbench or HeidiSQL) on your own machine. when you get that to connect, you can use the same credentials in your createPool() call.
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've got two docker services, one running a simple node server and the other a mysql (mariadb actually) database server.
All instances of a socket file mentioned anywhere in /etc/mysql/ say
/var/run/mysqld/mysqld.sock
This will be important soon.
My node server is running some Sequelize code that is trying to connect to the MySQL server.
Whenever I try and connect via Sequelize, I get:
{"statusCode":500,"error":"Internal Server Error","message":"connect ENOENT /var/run/mysqld/mysqld.sock"}
However, if I log into the Node docker container I can successfully connect to MySQL on the other docker container using the mysql CLI client.
I think I understand that the mysql client is using a tcp connection, while Sequelize is using a socket connection. But, when Sequelize is throwing that error, it is showing the correct socket path, as far as I know. Here is my Sequelize config:
const options = {
host: "mysql",
dialect: "mysql",
dialectOptions: {
socketPath: "/var/run/mysqld/mysqld.sock"
}
};
let sequelize = new Sequelize("ibbr_dev", "devuser", "password", options);
The MySQL socket file is not available in your Node container, it is only available in the MySQL container as it is a file. Rather than setting up unix socket based connection, you should use a TCP connection (skipping the dialectOptions).
I am using the tunnel-ssh module to establish connection to the remote mysql database using node.js. The documentation is poor and I am not able to establish a connection. Here is the code snippet:
var tunnel = require('tunnel-ssh')
var server = tunnel.tunnel(config.sshData, function(err, result) {console.log('connected'});
Here is my sshData object.
config.sshData = {host : 'serverxyz.web-hosting.com', username : 'xyz', password : 'xyz',
srcPort: 3307, dstPort : 21098}
The dstPort is 21098 as suggested by the namecheap documentation.
However I am getting timeout error and whenever I add this snippet:
server.on('error', function(err) {});
I get the error server.on is not a function. The remote connection is working fine on putty and SQLyog. Any procedure on how to establish successful connection would be of great help. Thanks!
Update
Got the database working by using the correct ports specified and by directly using ssh2 module with the code example given here
There is a misunderstanding with the namecheap documentation. 21098 is the ssh port, not the port the database is listening on. In order to use a non-standard ssh port, you will need to explicitly specify the port value like:
config.sshData = {
host: 'serverxyz.web-hosting.com',
port: 21098,
username: 'xyz',
password: 'xyz',
dstPort: 3306
};
Then you should be able to connect to localhost:3306 to access your remote database.