Google App Engine PHP PDO connection fails 'MySQL server has gone away' - google-cloud-sql

So I've setup two different GAE app projects, in the primary app i've created two Cloud SQL instances. I've granted access to my other app on both SQL instances.
When I attempt a simple PDO connection from the app which does not contain the cloud sql instances (live on GAE) I receive this error: SQLSTATE[HY000] [2006] MySQL server has gone away
here is the PHP connection line as per the GAE docs:
$db = new pdo('mysql:unix_socket=/cloudsql/<app-id>:<sql-instance-name>;dbname=<db-name>', 'root', '');
Connecting via IP from my local GAE SDK instance works as expected.
Any suggestions would be great!
So far I've tried:
increasing the max_allowed_packet size 32000
increasing the long_query_time to 60
increasing the wait_timeout to 60

The google app engine documentation states:
"Google Cloud Platform project called <your-project-id> is connecting to a Cloud SQL instance named <your-instance-name>."
You're probably getting an error because for project id you are using the project Id for your appengine application, not your Cloud SQL project ID.
Try:
$db = new pdo('mysql:unix_socket=/cloudsql/<sql-project-id>:<sql-instance-name>;dbname=<db-name>', 'root', '');

In the Cloud Console's Instance Details, under Properties toward the bottom of the screen, there's a field called Instance connection name. So what worked for me was:
$db = new pdo('mysql:unix_socket=/cloudsql/<instance-connection-name>;dbname=<db-name>', 'root', '');

This happened to me today, the problem for me was the app-id, make sure it includes the whole Google Apps domain. e.g. if your app is testapp, your instance is instancetest and your domain is domain.com the complete statement is:
$db = new pdo('mysql:unix_socket=/cloudsql/domain.com:testapp:instancetest;dbname=testdb', 'root', '');
Hope it helps.

Related

Connecting Postgres instance to AppEngine - Google Cloud, with SpringBoot

