NodeJS AWS RDS Connection Database Not Found Error - mysql

I am brand new to MySQL and AWS in general so excuse me if this is a silly question but here goes:
I am trying to create a connection between my nodejs application and my MYSQL database hosted on AWS. I have this so far
var mysql = require('mysql');
var connection = mysql.createConnection({
host: "AWS RDS Endpoint",
user: "username",
password: "password",
database: "DB Instance Name on AWS Console"
});
connection.connect(function(err) {
if (err) {
console.error('Database connection failed: ' + err.stack);
return;
}
console.log('Connected to database.');
});
connection.end();
When I try running the code I get back "Unknown database 'DB Instance Name on AWS Console'" with the database name being, obviously the db instance name that appears on the AWS console. The password and username I know are correct but I'm not sure about the host or database parameters. Can anyone give me some insight into, if I am doing this correctly?

Last attribute database, should be DB name attribute from configuration tab, which is default database we choose when creating RDS
OR you can remove database attribute completely if you are using an admin credentials.
var connection = mysql.createConnection({
host: "my-mysql-instance.acdefghij.us-east-1.rds.amazonaws.com",
user: "username",
password: "password"
});

Related

MySQL connection node

I'm running my node app on a linux vps where I have installed apache2 and phpmyadmin. I have my mysql database on the server there which I can connect to user the mysql -uusername -ppassword command, but when running my node app with this code:
const mysql = require('mysql');
const connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '*******',
database : 'db_name',
});
connection.connect(function(e) {
if(e) {
console.log('Database didn\'t connect');
} else {
console.log('Database connected successfully');
}
});
It says "database didn't connect". the user, password and db fields are all correct for sure.
When console.log(e) I'm getting:
I'm getting:
error code: 'ER_NOT_SUPPORTED_AUTH_MODE'
errorno: 1251
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client'.
anybody knows why ?
You can change the connection to use the unix socket or check the mysqld is binding on 0.0.0.0 or on the ip of the ethernet card of the server it's running on.
According to the mysqld version you're using, the option can be "skip-networking" or "bind-address".

Connect to RDS MySQL programatically (NodeJs, Java) - ER_ACCESS_DENIED_ERROR: Access denied for user

I am trying to connect to my RDS (AWS MySQL database) using NodeJs mysql library.
const mysql = require('mysql');
var connection;
connection = mysql.createConnection({
host: 'xxx.us-east-1.rds.amazonaws.com',
port: 3306,
user: "username",
password: "MyPassword",
database: "DbName",
});
I was also trying mysql2 and sequelize. All of them got me the same result:
{
error: ER_ACCESS_DENIED_ERROR: Access denied for user 'username'#'myhost,
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000'
}
However, this remote database is set to public and I am able to connect to it with mysql command:
mysql -u username -pMyPassowrd -h xxx.us-east-1.rds.amazonaws.com
I am also able to connect to it with MySQL Workbench.
Also, the problem is not in NodeJs, because I am able to connect to my local database:
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'test',
});
In conclusion, my AWS database is publicly available and I can connect to it, just not using NodeJs for some reason.
I have not found a useful answer yet. Did anyone encounter this problem ?
UPDATE:
Trying to connect in Java with simple Connection returns the same result as well:
final Connection con = DriverManager.getConnection("jdbc:mysql:/xxx.us-east-1.rds.amazonaws.com:3306/DbName", "username", "MyPassword");
UPDATE 2:
I was missing one slash for Java code. I needed to use jdbc:mysql:// instead of jdbc:mysql:/. But NodeJs implementation is still one big mystery.
The answer was rather simple, just add an ssl option with true value
connection = mysql.createConnection({
host: 'xxx.us-east-1.rds.amazonaws.com',
port: 3306,
user: "username",
password: "MyPassword",
database: "DbName",
ssl: true //this does the trick
});

How to connect mysql database with nodejs from google cloud

I have a NodeJs application and MySQL database.
I have deployed nodejs application in google cloud and created MySQL database in google cloud and connected to nodejs application
i am able deploye the application successfully but application not able to connect to cloud mysql dayabase.
But when i am trying to connect cloud mysql from my local mysql workbench, it's successfully connecting to database. and i am able to connect cloud mysql database from local nodejs application
but i am not able to connect from deployed nodejs application to cloud mysql db
error Database is not connectedError: connect ETIMEDOUT
Db.js
var mysql = require('mysql');
var PropertiesReader = require('properties-reader');
var properties = PropertiesReader('./db.properties');
var connection = mysql.createConnection({
connectionLimit : properties.get('pool'),
host: properties.get('hostname'),
user: properties.get('username'),
password: properties.get('password'),
database: properties.get('dbname')
});
connection.connect(function (err) {
if (!err) {
console.log("Database is connected");
} else {
console.log("Database is not connected" + err);
}
}
);
module.exports = connection;
app.yaml
runtime: nodejs
env: flex
In your connection configuration for mysql,host does not work on App Engine. You have to use socketPath . socketPath is the path to a unix domain socket to connect to. Socket path must be something like this
var connection = mysql.createConnection({
socketPath : '/cloudsql/my-project-12345:us-central1:mydatabase',
user : 'username',
password : 'password',
database : 'db_name'
});
It's a similar process if you're using Postgres on GCloud which is answered here

mariadb for node.js ER_ACCESS_DENIED_ERROR Access denied for user

I am using the following code to establish connection to a MariaDB 10 server:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "local_ip",
user: "username",
password: "my_password",
port: 3307,
database: "name_of_db",
});
con.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
The SQL-Server is running on a machine within a local network.
The SQL-User is granted connections from all hosts (%).
I am able to establish a connection with a SQL GUI tool, with exact the same user.
Seems to me like a bug inside the mysql node package, anybody encountered such a problem?

Connect Express js project to external MySQL databse via SSH

I would like to know the best way to connect an Express.js project to an external MySql database using conf.ini ?
Should I use SSH ?
There is no need for ssh, mysql has it's own protocol to connect remote servers, you only need to use mysql module for nodejs, the following code is to ensure connection between hosts:
const mysql = require('mysql');
var connection = mysql.createConnection({
host : 'remote_ip',
user : 'mysql_username',
password : 'password',
database : 'database_name'
});
connection.connect( function (err) {
if (err) {
console.log('Cannot connect to mysql server', err);
} else {
console.log('Successfully connected');
}
connection.end();
});
One last thing, make sure to edit mysql config file /etc/mysql/my.cnf and set bind-address parameter to 0.0.0.0, and do not forget to restart mysql service: sudo service mysql restart