How to connect MySQL database in nodeJS file.

0

Hi,
I have an AWS Lightsail account designed to service a MySQL database given HTTP "QueryData" parameters. I have it mostly working but I am having difficulty connecting my MySQL database via my node JavaScript code. In the AWS documentation I find all kinds of info on how to connect my MySQL to a MySQL client [e.g. MySQL Workplace] but nothing on how to not use a client and make the connection in my JavaScript code.

Presently I am doing the following :
// Connect to MySQL AWS Lishtsail 'MySQL' database...
const vpsDB = mysql.createConnection(
{ host : 'ls ...us-east-1.rds.amazonaws.com', [i.e. my MySQL Endpoint]
port : '3306',
user : 'dbmasteruser',
password : 'My custom password',
database : 'jFormTK-MySQL'
} );

vpsDB.connect(function(err) {
if(!err) {
console.log("Database is connected ...");
} else {
console.log("Error connecting database ..."lib.nl
err);
}
});

The above yields the following 'err' :
Error connecting database ...
Error: ER_BAD_DB_ERROR: Unknown database 'jFormTK-MySQL'

Can you please point me to where this is documented OR suggest where I have gone wrong.

Using Google I found minimal hits that did not help.
That effort made me think my problem was the 'Host' ordered pair where some shown code used an IP address. I tried using MySQL static IP address but got an error from the 'node' command.

Another suggestion made me think my Security Setting was the culprit but I think LightSail manages that setting for me and have no idea how to control that???

Sorry for the long query but I'm stumped as to what to try next.

Regards,
Jim...

已提問 4 年前檢視次數 894 次
2 個答案
0

Hey Jim,

I'm using the npm mysql-node (https://github.com/mysqljs/mysql) to connect on RDS instance, but I'm not informing my database, because they are separated in Schemas.

// My MySQL Config Object
{
host: '<URL to my RDS instance without protocol http or https>',
port: port,
user: user,
password: 'passowrd'
}

Another thing is to try to confirm the database name, the lower and upper cases maybe.

已回答 4 年前
0

I am now using LightSail and am successfully using MySQL in my VPS instance.

Regards,
Jim Crowell

已回答 4 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南