Connection refused when connecting with sequelize to aws rds - mysql

I have tried to connect to aws rds in every way possible and I'm getting a connection refused error when using sequelize so I wonder if there's a setting I need to set or include when making the connection. The code works on my local machine and if I push to heroku, however I need to use the aws instance for this project.
I used the sequelize CLI to generate an index.js file and the code for that is:
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
const config = require(path.join(__dirname,'../config/config.js'));
const db = {};
console.log('config: ',config);
if (config.env === 'production') {
console.log('i ran');
var sequelize = new Sequelize(config.use_env_variable);
} else {
console.log('else ran');
var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
dialect:config.db.dialect});
}
the error that is being thrown is:
as you can see the config variable is doing it's thing and I've made sure the env variables are set. I've triple checked to make sure the username and password were correct. I also tried using the end point that aws provides instead of localhost but that failed too. I've made sure to change the security settings on aws to allow any IP address to access it and I've successfully used mysql workbench to connect so there must be something else specific to sequelize or the code I'm using correct ?
UPDATE
my config.js file is as such:

In case anyone comes across this with connection issues to amazon...
Your host is set to "localhost", but this must be set to the host that you're connecting to (not from). In aws rds case this will look something like this:
some-rds-name-inserted-here.74389nctj.eu-west-2.rds.amazonaws.com

Check If your RDS network could limited to different zone.

Related

How to connect an Azure App services to Azure Database for MySQL Flexible Server

I am working on an Azure app services in conjunction with a flexible mysql database server. I have successfully deployed my website to NodeJS v18.LTS, but my server is Throwing: SequelizeHostNotFoundError: getaddrinfo ENOTFOUND mynameserver.mysql.database.azure.com in my app services log stream. In the following question I find a possible solution by adding the ip address of the connecting host to my database instance instead of a FQDN https://stackoverflow.com/questions/25521755/errorerror-getaddrinfo-enotfound-mysql.
However, this configuration is completely discouraged.
https://techcommunity.microsoft.com/t5/azure-database-for-postgresql/dns-configuration-patterns-for-azure-database-for-postgresql/ba-p/2560287
How can I correctly set up my Flexible Server for MySQL instance to work in my production App Services environment without violating this policy?
this is my connection instance configuration:
const sequelize = new Sequelize(
process.env.DATABASE,
process.env.USER,
process.env.MySQLPASSWORD,
{
host: process.env.HOST, // String conection xxxx.mysql.database.azure.com
dialect: process.env.dialect,
});
here I have an alternate approach of connecting to azure MySQL flexible server where I have used mysql2 npm package.
now here I am directly hard coding config data in the code, but you can easily read the application setting using same way you have used before just make sure that you first reading the respective setting for e.g.: username in a variable and then add that variable while configuring the connection to MySQL .
var username = process.env.USER
Here we use the create connection function to connect to the MySQL database and then use the query function to runa query .
The below is code form an express api:
app.use('/', (req,res)=>{
const mysql = require('mysql2');
const connection = mysql.createConnection({
host:'',
user:'',
database:'',
password:'',
port:'',
});
connection.query("CREATE TABLE TESTTABLE ( TEST int)",(err)=>{
console.log(err);
});
res.send("Hello World");
});
Here I have connected the database to MySQL Workbench where I created the table using the above code.
Here in the server I have disabled the ssl mandate

Cannot connect to MySQL on Google Cloud Run due to unknown socket error

I'm running a .NET 6 service on Cloud Run and trying to connect using the built in socket. My instance name is project-id:region:instance-name and the code for building my connection is as follows...
var builder = new MySqlConnectionStringBuilder
{
SslMode = IsCloud ? MySqlSslMode.None : MySqlSslMode.Required,
Server = IsCloud ? $"/cloudsql/{Endpoint}" : Endpoint,
UserID = User,
Password = Password,
Database = Schema,
Pooling = true
};
switch (IsCloud)
{
case true: builder.ConnectionProtocol = MySqlConnectionProtocol.UnixSocket; break;
case false: builder.Port = Convert.ToUInt32(Port); break;
}
_connectionString = builder.ConnectionString;
return _connectionString;
I also added the cloud SQL connection to the Cloud Run revision configuration. But every time I run it and the container has to hit the database, I get an unknown socket error and nothing to go on. My code is basically the same as the code recommended by Google, but I'm obviously doing something wrong setting up my socket because when I use a public IP, everything works without a problem.
Unfortunately, I have no idea what I could be doing wrong and would appreciate any advice. The IsCloud switch is there so the code can also be ran on localhost, which requires encryption and a port.

1and1 IONOS Database with Node.JS

