简体   繁体   中英

Cannot connect to cloud sql instance from google app engine flexible environment PHP project

i am trying to deploy a test php project on google app engine flexible environment.

My app.yaml looks like this:

*

runtime: php
env: flex
service: testphpflex
manual_scaling:
  instances: 1
#[START cloudsql_settings]
# Use the connection name obtained when configuring your Cloud SQL instance.
beta_settings:
    cloud_sql_instances: "my-first-project:us-central1:tempdb"
#[END cloudsql_settings]

*

The composer.json looks like this:

{
    "require": {
        "silex/silex": "^1.3",
        "php": "5.6.*",
        "google/apiclient": "^2.0"

    },
    "require-dev": {
        "google/cloud-tools": "^0.6",
        "paragonie/random_compat": "^2.0",
        "phpunit/phpunit": "~4"
    }
}

And my sample code looks like this:

<?php
 $conn = mysql_connect('unix_socket:/cloudsql/my-first-project:us-central1:tempdb',
         'username', 
         'password'
     );

     echo "<br><br/>connection done";
if(!$conn)
{
die('Connection Failed'.mysql_error());
}
else 
{
die('Connection successful');
}

?>

However, i am not able to connect. What is wrong?

My hypothesis is that the Google Cloud SQL API is not enabled.

Go to the following link and see if it's enabled. Enable it if not.

https://console.cloud.google.com/apis/api/sqladmin.googleapis.com/overview?project=_

Then try re-deploying the app (unfortunately you need to deploy it again). If this is the case, it's a dup of Connecting to 2nd gen Cloud SQL on App Engine flexible PHP 7.0 - missing socket

Google Cloud Support tells me that mysql_connect is deprecated. It has been deprecated in PHP since 5.5 and so there is no support for this. So the above is not supported.

I think I understand why you're getting the error.

You should use:

mysql_connect(':/cloudsql/my-first-project:us-central1:tempdb',

or

mysql_connect('localhost:/cloudsql/my-first-project:us-central1:tempdb',

According to the manual http://php.net/manual/en/function.mysql-connect.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM