How to connect to Google Cloud SQL using native PHP hosted on Hostinger?

Viewed 75

I hope they are well, I ask for your help please since I have been researching a lot without finding a solution, I currently have my system hosted in Hostinger, everything worked fine, however I need to migrate the database to Google Cloud SQL, until now I have everything ready, my database is already in the cloud and I was able to connect with workbench, but when connecting from my project there was a problem, my code is:

define('DBH_SOCKET', '/tmp/mysql.sock/project-test:us-east1:test');
define('DBH_NAME', 'db_test');
define('DBH_USER', 'admin');
define('DBH_PASSWORD', 'df434hsXsfvr45hgfhd');

$pdo_conn = "mysql:unix_socket=".DBH_SOCKET.";dbname=".DBH_NAME.";charset=utf8";

try {
    $dbconn = new PDO($pdo_conn, DBH_USER, DBH_PASSWORD);
    $dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
    echo 'Connection failed: ' . $e->getMessage();
}

echo "<br> db is ".$pdo_conn;

with this code I get the following error:

Connection failed: SQLSTATE[HY000] [2002] No es un directorio
var is mysql:unix_socket=/tmp/mysql.sock/project-test:us-east1:test;dbname=db_test;charset=utf8

I do not know what is happening or what I am doing wrong since I have asked hostinger what is the path of his socket for the connections and they send me to this link: https://support.hostinger.com/en/articles/1583557- what-is-my-database-socket-path, Because my project is hosted by Hostinger's Cloud Hosting.

I hope you can help me.

1 Answers

I did it with the public address method

Related