NodeJS: Connect my Google App Engine app to my Cloud SQL databases - mysql

Help needed. On my local Mac, I use the following code to connect my app to my MySQL database, and it works perfectly on my local computer.
const pool = mysql.createConnection({ //
password: 'x142a0ejeqee', // my password
user: 'myusername',
database: 'mydatabase_name',
host: 'localhost',
port: '3306'
});
Now, I want to deploy my app to the World. I uploaded all of the files in App Engine (Google Cloud), but I cannot connect my app to my databases (MySQL on Cloud SQL). Help. Anyone has a code that helps?

The steps to connect locally to a MySQL database and the steps to connect remotely to a Cloud SQL instance differ slightly in nature.
I would recommend following this Connecting to Cloud SQL from App Engine guide for the specific implementation walk through.

Related

listen EADDRINUSE: address already in use :::3306 when attempting connection with MySQL server via Node.JS app

I am experiencing problems with MySQL connection since making a few changes, and exhausted all suggestions, found here and the net as well as official troubleshooting docs, I come here in the hope of help.
The problem.
When trying to connect to MySQL DB via Node.JS (VSC)
Error: listen EADDRINUSE: address already in use :::3306
Node works on all Ports as requested, apart from any port that MySQL uses. Also MySQL connection fails if either the port number is edited or a new instance created.
A little history of the problem:
Worked perfectly with Node.JS app + MySQL DB Workbench 8.0 (MWB). Could connect and webpage populated with data from DB with no issues until i hooked it up to AWS Elastic Beanstalk, which I have since delete but problem persists even though I'm back at the beginning.
3306 is the port MySQL listens on. Your NodeJS app should not try to also listen on that port.
You should be specifying port 3306 here:
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "root12345",
});
Not here:
app.listen(PORT, () => {
console.log(`listening to port: ${PORT}`);
});
Your app should probably be listing on port 80 or 443 or whatever is appropriate for whatever your NodeJS app is trying to do. It may not need to listen on any port at all.
Also, you are using local MySQL here, you aren't making a connection to RDS at all, you are making a connection to the MySQL software running on the same server as the NodeJS app.

Error: connect ECONNREFUSED 127.0.0.1:3306 on MySQL, Heroku, and Node.js

I am testing simple code with mysql and node.js but it is saying:
Error: connect ECONNREFUSED 127.0.0.1:3306
This is my code:
require('dotenv').config();
const mysql = require('mysql2/promise');
module.exports = mysql.createConnection({
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
});
I just started doing some code of mysql connections. Can anyone fix this?
The way heroku works, your web dyno (virtual machine) cannot have a MySQL instance built into it. That is, you won't be able to connect to MySQL on localhost when you use a web dyno.
Heroku offer their own postgreSQL service you can use (you'll have to change your web app code from MySQL to postgreSQL, of course). Or there are a few different third-party MySQL addon offerings you can install in your Heroku app. https://elements.heroku.com/addons/
Each of these offerings will require you to connect to it using an environment variable you get from your web dyno. So if you do something like this, not debugged, you'll be able to run both locally and on your dyno.
const dburl = process.env.MYSQL_ADDON
|| 'mysql://' + process.env.DB_USER+ ':' + process.env.DB_PASS
+ '#localhost/' + process.env.DB_NAME
const conn = mysql.createConnection(dburl);
The add-on you use has documentation explaining the name and details of their environment variable.
The available third party add-ons have free tiers allowing you to handle a very small (5MiB) database. For databases bigger than that they charge.

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.

nodeJS app cannot connect to google MySQL data instance

I hosted a nodeJS application in google console under a paid account. When i tried to connect my nodeJS app to MySQL db in localhost server it is working but once i configured it to work in google cloud console it says can't connect to database. I successfully created a google SQL instance and sure about user name and password as i can connect to database via cloud console.
i referred to many tutorials in the internet and couldn't get a way....
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1234',
database: 'test'
});
con.connect(function(error){
if(error){
console.log('error');
}
else{
console.log('connected');
}
});
Since this question is tagged with google-app-engine, I assume it is the product you are using to deploy your application. In this case:
App Engine uses a Unix socket to connect to a Cloud SQL instance, because of that you need to pass the instance's connection name, like in the example below:
var con = mysql.createConnection({
host: "localhost",
socketPath: "/cloudsql/<PROJECT_ID>:<REGION>:<SQL_INSTANCE_NAME>",
user: "root",
password: "secret"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
You can actually test that by running the cloud_sql_proxy locally and connecting through the unix socket. The Using Cloud SQL for MySQL tutorial explains how to do that.
EDIT:
In case you are using App Engine Flex, it is also important to set the correct beta_settings on your app.yaml like in the example below:
beta_settings:
# The connection name of your instance, available by using
# 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
# the Instance details page in the Google Cloud Platform Console.
cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME
Your local machine sql server can't be connected to the google console as it is not
exposed to the internet.You should host your mssql db in some platform and then you
can connect with that in google console

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.