mysql - CakePHP connect to Google SQL Proxy Container - Stack Overflow

admin2025-04-17  2

For starters, I have a docker container running for "cloud-sql-connectors/cloud-sql-proxy:2.14.3" and it has credentials and appears to have started and is running fine.

2025-01-29 12:31:44 2025/01/29 18:31:44 Authorizing with the credentials file at "/path/to/service-account-key.json"
2025-01-29 12:31:44 2025/01/29 18:31:44 [project-name:us-central1:instance-name] Listening on [::]:5432
2025-01-29 12:31:44 2025/01/29 18:31:44 The proxy has started successfully and is ready for new connections!

I have a CakePHP app, just the basic starter app right now, and I am trying to connect to the SQL database through the proxy container.

In my 'app.php' file, I have 'Datasources' as below

'Datasources' => [
        'default' => [
            'className' => Connection::class,
            'driver' => Mysql::class,
            'persistent' => false,
            'host' => '[::]:5432', // I have also tried 'localhost:5432'
            'username' => 'myusername',
            'password' => 'mypassword',
            'database' => 'mydatabasename',
            'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'encoding' => 'utf8mb4',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,
            'quoteIdentifiers' => false,
        ]

The docker container was created using:

docker run -d -v F:\local\path\to\service-account-key.json:/path/to/service-account-key.json -p 127.0.0.1:5432:5432 gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.3 --address 0.0.0.0 --port 5432 --credentials-file /path/to/service-account-key.json project-name:us-central1:instance-name

I am trying to develop the app on my local machine, while using the Google Cloud SQL database so I don't have to worry about figuring this step out after development. Needing to bake the database in question so I can actually start figuring out what I need to do in regards to my own logic.

I'm running the app inside a docker container as well, in hopes to load it up to App Engine.

All online searches either take me to Google's documentation which has very little about actually use cases after getting the proxy running... or 7+ year old CakePHP stuff that doesn't really talk about Google SQL Proxy at all.

And searching the title of the question here on Stack Overflow gave 0 results.

The CakePHP debug landing page says it isn't connecting:

Database CakePHP is NOT able to connect to the database.

Connection to Mysql could not be established: SQLSTATE[HY000] [2002] Connection refused

But the Proxy's container doesn't even show an attempt to connect was made (if I try to go to localhost:5432 the container does accept the connection, but is closed by client/browser, due to it not sending any actual code to display on the HTML, it shows:

localhost sent an invalid response.

ERR_INVALID_HTTP_RESPONSE"

Any help appreciated, thanks in advance.

For starters, I have a docker container running for "cloud-sql-connectors/cloud-sql-proxy:2.14.3" and it has credentials and appears to have started and is running fine.

2025-01-29 12:31:44 2025/01/29 18:31:44 Authorizing with the credentials file at "/path/to/service-account-key.json"
2025-01-29 12:31:44 2025/01/29 18:31:44 [project-name:us-central1:instance-name] Listening on [::]:5432
2025-01-29 12:31:44 2025/01/29 18:31:44 The proxy has started successfully and is ready for new connections!

I have a CakePHP app, just the basic starter app right now, and I am trying to connect to the SQL database through the proxy container.

In my 'app.php' file, I have 'Datasources' as below

'Datasources' => [
        'default' => [
            'className' => Connection::class,
            'driver' => Mysql::class,
            'persistent' => false,
            'host' => '[::]:5432', // I have also tried 'localhost:5432'
            'username' => 'myusername',
            'password' => 'mypassword',
            'database' => 'mydatabasename',
            'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'encoding' => 'utf8mb4',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,
            'quoteIdentifiers' => false,
        ]

The docker container was created using:

docker run -d -v F:\local\path\to\service-account-key.json:/path/to/service-account-key.json -p 127.0.0.1:5432:5432 gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.3 --address 0.0.0.0 --port 5432 --credentials-file /path/to/service-account-key.json project-name:us-central1:instance-name

I am trying to develop the app on my local machine, while using the Google Cloud SQL database so I don't have to worry about figuring this step out after development. Needing to bake the database in question so I can actually start figuring out what I need to do in regards to my own logic.

I'm running the app inside a docker container as well, in hopes to load it up to App Engine.

All online searches either take me to Google's documentation which has very little about actually use cases after getting the proxy running... or 7+ year old CakePHP stuff that doesn't really talk about Google SQL Proxy at all.

And searching the title of the question here on Stack Overflow gave 0 results.

The CakePHP debug landing page says it isn't connecting:

Database CakePHP is NOT able to connect to the database.

Connection to Mysql could not be established: SQLSTATE[HY000] [2002] Connection refused

But the Proxy's container doesn't even show an attempt to connect was made (if I try to go to localhost:5432 the container does accept the connection, but is closed by client/browser, due to it not sending any actual code to display on the HTML, it shows:

localhost sent an invalid response.

ERR_INVALID_HTTP_RESPONSE"

Any help appreciated, thanks in advance.

Share Improve this question edited Jan 31 at 15:36 Basheer Jarrah 6324 silver badges16 bronze badges asked Jan 31 at 6:44 Me JW CrouchMe JW Crouch 113 bronze badges 3
  • Can you test the connexion with mysql command directly in the terminal and see the logs of sql auth proxy ? mysql -h 127.0.0.1 -u USERNAME -p -P And can you verify this requirements plz ? Does the instance have public IPv4 address ? Does the service account used have a role that includes the cloudsql.instances.connect (any of this roles: Cloud SQL Client, Cloud SQL Editor, Cloud SQL Admin) – Abdellatif Derbel Commented Jan 31 at 8:24
  • Service Account: yes, it does have the Cloud SQL Client role Public IPv4: yes, but if I put that in Cakes Datasources dictionary above, I still get the same connection refused error. As for connecting directly... I downloaded a fresh install of MySQL... had to add the bin to path manually, but when I tried to run the above, I get ERROR 2059 (HY000): Authentication plugin 'auth_gssapi_client' cannot be loaded: The specified module could not be found. If I try to connect with the public IP in the mentioned way, I get a simple Can't connect to MySQL server on 'the-ip:3306' error – Me JW Crouch Commented Jan 31 at 16:30
  • le sigh. Well for starters, helps if I use the right password... I can confirm that with the public IP, and the right password in the Datasources info, I am now connecting to the Database. This may or may not be related to also adding my IP as an authorized network for the Cloud SQL Instance... (Which I did specifically to test connection with SQL Workbench.) I may still have no idea what I'm doing, but this is answered. @AbdellatifDerbel, your inquiry got me poking into things that made me realize I was using the incorrect password, and got me to add my IP to authorized networks. – Me JW Crouch Commented Jan 31 at 17:00
Add a comment  | 

1 Answer 1

Reset to default 0

Jonathan from the Cloud SQL Auth Proxy team here. It looks like you have your proxy set up correctly. I think that the configuration of your application's docker container may need to be adjusted.

You got the first step correct. You started your proxy docker container with this command:

docker run -d \ 
  - v F:\local\path\to\service-account-key.json:/path/to/service-account-key.json \
  -p 127.0.0.1:5432:5432 \
  gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.3 \
  --address 0.0.0.0 \
  --port 5432 \
  --credentials-file /path/to/service-account-key.json \
  project-name:us-central1:instance-name

This flag: -p 127.0.0.1:5432:5432 binds host machine port 127.0.0.1:5432 to port 5432 inside the container, where the proxy is listening for connections.

Now, you need to run your application's container so that it can reach the host network interface and connect to 127.0.0.1:5432.

There are several ways to do this, but for local development the easiest is to start your application container with the --network=host flag. This way the PHP application inside the container will behave as if it were connected directly on your host machine's network interfaces, which will give it access to the host machine's 127.0.0.1:5432 port.

docker run -d \
   --network=host \
   your-application-image

In your PHP application, configure the database to connect to 127.0.0.1:5432 It is generally better to use the localhost IP address than the hostname "localhost".

'Datasources' => [
        'default' => [
            'className' => Connection::class,
            'driver' => Mysql::class,
            'persistent' => false,
            'host' => '127.0.0.1:5432',
            'username' => 'myusername',
            'password' => 'mypassword',
            'database' => 'mydatabasename',
            'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'encoding' => 'utf8mb4',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,
            'quoteIdentifiers' => false,
        ]

As a more secure alternative, you may consider using Docker Compose, however that is beyond my area of expertise.

Let us know if this works for you.

转载请注明原文地址:http://anycun.com/QandA/1744877685a88894.html