Accessing Google Cloud SQL instance from Google Compute Engine? - google-cloud-sql

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...

Related

Newbie help - how to connect to AWS Redshift cluster (currently using Aginity)

(I'm afraid I'm probably about to reveal myself as completely unfit for the task at hand!)
I'm trying to setup a Redshift cluster and database to help manage data for a class/group project.
I have a dc2.large cluster running with either default options, or what looked like the most generic in the couple of place I was forced to make entries.
I have downloaded Aginity (Win64) as it is described as being specialized for Redshift. That said, I can't find any instructions for connecting using it. The connection dialog requests the follwoing:
Server: using the endpoint for my cluster (less :57xx at the end).
UserID: the Master username for the database defined for the cluster.
Password: to match the UserID
SSL Mode (Disable, Allow, Prefer, Require): trying various options
Database: as named in cluster setup
Port: as defined in cluster setup
I can't get it to connect ("failed to establish connection") and don't know if I'm entering something wrong in Aginity or if I haven't set up my cluster properly.
Message: Failed to establish a connection to 'abc1234-smtm.crone7m2jcwv.us-east-1.redshift.amazonaws.com'.
Type : Npgsql.NpgsqlException
Source : Npgsql
Trace : at Npgsql.NpgsqlClosedState.Open(NpgsqlConnector context, Int32 timeout)
at Npgsql.NpgsqlConnector.Open()
at Npgsql.NpgsqlConnection.Open()
at Aginity.MPP.Common.BaseDataProvider.get_Connection()
at Aginity.MPP.Common.BaseDataProvider.CreateCommand(String commandText, CommandType commandType, IDataParameter[] commandParams)
at Aginity.MPP.Common.BaseDataProvider.ExecuteReader(String commandText, CommandType commandType, IDataParameter[] commandParams)
--- Inner Exception: ---
......
It seems there is not enough information going into Aginity to authorize connection to my cluster - no account credential are supplied. For UserID, am I meant to enter the ID of a valid user? Can I use the root account? What would the ID look like? I have setup a User with FullAccess to S3 and Redshift, then entered the UserID in this format
arn:aws:iam::600123456789:user/john
along with the matching password, but that hasn't worked either.
The only training/tutorial I have been able to find/do on this is the Intro AWS direct you to, at https://qwiklabs.com/focuses/2366, which uses a web-based client that I can't find outside of the tutorial (pgweb).
Any advice what I am doing wrong, and how to do it right?
Well, I think I got it working - I haven't had a chance to see if I can actually create table yet, but it seems to be connected. I had to allow inbound traffic from outside the VPC, as per the above snapshot.
I'm guessing there's a better way than opening it up to all IP addresses, but I don't know the users' (fellow team members) IPs, and aren't they all subject to change depending on the device they're using to connect?
How does one go about getting inside the VPC to connect that way, presumably more securely?

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

Hosting the database separately for Meteor apps

It seems to be a common and safer practice to host the database separately from Meteor apps. That is to say, have an EC2 instance for your Meteor app, and an EC2 instance for your MongoDB, and make them talk to one another.
From what I understand, people do this because it's more secure, and it allows them to deploy newer versions of their app without touching the database.
I'd like to do this with Amazon EC2 alone, as opposed to using another 3rd party service, like Compose.io.
How can I host a Meteor app and its database separately on two EC2 instances, and have them communicate with one another?
It is common practice, and people mostly do it because it offers you the ability to scale them both independently.
As to the how, you'll want to obviously configure each of your Amazon EC2 instances, installing meteor on one, and MongoDB on the other. You'll also need to configure your VPC (Amazon Virtual Private Cloud) so that your MongoDB instance accepts incoming connections on whatever port you specify (default is 27017), so that your Meteor Application can connect.
After that it's just a matter of telling your meteor app where to go to get the database connection. The most secure way of doing this will be to set a couple Environment Variables, named MONGODBSERVER and MONGODBPORT, DBUSER, DBPASSWORD, etc.
You'll then want to set some variables in your server Meteor code, using something like:
Meteor.startup(function() {
var DbUser = process.env.DBUSER;
var DbPassword = process.env.DBPASSWORD;
var MongoDBServer = process.env.MONGODBSERVER;
var MongoDBPort = process.env.MONGODBPORT;
});
And if you're using the native MongoDB Driver, connecting becomes trivial:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://DbUser:DbPassword#MongoDBServer:MongoDBPort/databasename', function(err, db) {
...
});
Then it's just a matter of constructing your Mongo models using something like:
Temperatures = new Mongo.Collection('temperatures');
Temperatures._ensureIndex({temp: 1, time: 1});
And then taking action on those models in regard to the database:
Temperatures.insert({temp: ftemp, time: Math.floor(Date.now() / 1000)});
I'll also mention that http://modulus.io is a really decent Meteor hosting solution. I'd recommend them, unless you are stuck on using Amazon EC2 instances, which is fine, but more complicated for a simple application.
You need to set an Environment Variable for Mongo where it is hosted
MONGO_URL
mongodb://:#hostingproviderurl:port/xxx?autoReconnect=true&connectTimeoutMS=60000
the correct mongodb:// url string would be provided by the mongodb hosting provider.

Google App Engine PHP PDO connection fails 'MySQL server has gone away'

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.

Scala Lift - Connect to remote MongoDB

I currently have my app running on my local machine, in Boot.scala I have:
MongoDB.defineDb(
DefaultMongoIdentifier,
MongoAddress(MongoHost("127.0.0.1", 27017), "platform")
)
I've successfully deployed the app to a cloud provider, and am in the process of setting up a database # mongohq.com
What would I need to change to enable the app to connect? I've taken a look here:
https://www.assembla.com/wiki/show/liftweb/Mongo_Configuration
But am a little confused by the connection details provided by mongohq, all they provide is:
Mongo URI
mongodb://<user>:<password>#<host>:<port>/<my_account_name>
Thanks in advance for any help, much appreciated :)
I am not familiar with MongoHQ in particular, but you should be able to put something in Boot like this:
MongoDB.defineDbAuth(
DefaultMongoIdentifier,
new Mongo(new ServerAddress("<host>", <port>)),
<my_account_name>,
<user>,
<pass>
)
Where the <*> variables are the particular part of the connection URI that were provided to you when you signed up for MongoHQ.