jaredlt
jaredlt

Reputation: 713

How to connect to a Google Cloud SQL database from Metabase (when Metabase is running on Google Cloud Run)

I am running Metabase on Google Cloud Run and am trying to connect to a MySQL database instance (which also resides in Google Cloud SQL, in the same project).

NB. This is not Metabase's application database, but rather connecting a database to perform analysis on the data as per https://www.metabase.com/docs/latest/setting-up-metabase.html

For connecting to the database I am constrained to using Metabase's web interface:

enter image description here

Within this interface I have tried:

Thoughts:

Any help would be greatly appreciated.

Upvotes: 1

Views: 2901

Answers (3)

Chris Ivan
Chris Ivan

Reputation: 556

2 years later, and I think perhaps conditions have changed. I was able to do this on Google App Engine, using the beta_settings configuration option in the app.yaml definition file.

For example

beta_settings:
  cloud_sql_instances: 
    - <Project><Zone><Metabase_backend_postgresql_instance>=tcp:5432
    - <Same Project><Same Zone><Some MySql cloud instance>=tcp:3306

When adding the database connection in the Metabase UI, the IP address needs to be set to 127.17.0.1 per Google documentation on the flex environment.

Upvotes: 0

Andrei Tigau
Andrei Tigau

Reputation: 2048

You cannot connect to CloudSQL thorugh any TCP connections as it is stated in the documentation:

Cloud Run (fully managed) does not support connecting to the Cloud SQL instance using TCP. Your code should not try to access the instance using an IP address such as 127.0.0.1 or 172.17.0.1.

You can connect to the CloudSQL instance, using the instance connection name using a code similar to this:

// The configuration object specifies behaviors for the connection pool.
HikariConfig config = new HikariConfig();

// Configure which instance and what database user to connect with.
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
config.setUsername(DB_USER); // e.g. "root", "postgres"
config.setPassword(DB_PASS); // e.g. "my-password"

// For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections.
// See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
config.addDataSourceProperty("useSSL", "false");

// ... Specify additional connection properties here.
// ...

// Initialize the connection pool using the configuration object.
DataSource pool = new HikariDataSource(config);


  [1]: https://cloud.google.com/sql/docs/mysql/connect-run

If you are concerned about the security of the connection you can always choose to connect to the CloudSQL proxy, using the JDBC socket factory. Please note that in this situation your JDBC URL should look like this :

jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>

Try to connect using the full URL and review it a few times. I've see this identical error when people did a little mistake in the JDBC Url, like an extra semicolon, colon etc..

Upvotes: 1

guillaume blaquiere
guillaume blaquiere

Reputation: 75745

You can't use Cloud SQL private IP with Cloud Run, it's not yet compliant, you can forgot this way. Read replica will change nothing, you can also save money and delete it!

First, allow you Cloud SQL database to be reached by 0.0.0.0/0 network. Like this, you can validate that your Cloud Run container work correctly with a database open to internet.

Then, delete this configuration and retry to follow this page for connecting your MySQL instance to Cloud Run as you did. In your application, you also have to add a dependency (maven or gradle) for getting the socket factory jar.

It should work. Share more code or configuration for being able to help you more!

Upvotes: 0

Related Questions