NodeJS MySQL DB connection fail - mysql

thank you in advance for the help.
I am using nodejs v16.13.1 and mysql 5.7.37-cll-lve.
my nodejs app is currently on my local machine and my mysql db is on a remote server. I want to connect to the remote mysql but, I keep getting this error:
Access denied for user ''#'mylocalcomputerip' (using password: YES)
I am using shared hosting btw.
What is weird is that the user is not showing and I know it is correctly being used. as well the the connection trying to connect to my local ip despite me putting in my server ip.
I tried using one of the nameservers and then it gives a timeout.
this is how I am trying to connect to the remote db.
createConnection(){
const conn = mysql.createConnection({
host: process.env.DB_HOST,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT
})
conn.connect(err => {
if(err) throw err
console.log("Connected")
})
return conn
}
The host is the shared ip.
The username is the user I created to interact with the db.
The password is the password for the username.
The database is the database that I'm trying to connect to.
The port is the default port 3306 for mysql dbs.
Any kind of knowledge or insight to what I'm doing wrong or don't know about would be greatly appreciated

Found the issue!! The object that is passed to the function createConnection uses a "user" key and not "username".

Related

ER_ACCESS_DENIED_ERROR: When i try to connect to the database on remote server - Node.js

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.

How do I establish MYSQL connection to AWS ec2 instance from local machine?

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.

Cannot connect Azure Web App - NodeJS to Azure Mysql

I am using azure connection string (from Azure portal) for Node.js app, still cannot connect to Azure Database for Mysql server.
var conn = mysql.createConnection({
host: yourhost,
user: "username",
password: "password",
database: "database",
port: 3306,
ssl:{ca:fs.readFileSync(__dirname + '/baltimore.pem')}});
I get following error:
"Error: MySQL server is requesting the old and insecure pre-4.1 auth mechanism.Upgrade the user password or use the {insecureAuth: true} option."
But I can connect using Mysql Benchmark same credentials.
What I am doing wrong ?
Azure Database for MySQL is incompatible with the mysqljs connector library. See issue 1729 for details and pull 1730 for a workaround.

errorError: getaddrinfo ENOTFOUND - mysql

I'm running a server on c9.io using Node.js and trying to connect to Mysql
I guess I get this error:
Error: getaddrinfo ENOTFOUND
because the connection to the db is wrong.
I'm using this:
var connection = mysql.createConnection({
host: "REMOTE_ADDR",
user: "MYUSERNAME", // this is replaced by my username
database: "c9",
port: 3306
});
Any idea what's wrong?
Thanks!
After one hour, and I don't know why, I found the issue.
In my case I replaced
host: 'localhost'
by
host: '127.0.0.1'
I know it's been a while since this was asked but I spent the better part of today trying to fix this. What to check for:
Within your nodejs source:
Try it without the 'port' option unless on your db VM, MySQL is
listening on a port besides 3306. I had this option specified but had
connection problems until I removed it. And 3306 is already the
default value for the option anyway.
Try it with the actual host IP in the host 'option'. I was using an actual domain name and, more often than not, node-mysql would throw
the same exact "errorError: getaddrinfo ENOTFOUND" When I used the IP
address, no more errors. Also, I did not get this error while on
Ubuntu instances in AWS. As soon as I switched over to Ubuntu VMs in
Azure, I got the problem.
Set the "debug" connection option to true which will give you a verbose trace of what's happening during the connection
On your db VM/Box/Instance:
Ensure that MySQL is listening on the right port
If you're seeing the error when trying to make concurrent connections using a pool, check that your max_connections and max_user_connections haven't been changed from the default settings in my.conf
Monitor "SHOW PROCESSLIST;" in MySQL to see if you see any issues there
This code works for me.
var mysql = require('mysql');
var con = mysql.createConnection({
host: '127.0.0.1',
port: '3306',
user: 'yourUsername',
password: '**********'
});
con.connect(function(err){
if(err) throw err;
console.log('connected!');
});
the host and port name find them in your mysql server.
in-place of '127.0.0.1' you can use 'localhost'
I was facing this issue. I did fix it simply you have to change your configuration of a connection string like if you are running on local machine try
host:'localhost' or host:'127.0.0.1'
...and set your user name and if you want this to publish your code on server then give the host according to server if in docker container give it name host:'db'.
I almost spend two hours to understand what is the problem with the mysql database. I am using Windows cmd.
I tried multiple ways e.g
npm install mysql2
Changing localhost to 127.0.0.1
Refreshing node
so in order to resolve this issue
[Error: getaddrinfo ENOTFOUND 127.0.0.1:3306]
I open XAMPP and start the MySQL database then manually check the credentials and works fine for me.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'test',
password: 'test123',
});

How to tell node.js that mysql is not running on default port?

I'm having a problem similar to this person's: connect ECONNREFUSED - node js , sql
I'm trying to connect app.js to an existing database running as part of MAMP. MAMP runs mysql on port 8889; how do I tell node.js which port to find mysql on?
When you are initializing the mysql client, along with username and password, you can pass a different value for port.
const client = mysql.createClient({
user: '??',
password: '??',
port: 8889
});
For those initializing the mysql connection object using mysql.createPool({...}), it's the same solution as the accepted answer. For Reference
const connection = mysql.createPool({
host: ...,
port: 8889,
user: ...,
password: ...,
});