ER_ACCESS_DENIED_ERROR when connecting to MySQL using process.env.* values - mysql

I'm facing the following issue with https://github.com/sidorares/node-mysql2 (also tried with other libs):
await mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
})
throws Access denied for user 'user'#'ec2-44-201-80-189.compute-1.amazonaws.com'
while
await mysql.createConnection({
host: 'xxx.xxx.xxx.xxx',
port: '3306',
user: 'user',
password: 'password',
database: 'database'
})
works fine.
Please note that 'xxx.xxx.xxx.xxx' is the IP of the remote MySQL server, but in the first case it looks like the library is instead trying to connect to the NodeJS server (Heroku. But same is happening when I run the app locally, the IP doesn't match what I supplied). Just before the call, I console.log the process.env.* values read from env file and they are all correct.
Any clue on what I'm doing wrong?
thank you.

Related

Database query doesn't return anything

I'm trying to connect my flutter app to the database using the mysql1 dart package. Everything seems right, I get no errors on the connection but I can't query my database. I'm not sure if my query reaches the database and is ignored or maybe I'm not reading the results right. I did as much debugging as my skills allow for and I'm out of ideas.
MySqlConnection.connect(ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'root',
password: 'Haffenstadt!',
db: 'dbco'))
.then((conn) {
conn.query('SELECT * FROM User').then((results) {
print(results.isEmpty);
print(List.from(results));
});
conn.close();
});
My console:
flutter: true
flutter: []

Unable to connect to the database with mySQL

i have created database file
export default {
host: process.env.DB_HOST,
type: 'mysql',
port: process.env.DB_PORT || 3306,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: ['src/**/*.entity{.ts,.js}'],
synchronize: process.env.DB_SYNCRONIZE === 'true',
logging: process.env.DB_LOGGING === 'true',
};
I am getting error :Client does not support authentication protocol requested by server; consider upgrading MySQL client
For MySQL8, Default authentication mechanism has been changed to caching_sha2_password. If your drivers or client doesn't support then Create a new user with mysql_native_password mechanism and use that user in code. Please follow below command to create new user with native password.:
CREATE USER 'nativeuser'#'localhost' IDENTIFIED WITH
mysql_native_password BY 'password';

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
});

0 how to fix Database connection Error, In description is the the code and the error

I am using mysql and node.js. I am trying get a simple connection, but getting this weird error. the code i am using is this
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'userlogin'
});
connection.connect();
and here is the error i am getting:
here is some details about the server:
Have you tried executing this in the workbench worked for me
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

node.js-mysql connection failed on windows7

I am just getting started with nodejs.
My question is that i get an Error:
connect ECONNREFUSED
npm install mysql;
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
port: '3306'
});
connection.connect(function(err) {
if(err) {
console.log(err);
}
});
connection.end();
Finally
1.I don't know the "user" and "password";
2."In mysql.conf, comment skip-networking.", I can't find mysql.conf;
3.I have tried this and it's not working, my platform is windows 7.
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});
It's clear you have not install mysql server in your windows yet. So you should go to http://www.mysql.com and download the server, install and set up, you can set your username and password and grant privilege to it, so you will know the user and password, and mysql.conf will be inside your installation. And if you are in windows, generally you would not use a UNIX socket to build the connection. So "_socket: '/var/run/mysqld/mysqld.sock'" should not be here.