1

DSN is: mysql:dbname='MyDB';unix_socket='/cloudsql/ethereal-accord-123456789:us-central1:dev-Instance'

Connection failed: SQLSTATE[HY000] [2002] No such file or directory

PDO Connection Code:

 try {
              $conn = new PDO($dsn, $username, $password);
              // set the PDO error mode to exception

              $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

              echo "Connected successfully";
              return $conn;

            } catch(PDOException $e) {
              echo "Connection failed: " . $e->getMessage().' <BR>';

            }

I Cannot seem to find detail information on how to setup the connections correctly. I follow all documents and activated the permissions for the VM IP, the Client SQL API, and Admin API, and nothing..

Any Help will be appreciated.

nbk
  • 45,398
  • 8
  • 30
  • 47
Will Mo
  • 11
  • 2

2 Answers2

1

You don't run the cloudsql proxy or you have defined another location for the socket file. You should check this.

./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306 &
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=DATABASE_NAME';

https://cloud.google.com/sql/docs/mysql/connect-external-app#pdo-tcp

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • So that still does not work.. I guess i need to invoke the "cloud_sql_proxy" but that implies i need to install it in the "Cloud Compute VM", because whne I run ```./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306 & -bash: ../cloud_sql_proxy: No such file or directory``` again I am running this from a PHP page. - Obviously using my instance name. I can run the command ``` mysql --host=35.221.000.00x --user=root --password``` and i can connect from VM Compute Cloud to Cloud SQL not a problem. – Will Mo May 29 '20 at 19:12
  • ``` $host_name = "35.222.111.111"; $database = "myDB123"; $user_name = "dbuser123"; $password = "pass12345"; $GCSocket ="/cloudsql/ethereal-accord-2000000:us-central1:dev-instance-1"; $GCPort='3306'; $connect = mysqli_connect($host_name, $user_name,$password,$database,$GCPort,$GCSocket );``` I tried using MYSQLI and I get "Failed to connect to MySQL: Permission denied" I have the "google-cloud-sdk" installed in the Cloud-Compute-VM, not sure why i need to install the Cloud SQL Proxy if the SQL and Compute are in the same Google Project – Will Mo May 29 '20 at 20:49
  • I don't know Google Cloud very well. But is there a simple DNS-Name like all other cloud services do? – René Höhle May 30 '20 at 07:47
0

I reverted my code back to MYSqli instead of PDO using these lines

$host_name = "35.222.111.111";
$database = "myDB123";
$user_name = "dbuser123";
$password = "pass12345";
$GCSocket ="/cloudsql/ethereal-accord-2000000:us-central1:dev-instance-1"; $GCPort='3306';

$connect = mysqli_connect($host_name, $user_name,$password,$database,$GCPort,$GCSocket )
  if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error() ."<br>";
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL ."<br>";
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL  ."<br><br>";

         } else {

        echo 'DB is connected..! <BR>';
    }

    return $connect;

The issue was not with the above code, it worked all along, the issue was with permission.

  1. I whitelisted my IP from my Compute Engine VM in the Cloud SQL Server
  2. give permission to your Compute Engine Service Account to access the Cloud SQL
    • Cloud SQL Client or Cloud SQL Admin
  3. Execute this in the Compute Engine VM:
setsebool -P httpd_can_network_connect_db 1

and voila... here is where i found the answer!

Will Mo
  • 11
  • 2