Using firebase functions emulator with cloud SQL proxy - google-cloud-functions

I deployed a firebase https function that calls a GCP MySQL instance and returns some data. How can I run this function in the firebase function emulator?
I loaded the function into the emulator along with a simple helloWorld https function. That returns nicely with the local URL (http://localhost:5001/INSTANCE/us-central1/helloWorld). The locally run SQL https function, however, returns
{"errno":-4058,"code":"ENOENT","syscall":"connect","address":"/cloudsql/(deleted)","fatal":true}
... where I've deleted the instance connection name.
Next, I run gcloud_sql_proxy on my PC, and it claims to be listening on 127.0.0.1:3306. The https function continues to return the exact same error above, so I'm not sure if the emulated function is going through my gcloud_sql_proxy or still trying to connect to the cloud SQL directly - I am guessing the latter.
Should I also re-instrument the function itself to connect using host+port (127.0.0.1, 3306) versus the unix socketpath method that it's currently using? I was hoping the emulator would automagically do that so I can avoid going down that rabbit hole, as the cloud documentation only provided the socketpath method. But I do plan to try that next.
Thank you.

Based on the error message, I think the emulator is looking for a Unix socket and it sounds like you're starting the Cloud SQL Proxy with a TCP socket.
Try using a Unix socket whose path matches your function. Something like:
cloud_sql_proxy -dir /cloudsql
This will create a Unix socket for all SQL instances in your active gcloud project.

Related

PHP MyAdmin Azure App Services - (HY000/2002): An attempt was made to access a socket in a way forbidden by its access permissions

When attempting to connect to the Azure App Services (ASP.NET) MySQL database (Navigating to the Manage link) the Page info returned from PHPMyAdmin prevents me from moving forward. I cannot seem to find the location of the log for the app within Kudu and navigating to the MYSQLCONNSTR_*.ini file did not provide me with the information that I needed.
I've attempted to use the connection strings for the database for both the scm and the web app but neither work. I have also attempted to use the public-facing IP within the Kudu environment variable without any luck.
I cannot seem to find the information I need to connect to my instance of PHPMyAdmin, and am now quite lost.
The solution to my problem was here: https://stackoverflow.com/a/57545678/4212591
The free tier of the Azure Web Service does not allow for the always-on feature. Thus my inability to connect to the service.
I’m unsure on the exact location/path you are looking for that file, the connection string is stored at D:\home\data\mysql\MYSQLCONNSTR_localdb.txt.
If you wish to customize the database, username and password, after you have created a new database, add new username or update password, simply modify:
D:\home\data\mysql\MYSQLCONNSTR_localdb.ini , remove D:\home\data\mysql\MYSQLCONNSTR_localdb.txt and restart the WebApps.
Furthermore, the connection string flows to your application as an env variable MYSQLCONNSTR_localdb. The env is added to the main site process. For Kudu site, one may read the same info from D:\home\data\mysql\MYSQLCONNSTR_localdb.txt file. Beware that we are not using the default MySql port (3306). In fact, the port number may vary for each application life cycle depending on its availability at startup time. The port info is also available as an env variable WEBSITE_MYSQL_PORT to your site.
Ensure that MySQL process is running. Check in your web app application settings if there is a connection string. PHPmyadmin uses MYSQLCONNSTR_ to connect to the MySQL server. If you have a connection string in application setting change the connection string type to Custom , so you can still have the information if needed or delete it. This will force PHPmyadmin to access MYSQLCONNSTR_localdb and connect to the MySQL in-app server.
As a side note, on Azure WebApps Sandbox - Connection attempts to local addresses (e.g. localhost, 127.0.0.1) and the machine's own IP will fail, except if another process in the same sandbox has created a listening socket on the destination port.
To rule out, application code or the client library leaking TCP socket handles or burst load of requests opening too many TCP socket connections at once, scale-up Azure App Service Plan and see if that makes any difference.

How to connect MySQLdb code to google cloud SQL?

I have a flask server which I want to deploy on Google Cloud Platform. The code uses MySQLdb library to connect with local MySQL instance in the following manner:
#app.route('/show_table', methods=['POST'])
def login():
db = MySQLdb.connect("localhost", "root", "", "db_name")
cursor = db.cursor()
query = "select * from table_name;"
cursor.execute(query)
res = cursor.fetchall()
return res, 200
But instead of local MySQL instance, I want to connect this code to Cloud SQL so that it reads data from the cloud. What changes should I make to this code? I have currently created a project in Google Cloud Platform and a Cloud SQL instance inside this project. I have also created the required tables inside this instance by following
this tutorial.
You shouldn't have to change your code too much, it just depends on how you're going to connect to the database. The Google documentation has step by step information on how to connect to Cloud SQL from an external application.
Since you're not using Java or GO, there's two options:
Use the Cloud SQL proxy
Whitelist the public IP address of your server on the Cloud SQL instance page
All the steps are in the documentation, but it basically says that if you use the proxy, you'll need to enable the Cloud SQL Admin API, install the proxy client on your local machine and authenticate it. There's a few authentication options but the recommended way is creating a credentials file from a service account using the console and passing the file as a parameter when you first start the proxy. Once you've got the proxy running the documentation has examples on how to connect using either TCP or UNIX sockets. With TCP you'll be using the proxy as localhost so you won't have to change your code. Using UNIX sockets you'll use the instance connection name which you'll find in your instance details on the GCP console. MySQLdb supports both.
With the second option you need to allow access to your Cloud SQL instance from a specific IP address range. Go to the connections tab in your Cloud SQL instance details page and add the IP address (using CIDR notation) you want to use to connect to your database. Once it's whitelisted then you can use the Public IP of your Cloud SQL instance, which you'll find in instance details, in place of localhost to connect to your database.

Connect to Google Cloud MySQL instance from local running NodeJS / TypeORM

I am trying to connect to local GCloud MySQL instance from my local machine. I managed to connect to the instance from Workbench. In this answer it says to add a socketPath in extra when creating the config file. But since I'm trying to run this on local machine I tried changing that "/cloud/" path in many ways but didn't succeed.
Can someone please explain how I can connect to GCloud MySQL instance from a local NodeJS project that use TypeORM.
As per the official documentation we can establish a connection to Cloud SQL from an application running outside of Google Cloud Platform in different ways. Now, I went through the documentation and I found that you can use a Proxy if is for local test environment. I successfully reproduced this scenario and I did all the steps that the documentation said. According to my tests, there is no need to change the socket path.
Please let me know how it goes.
If you are running an application locally and want to connect to a Cloud SQL instance, I would recommend the Cloud SQL Proxy. It creates local entrypoint (Unix socket or TCP port, depending on what you tell it) that will authenticate and proxy your connections to your instance.

Google App script and local database connectivity

I have mysql server installed on my machine. I would like to connect to the same local mysql instance using Google app script's jdbc service. I am getting connection failed error. So, I would like to confirm:
1. is it possible to connect to mysql instance running on private machine(laptop) using jdbc service.
2. If not is it always mandatory to connect to public IP. What is the alternative to connect to mysql instance running on private machine.
This definitely works, but you need to permit Google's machines to connect to your machine. That means opening a hole in your firewall, possibly setting up port forwarding on your router, etc.
There is an alternative to poking a hole in your security arrangement, it is called Secure Data Connector, and it lets your machine initiate the connection to Google - https://developers.google.com/secure-data-connector/
Unfortunately, I don't believe that SDC supports JDBC yet (I believe it only supports HTTP based communication).
(P.S. One very simple work around is to mirror your local database to some kind of cloud service, and then permit access to that replica from Google).

Error connecting using JDBC Mysql

I'm not able to establish a connection to my MySQL server.
This is the connection string:
jdbc:mysql://<my_IP_address>:1005/rs_pm
This is the line of code:
var conn = Jdbc.getConnection(connString, mysql_user, mysql_pass);
I've been able to write a native Java program using JDBC and it connects just fine using the same connection string/username/password. (Yes, I know the port is non-standard but that's how I have it configured and it works with the native Java app).
The weird thing is, I'm looking at my router logs and when I try to connect with a native Java app I can see the traffic coming in. But when I run the script in G Apps I don't see any packets being received by Google IP addresses.
Is it just me? Is there a problem somewhere?
Thanks in advance.
Update So it appears that if I use the standard mysql port (3306), the connection works fine. It seems as though something in Google Apps won't try an outbound connection on the port I had specified (1005), or any other ports I tried. Perhaps the jdbc connector they use only supports port 3306? I will file a bug since this behavior isn't documented anywhere.
JDBC connection to a database server only supported to ports 1025 and higher.