NodeJS + MySql hosting - mysql

I want to host my application based on node.js and MySql database. I try heroku hosting. I've create an application and add ClearDB add-on with Punch DB plan. The main restriction of this add-on that I cannot create any user-defined functions and events in this plan. It is sopported for expensive database plans.
Then I host my db on Google Cloud SQL. It allows to create user-defined functions and events. However I cannot access to the database from my heroku app because I don't know how to authorize external heroku network for my google cloud database. How can I do this?
Or may be there are any other hostings for NodeJS + MySql application where there are no the restrictions mentioned above?

You should be able to:
a) Use Cloud SQL by authorizing the external IP of your Heroku dyno via a proxy service like QuotaGuard Static or Proximo;
b) Do the same using Amazon RDS.
Additional details that may help:
1. SO question related to QuotaGuard
To clarify: you have to use a third-party add-on like this because Heroku won't guarantee a single IP or even a range of IPs for your dyno.

I add QuotaGuard Static add-on and it holds two ip adresses. Than I add this addresses to the Authorized Networks of my Google Cloud Sql. After then I try to connect to tha database using a guide. Here is my code:
exports.getConnection = () ->
dbConnParams =
host: 'google cloud sql ip'
port: 3306
proxy = url.parse process.env.QUOTAGUARDSTATIC_URL
auth = proxy.auth;
username = auth.split(":")[0]
pass = auth.split(":")[1]
sock_options =
host: proxy.hostname
port: proxy.port
user: username
pass: pass
sockConn = new SocksConnection dbConnParams, sock_options
dbConnection = mysql.createConnection({
user: 'root',
password: 'pwd',
stream: sockConn
});
return dbConnection
However I obtain an error: "connect ECONNREFUSED". Whats wrong with this approach?

Related

How to connect an Azure App services to Azure Database for MySQL Flexible Server

I am working on an Azure app services in conjunction with a flexible mysql database server. I have successfully deployed my website to NodeJS v18.LTS, but my server is Throwing: SequelizeHostNotFoundError: getaddrinfo ENOTFOUND mynameserver.mysql.database.azure.com in my app services log stream. In the following question I find a possible solution by adding the ip address of the connecting host to my database instance instead of a FQDN https://stackoverflow.com/questions/25521755/errorerror-getaddrinfo-enotfound-mysql.
However, this configuration is completely discouraged.
https://techcommunity.microsoft.com/t5/azure-database-for-postgresql/dns-configuration-patterns-for-azure-database-for-postgresql/ba-p/2560287
How can I correctly set up my Flexible Server for MySQL instance to work in my production App Services environment without violating this policy?
this is my connection instance configuration:
const sequelize = new Sequelize(
process.env.DATABASE,
process.env.USER,
process.env.MySQLPASSWORD,
{
host: process.env.HOST, // String conection xxxx.mysql.database.azure.com
dialect: process.env.dialect,
});
here I have an alternate approach of connecting to azure MySQL flexible server where I have used mysql2 npm package.
now here I am directly hard coding config data in the code, but you can easily read the application setting using same way you have used before just make sure that you first reading the respective setting for e.g.: username in a variable and then add that variable while configuring the connection to MySQL .
var username = process.env.USER
Here we use the create connection function to connect to the MySQL database and then use the query function to runa query .
The below is code form an express api:
app.use('/', (req,res)=>{
const mysql = require('mysql2');
const connection = mysql.createConnection({
host:'',
user:'',
database:'',
password:'',
port:'',
});
connection.query("CREATE TABLE TESTTABLE ( TEST int)",(err)=>{
console.log(err);
});
res.send("Hello World");
});
Here I have connected the database to MySQL Workbench where I created the table using the above code.
Here in the server I have disabled the ssl mandate

Cloud Run:<Cloud SQL instance IP adress> :3306: connect: connection timed out

