Unable to connect to Google Cloud SQL using PDO

admin

Administrator
Staff member
I'm a PHP newbie. I'm trying to connect to GAE using PHP and PDO. I have tried connecting using mysql_connect() and mysql_select_db(). I have been successful. However when I try to connect using PDO, I get an error

Code:
Connection failed: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

My code is as follows:

Code:
define('DBH_SOCKET', '/cloudsql/****:****');
define('DBH_NAME', 'wordpress_db');
define('DBH_USER', 'censored');
define('DBH_PASSWORD', 'censored');

$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> var is ".$pdo_conn;

/*$conn = mysql_connect(DBH_SOCKET, DBH_USER, DBH_PASSWORD);
if (!$conn)
    die('Connect error ('.mysql_error());

$db_selected = mysql_select_db(DBH_NAME, $conn);
if(!$db_selected)
    die('Cant use db: '.mysql_error());*/

What am I doing wrong? I have looked at some tutorials on the net like, <a href="http://webandphp.com/WorkingwithPHPontheGoogleCloudPlatform-166942" rel="nofollow">http://webandphp.com/WorkingwithPHPontheGoogleCloudPlatform-166942</a>. They use PHPStorm and a JDBC driver. However, there is no mention of using a JDBC driver in the official google tutorials.