0

I have a google cloud platform mysql instance (say m1) with both private and public IP. I want to connect to the database using PHP PDO from a local machine (say m2) over the internet. I have authorized the IP of m2 in m1's public network. I am able to connect to the MySQL db@m1 from m2 and create tables using mysql client from bash.

$rr@m2:~ mysql -h public_ip_of_m1 -u root -p mypass

This is successful.

However, from the same machine when connecting from a PHP application

<?php
 try {
$dbhost = '35.x.x.x';
$dbuser = 'root';
$dbpass = 'xxxx';
$dbase = 'xxxx';

$connConfig = [
    PDO::ATTR_TIMEOUT => 5,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]; 

$conn = new PDO('mysql:host=$dbhost; dbname=$dbase', $dbuser, $dbpass, $connConfig);
}catch (PDOException $e){
 echo "Error!: " . $e->getMessage() . "<br/>";
 die();
 }
 
 ?>

fails and shows Error!: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known.

I have not used "Cloud SQL Auth proxy". Is it absolutely required even for connecting from local applications to public IP over the internet? I certainly did not feel so from the docs. Would anyone please point out what is going wrong here? Thank you.

rajesh
  • 313
  • 3
  • 9
  • The driver specific error code and message ("php_network_getaddresses: getaddrinfo failed: Name or service not known") tell you that PDO is not able to resolve the host name. The error reveals that you did not specify the database connection parameters in the configuration file. You need to update /.env with the actual database connection parameters. – Jose Gutierrez Paliza Oct 06 '21 at 20:02
  • Thank you for your response. The error was due to the single quotes. – rajesh Oct 09 '21 at 10:41

0 Answers0