I've nearly got a SpringBoot app running on Google Cloud services that is connected to a Postgres instance.
I've ran through the steps on their guide, located here and have gotten to the point where I need to set my variables up for the app to find the database instance:
The problem encountered is two fold:
I don't know where and how to set these
My server logs report this error:
meaning that the Spring application is trying to find the database like it would on my local.
I set the following values in the app.yaml (assuming this is where they should be?)
runtime: java11
instance_class: F1
env_variables:
SQL_USER: quickstart-user
SQL_PASSWORD: <password>
SQL_DATABASE: quickstart_db
INSTANCE_CONNECTION_NAME: quickstart-instance
So, my question(s) are:
Is this the correct place to set them?
If not, do I need a appengine-web.xml file instead (And does anyone have an example of what this looks like, I can't find one)
How do I stop the app from looking for the local database?
Thanks

Azure database works on localhost, but not when used with azure service app

So I've been trying to publish my first project to azure. I've got everything set-up, a service app and a sql database.
My initial page loads properly(It's the standard view for a .net core web application).
The first thing I need to do is register a new user. Whenever I try through my azure app (myapp.azurewebsites.net) it fails and the logs says it's db related.
However I try the same thing by running the application on my machine in production environment, again connected to the azure sql server and everything works perfectly. I can register users, I can create posts, I can edit them. The allow access to azure services option is turned on. This error is from the eventlogs. I have not included the stacktrace.
Category: Microsoft.EntityFrameworkCore.Query EventId: 10100 RequestId: 800001be-0000-ba00-b63f-
84710c7967bb RequestPath: /Identity/Account/Register SpanId: |1e5a93ae-43f424904f38ea9f. TraceId:
1e5a93ae-43f424904f38ea9f ParentId: ActionId: c3430236-e61c-4785-a3c3-4f60ba115b6e ActionName:
/Account/Register An exception occurred while iterating over the results of a query for context type
'MyApp.Data.ApplicationDbContext'. Microsoft.Data.SqlClient.SqlException (0x80131904): Server
name cannot be determined. It must appear as the first segment of the server's dns name
(servername.database.windows.net). Some libraries do not send the server name, in which case the
server name must be included as part of the user name (username#servername). In addition, if both
formats are used, the server names must match.
Those are the different ways I tried to add the connection string to the appsettings.json file. (Server name, catalog, user and password have been replaced, they are written correctly in the appsettings file)
Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user#server;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=#server.database.windows.net;Password=password;
Alright so after a day and a half, I finally managed to fix it. The solution is rather simple and it is most likely my newbie mistake, that caused so much trouble.
I was following a tutorial for setting up the application and database connection after that. In the tutorial, the connection string that was being used, was the default one, found in the "myApp -> Configuration -> Connection strings", the format was:
Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=#server.database.windows.net;Password=password;
This one was working in the guide, but not for me. So what I did, was go to my "sqldb -> connection strings" and copied the one provided there. I then went back to the app configuration and added it as a new configuration string using SqlServer as the Type.
This string was in the format:
Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
After that, the app started working properly.

How do i deploy a Perfect (swift) backend code + PostgreSQL to Google App Engine

i'm pretty new to web development and much more in Google Cloud, sorry for anything.
Basically, i'm doing the backend part of an app in Swift (using Perfect), and it's running smoothly and okay in my local computer, i'm using a local Postgre database (using PostgreORM in my application).
But, when i deploy that to the Google Cloud, it does not recognize the database (i've created a identical poster database in the computer engine AND a Cloud SQL (Postgre service of Google Cloud with the same names and credentials), but again, when the app is on the cloud, it does not recognize the database, what i'm missing? How should i do it? Install other docker image with Postgre?
Here's my DBConnector code:
import PostgresStORM
func setupDBCredentials() -> PostgresConnector.Type{
let connection = PostgresConnector.self
connection.host = "localhost" // or the connection name of the Google Cloud, it doesn't work as well
connection.username = "postgres"
connection.password = "nearby"
connection.database = "nearby"
connection.port = 5432
return connection
}
Basically, how do i make my google app engine code connect to any database at all?
Also, if it helps, i'm using the Perfect Assistant to deploy my code to Google Cloud, using Docker.
Thanks already!
You'll likely need to do a few things to get connected, such as granting access to the Cloud SQL instance. Here is the link to the PHP docs that cover the broad steps that you'll want to follow and it shows a representation of the connection string that you'll also need.
I believe your connection string needs to look something like this
pgsql:dbname=DATABASE;host=/cloudsql/CONNECTION_NAME
Where CONNECTION_NAME is in the format of PROJECT_ID:CLOUD_SQL_REGION_ID:CLOUD_SQL_INSTANCE_ID

How to connect to Bluemix dashDB outside the Bluemix environment

I have several tables in dashDB and I want to access them from another server in order to filter out the data and insert it to a database on my own server.
In dashDB, there is a Service Credentials section and I clicked "Add Credentials" and it outputted a json file with service credentials info.
I tried to run a simple PHP to test the connection :
<?php
$servername = "dashdb-entry-....";
$username = "dash....";
$password = "....";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
and it fails. The error is as follow:
Connection failed: A connection attempt failed because the
connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond.
I checked service status of IBM and it seems that everything is running. Is it possible to access the dashDB outside the Blumix environment?
Yes it is possible to connect to dashDB from outside Bluemix. Here you can find some samples on how to connect to dashDB using several programming languages. Please take a look at the PHP Sample: One first difference I can see is that you are using mysqli while you should use db2_connect as in the sample.
You should be able to connect to the dashdb on bluemix using PHP. I also found the following link that explains this in detail.
http://php.net/manual/en/function.db2-connect.php
You can also use ODBC or JDBC method with proper drivers installed on the client and providing the connection credentials found under the
connect --> connection information
from the left hand side after you log in.
Murali

Accessing Google Cloud SQL instance from Google Compute Engine?

After spending a few hours, this is the only real documentation I can find for accessing Cloud SQL from outside of GAE: https://developers.google.com/cloud-sql/docs/external
The problem is, this is for a Java application (via JDBC).
I need to access my Cloud SQL DB from within a PHP, Dart, or NodeJS application. I thought by giving my GCE instance rights to connect to Cloud SQL, this would be easy. But no arrangement of socket strings (using mysql drivers) seems to be effective.
For argument's sake, let's say I'm trying to connect with a PHP app. My mysql connection array looks like this:
(
'driver' => 'mysql',
'unix_socket' => '/cloudsql/project-id:instance-id',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
This is as close as I got, but I'll get a generic "Can't connect to local MySQL server through socket" error.
While this is an older question, I just thought I should share what I've found in regards to this.
First off, you were attempting to connect to a MySQL Server on your GCE instance, not your remote CloudSQL instance.
To begin
Go into your dashboard and request an IP for your CloudSQL Instance.
Go to CloudSQL Access Control and add your GCE IP address.
Connect to CloudSQL from GCE via mysql-client and add a new (non-root) user
Use the CloudSQL IP and the new non-root user to access CloudSQL from GCE PHP files.
Hope this helps.
The Cloud SQL team are working on improving the connectivity from Compute Engine. If you send this question to google-cloud-sql-discuss#googlegroups.com, they will be able to follow up.
You could connect indirectly I.E. Create a Java-based App Engine App that provides an interface to the database for you, and consume that interface from your PHP app?
For example: Java App Engine App has a 'getEmployees' method call that calls a Select query on the DB and then formats and returns the results as a JSON file. Your PHP app would then call this method and consume the JSON...