Unable to connect to the database with mySQL - 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';

Related

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

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.

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 Sequelize cannot connect to MySQL SSL

I'm trying to connect my node app to my remote MySQL server and I am getting the following error:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'remoteUser'#'mysqlIpAddress' (using password: YES)
However, I can connect to the mysql server just fine by using the following command.
mysql -u remoteUser -h 138.68.15.94
Here is my connection string
sequelize = new Sequelize(config.mysql.database, config.mysql.user, config.mysql.password, {
host: config.mysql.host,
dialect: 'mysql',
ssl: true,
dialectOptions: {
ssl: true
},
define: {
timestamps: false
},
pool: {
max: 5,
min: 0,
idle: 10000
},
})
Any help would be great! I've been fighting this the past two days.

Using local.js to store sails-mysql password

I'm trying to use mysql as my database, but I can't figure out how to get my config/adapters.js to use mysql information in config/local.js to connect. What's the correct way to store the connection information so that sails-mysql can connect?
config/local.js is merged on top of all other configuration. So you can just put your own adapters key in there:
{
adapters: {
default: 'myLocalAdapter',
myLocalAdapter: {
module: 'sails-mysql',
host: 'localhost',
user: 'root',
password: 'password',
database: 'database'
}
}
}
and it'll be picked up and used by Sails.
EDIT In sails v0.10.x, use the key connections instead of adapters as the two are now differentiated and connections now hold authentication info. The original answer was for an earlier version of sails.
As of latest version of sails (i.e 0.12), while writing this answer, there is no Adapters.js. The list of connections are specified in the config/connections.js and default connection to use for all models are specified under config/models.js. So, going by this change the way to override the database configuration for local is specifying as below in config.local.js.
module.exports = {
connections: {
localMongoServer: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'YOUR_DB_USER',
password: 'YOUR_DB_PASSWORD',
database: 'YOUR_DB_NAME'
}
},
models: {
connection: 'localMongoServer'
}
}
NOTE: As in accepted answer, just changing adapters to connections won't work, since there is no default attribute under connections.