I would like to connect my 1and1 mySQL Database on an Express App Node.JS with the mysql npm package but when I tried to connect the console send me back this:
Error: getaddrinfo ENOTFOUND <DB_HOST_NAME>
I looked on the 1and1 dashboard and it was the good logs, I tried to put 'tcp:\\' and ':3306' but no results, node can't find the database. Moreover the first website version was in PHP and the logs was working and still working with PHP so the logs are correct. I asked the 1and1 service but no answer.
I looked for survey on this error but anybody was able to find an answer.
Few snippets:
// Import Modules
const EXPRESS = require('express');
const CORS = require('cors');
const MYSQL = require('mysql');
// Import Database Configuration
const DB_CONFIG = require('./config/DbConfiguration');
// Create Express App Instance
const APP = EXPRESS()
APP.use(CORS())
// Server Port Const
const SERVER_PORT = 4000
// Create MySQL Connection Instance
const CONNECTION = MYSQL.createConnection(DB_CONFIG.dbConfiguration);
CONNECTION.connect(function(err) {
// Print Error
if(err) {console.error(err)};
});
DbConfiguration.js
require('dotenv/config')
module.exports = {
dbConfiguration: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
}
}
.ENV file
DB_HOST=xxxxx.1and1.fr
DB_USER=xxxxxxx
DB_PASSWORD=xxxxxx
DB_NAME=xxxxxxx
Have been looking at this problem myself recently too.
Due to the way in which 1&1 IONOS manage security to their mySQL servers, you're only able to connect to them using your account (workspace). Further information on this is accessible via their website. Unfortunately, you either need to deploy your code to your workspace or use an alternative local phpMyAdmin installation for development. Not ideal I know!
change your configuration of a connection string like if you are running on local machine try
`host:'localhost' or host:'127.0.0.1`
wrap it in string.

Google Cloud SQL connection in NodeJS app (Express) --> Error: connect ENOENT

I'm trying to deploy my NodeJS app on Google Cloud services and getting the following error hen I try to call a database query using Postman through localhost/8080, which is where my server is listening: connect ENOENT /cloudsql/<MY_CONNECTION_NAME>. Here's my database connection file, config.js:
const mysql = require('mysql');
const dotenv = require('dotenv');
dotenv.config();
const {
DB_PASS,
DB_USER,
DB_NAME,
} = process.env;
const config = {
user: DB_USER,
password: DB_PASS,
database: DB_NAME,
socketPath: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
};
const connection = mysql.createConnection(config);
// create connection
connection.connect((err) => {
if (err) throw err;
console.log(`Connected to database ${DB_NAME}`);
});
module.exports.connection = connection;
I know that Google recommends using a pool to connect, but I'm afraid that doing that will require me rewriting all my database queries, and I'm on a tight deadline.
I've been able to successfully shell into the database with MYSQL using the terminal.
Take a look at the Connecting to App Engine page. In particular, here are some things you should check if the socket isn't present:
For GAE Flex, make sure you have the following in your app.yaml:
beta_settings:
cloud_sql_instances: <INSTANCE_CONNECTION_NAME>
Ensure the SQL Admin API is enabled and make sure you have the correct IAM permissions (Cloud SQL Client1 or higher) on your service account (service-PROJECT_NUMBER#gae-api-prod.google.com.iam.gserviceaccount.com`). If between projects, make sure you have the
Make sure you are spelling <INSTANCE_CONNECTION_NAME> correctly. It should be in the format <PROJECT>:<REGION>:<INSTANCE> - you can copy it exactly from the "Instance Details" page for your instance.
Additionally, using a connection pool will have no effect on your queries. Using a pool only means that when you "open" a connection, it is actually reusing an existing connection, and when you "close" a connection it puts it back in the pool for your application to use elsewhere. The queries you perform using the pool should be exactly the same.
this is happen if you are using flex as env in app.yaml to run without errors remove the env:flex from your app.yaml,
your app.yaml need to look like this
app.yaml
and you will successfully connected to the cloud sql without causing errors
console log

Using node.js, express, and MySQL

I am currently in the process of creating an app and I am using node.js, express, and MySQL. I feel that I am in a bit of a rut in connecting my server to my database. Here's how I see things so far
server.js
var express = require('express');
var app = express();
//These are just my static finals so this can be ignored for now
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public/views'));
app.use(express.static(__dirname + '/public/controlers'));
app.listen(3000);
console.log("Listening at 3000")
Here I am setting up a local server on my computer that is listening on port 3000. What I am hoping to do here eventually is handle post requests. My goal is to ensure that post requests are inserted into the database. So I realize that I need to do two things create a database and a schema for my database. For now I just want to create a simple table with an ID and first_name columns. I am using this driver https://github.com/felixge/node-mysql/.
So my next step is creating a connection
dbconnection.js
var mysql = require('mysql')
var app = require('../server.js');
var connection = mysql.createConnection({
host : 'localhost',
port : 3000,
user : 'username',
password : 'password',
database : 'liveDatabase'
});
connection.connect()
//queries and error handling go here
connection.end()
This is where I lose touch with my program.
Here's what I don't understand:
I don't get how a connection is being created to my localhost:3000. I see the key values for host and post being assigned to localhost and 3000 and I am requiring my server in the db.js file (var app = require('../server.js');. Is this all that is needed to create a connection? How does it find localhost:3000? I am guessing this is all happening under the hood, but I feel a little stuck here.
I have confusion about the database as well. Where should my database be created and live in order for .createConnection to be able to find it. Do I create a separate .sql file then just create my database and table(s) there and require(/database/path) in my dbconnection.js?
Any help is appreciated. Thanks!
This is just a start, but this seems to be the crux of the misunderstanding:
Your server.js is creating a web server that listens on port 3000. Your dbconnection.js is trying to connect on that server on port 3000. That's not right. You want it to connect to your MySQL server instead.
It seems more logical for server.js to require dbconnection.js rather than the other way around. Then server.js can send/receive info with the database via the required module.