I want to connect Cloud SQL in Cloud Run Application. I used golang. this is the code around sql connect setting.
func getEnv(key, def string) string {
v := os.Getenv(key)
if v == "" {
return def
}
return v
}
DB: DB{
User: getEnv("DB_USER", "<user name>"),
Pass: getEnv("DB_PASS", "<password>"),
Host: getEnv("DB_HOST", "0.0.0.0"),
Port: getEnv("DB_PORT", "3306"),
Database: getEnv("DB_DATABASE", "<database name>"),
},
dsn := fmt.Sprintf("%s:%s#tcp(%s:%s)/%s?charset=utf8&parseTime=true",
config.DB.User, config.DB.Pass, config.DB.Host, config.DB.Port, config.DB.Database)
db, err := gorm.Open("mysql", dsn)
I set environment variable at Cloud Run setting console. After delpoy Application, Cloud Run console display Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information. and dial tcp <Cloud SQL Private IP> :3306: connect: connection timed out I wonder SQL connection is wrong...
You have not mentioned the word "VPC" in your question so I'm assuming you don't use it.
Cloud Run cannot directly connect to a private IP of a Cloud SQL instance. You need to configure a Serverless VPC Access Connector and specify it while deploying your Cloud Run app.
Cloud Run containers are not part of a VPC by default, so unless you do this, they will not have access to the private networks.
There is several way to connect your Cloud SQL database to Cloud Run. If it's MySQL, the easiest way is to follow the official documentation
If you want to use the IP with TCP connection, firstly, you can't use 0.0.0.0 as IP.
Use the Cloud SQL public IP (for this you have to authorize 0.0.0.0/0 network range on your Cloud SQL instance, and it's absolutely not recommended)
Plug your Cloud SQL to your VPC. And, as described by Ahmet, use serverless VPC connector to link Cloud Run with your VPC. Then add the private IP of your Cloud SQL in your code.

What is the connection string for Google Cloud mySQL, in node.js?

The scenario is as follows:
I am writing an node.js app locally and want it (the mySQL modul, rather) to connect to a mySQL db running on Google Cloud Platform.
I can not get the connection string right and keep getting errors. GCP holds two names for the database - one i picked, one generated, more complex. Problably I should supply one of these into the connection string as "database", the host is likely the IP address with port (or not(?)), I am not sure what the "user" should be, or if it is necessary. Not enough info on the GCP help pages or elsewhere.
tl;dr: I need the following to connect a node.js app with a mySQL module remotely to a GCP database:
var con = mysql.createConnection({
host:"11.222.333.444:3306",
user: "me"
password:"passwo3d",
database:"projectname-1528892969949:europe-west5:dbname"
});
Thank you!
The raw connection string should look something like: mysql://user:password#localhost:port/db
So I think your code should look something like:
var con = mysql.createConnection({
host:"11.222.333.444", <-- remove the port from your string
user: "me"
password:"passwo3d",
database:"dbname" <-- you have it as the instance name .. its just suppose to be the DB name
});
You can also try connecting to the instance using the cloud proxy setup. You host would just be localhost.
Finally you can always go the GCP console -> go to your instance and open up a shell. I think they print out the gcloud cmd in there and you can copy values from the console as well.
Depending on how you are pushing your code, you may have to change a few connections strings in your manifests as well. If you can share those I can help further.
For that to happen, you have to first enable remote access for your mysql on your GCP. A step by step guide is over here, you can leave the first few steps for installing mysql.

How to sync ShinyApps with MySQL From Localhost to Public

I am currently developing an app using R Shiny and finishing my ShinyApps, now I am trying to deploy the apps to Shinyapps.io so multiple user can reach and use it, but I have an issue for the deployment.
my Apps is about a Pharmacy management, it controls a CRUD operation, so clearly it is binded with db Connection using these options configuration (running this in locally)
options(mysql = list(
"host" = "127.0.0.1",
"port" = 3306,
"user" = "root",
"password" = ""
))
one more thing, to connect to database, I usually started my XAMPP apps and switched on mysql admin so my apps can connect the database locally. it worked flawlessly and clear before deploy
but it crashed instantly when I try to run it in the shiny.io after delpoy (what I mean is disconnected automatically). so I did try to change the host ip to publically like this , (I am trying to get ip address on user local machine)
configA <- system("ipconfig", intern=TRUE)
configB <- configA[grep("IPv4", configA)]
configC <- gsub(".*? ([[:digit:]])", "\1", configB)
options(mysql = list(
"host" = configC,
"port" = 3306,
"user" = "root",
"password" = ""
))
the ConfigC variable stores IPv4 address to get the public IP on local machine, but still these doesn't work, I attached a log in below link
how can I connect and sync my apps with MySQL in Shinyapps.io ? I use DBI and RMySQL package..do I need to host MySQL first so i can sync my apps? can anyone brief me with step by step explanation how to? thankyou in advance
here is my error log from shinyapps.io
http://textuploader.com/dulzh
For people who have same problem & didn't know how, i'll share what have work for me:
1) I Recommend Host your MySQL Database to AWS (Amazon Web Service), it is free and great performance to sync any Web Service update online especially shinyapps.io, with creating an Account first
2) Validate your AWS Account with full information including credit card, so you can access its Services
3) Click Service > Database > RDS
4) Then you will be redirected to AWS RDS Dasboard, here you can manage Instances of your MySQL Database, to create one new Instance, click Launch DB Instance
5) Here is my Instance settings:
Engine Options: MySQL
Use Case: Production- MySQL
DB Instance Class: db.r4.large 2vCPU, 15.25 GiB RAM (i believe this setting is subjectively based on our CPU performance)
Multi AZ Deployment: No
Storage Type: Provisioned IOPS
Allocated Storage: 100 GiB
Provisioned IOPS: Depends on your Allocated Storage (I use 4000)
6) Then in Settings tab, fill your db instance identifier, master username & password, after that when you click Next, there is advanced configuration, fill again db name and then you will want to check all Log Reports in hope an easier maintenance later, after finished > Launch DB Instance
7) Wait until your instances status become Available (keep refreshing to know)
8) After the Instance become Available, check the Instances and scroll down until you found Connect section, remember and save the Endpoint, Security Group Rules, master username & password Instances from Detail section
9) In your server.R, edit your MySQL connection options, from localhost to AWS RDS..
options(mysql = list(
"host" = "your Endpoint",
"port" = 3306,
"user" = "your master username of db instance",
"password" = "your master password of db instance"
))
10) Before deploying your MySQL database from localhost to AWS RDS, firstly Go to your AWS > Services > VPC > Security Group > (Click one of Group Name that is actively used by your Instances)> Inbound Rules
11) In Inbound Rules you must whitelist all External IP that you or other PC access to your shinyapps http://whatsmyip.org, and whitelist all shinyapps IP address based on this http://docs.rstudio.com/shinyapps.io/applications.html#accessing-databases-with-odbc in section 3.8.4
12) And now lastly, to deploy your MySQL Database from localhost to AWS RDS cannot be done directly, I Recommend installing MySQL Workbench to do it, after done installing, launch MySQL WorkBench
13) Create new MySQL Connection adn then fill the connection form:
Connection Name: (anything you like)
Connection method: TCP/IP
Hostname : (paste your Endpoint)
Port 3306
Username: (your master username of db instance)
Password: (your master password of db instance)
14) After a successful Connection to AWS RDS, open your connection, and then MySQL WorkBench UI will open, Import your .bak files (MySQL database) from Navigator > Management > Data Import > Select Import from Self Contained Files > browse your file> and then start Import
15) You have successfully deployed your database to AWS RDS! you can use query in WorkBench to see all your table/database information
16) RunApp your ShinyApps and test it, and DONE!
(if you EVER found message can't connect to your AWS RDS host, probably that your External IP is changed to new one, and to solve it you need to whitelist again your IP to AWS VPC in step 10)
I hope these are helpful for you!

Sails on Google App Engine with Cloud SQL

I have followed this guide https://cloud.google.com/community/tutorials/run-sailsjs-on-google-app-engine
on how to deployed Sails to Google App Engine.
However, my app used MySQL as the database and I have set it up like this in "connection.js".
mysql: {
adapter: 'sails-mysql',
host: '104.199.196.99', // Internal IP Address of the Cloud SQL Instance
user: 'root',
password: 'xxxxxx',
database: 'xxxxxxxx'
}
The error I got when trying to connect with Cloud SQL (when I make a requrest to log-in which involved connection to database) was:
Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
Could not connect to MySQL:
Error: connect ETIMEDOUT
Did I set it up correctly?
For those who seek the solution for connecting to Google Cloud SQL using Google App Engine with Sails.js, I have figured it out.
Connecting to the Google Cloud SQL using host address doest not work here, because the Firewall Rules and the Access Control of the Cloud SQL will prevent this kind of connection to the whitelisted addresses. Unfortunately, Google App Engine could not be assigned with Static IP Address. Therefore, the App Engine could not be whitelisted by the Cloud SQL or you would need to whitelist 0.0.0.0/0 which is everything and that is not secure.
The solution is to use this configuration in your connection.js.
googleCloudSQL: {
adapter: 'sails-mysql',
socketPath: '/cloudsql/' + process.env.INSTANCE_CONNECTION_NAME,
user: 'XXXXX',
password: 'XXXXXXX',
database: 'XXXXXXXX'
}
By connecting to the Cloud SQL via socket path using the Instance Connection Name of the Cloud SQL, this is the native way of doing it. If your App Engine is already in the same project with your Cloud SQL then it will already be authorized to connect. However, if they are in different projects, you would need to set up service account and IAM.