4

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

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

My code is as follows:

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, http://webandphp.com/WorkingwithPHPontheGoogleCloudPlatform-166942. They use PHPStorm and a JDBC driver. However, there is no mention of using a JDBC driver in the official google tutorials.

user1801060
  • 2,733
  • 6
  • 25
  • 44
  • 1
    JDBC? That's a Java standard, so I find it odd that's what they are using. Given the error message, check what port you're using for communication. You need to pass in driver specific options to tell PDO you're using mysql so that it connects on the correct port, 3306 by default. I could be horribly wrong here, but it sounds like PDO isn't configured right, meaning the port isn't configured to use MySQL. There's other things to check too if you're just suddenly porting over to PDO: http://us1.php.net/manual/en/ref.pdo-mysql.php – Zarathuztra Jan 09 '14 at 18:52
  • Why are you using a strange connection string with a /cloudsql/ unix socket? What if you just use a standard 'mysql:dbname=wordpress_db;host=instance-IP' - the mysql command line client can connect to Cloud SQL this way, so I think PDO should be able to also - https://developers.google.com/cloud-sql/docs/mysql-client#connect – madebydavid Jan 09 '14 at 20:19
  • @madebydavid: It's not strange. Go to: https://developers.google.com/appengine/docs/php/cloud-sql/ and scroll down to the PDO section – user1801060 Jan 09 '14 at 20:35
  • @user1801060 - are you running your app inside the app engine or locally? – madebydavid Jan 09 '14 at 20:37
  • @madebydavid: Locally, the code is quite brief. Please run it for yourself – user1801060 Jan 09 '14 at 20:47
  • Ok, I've run it locally myself too, and unless it's running on the App Engine the /cloudsql socket won't work for me - but in this scenario, I get a different error message from you. Only other thing I can think of is did you add an _Authorized IP Addresses_? If you're connecting from outside the App Engine u'll need this. – madebydavid Jan 09 '14 at 21:05
  • @madebydavid: I run it on my GAE server too. I got:"Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?". Is there any way to typecast $conn from mysql_connect() as a PDO? – user1801060 Jan 09 '14 at 21:11
  • Don't think that's possible. [This tutorial](http://blog.neoxia.com/laravel-4-on-google-appengine-for-php/) shows the same problem and they had to patch the Laravel connector. Still investigating the patch they made. – madebydavid Jan 09 '14 at 21:22
  • @madebydavid: If you find the equivalent code in PHP for the connector, please let me know – user1801060 Jan 09 '14 at 21:35

1 Answers1

3

When connecting using PDO locally, you'll need to use a connection string that matches your local MySQL server settings, e.g. "mysql:host=localhost;dbname={db name};charset=utf8".

When running on GAE, your current connection string should work fine. The fact that you're getting the error message "Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?" suggests that you might have missed the "unix_socket=/cloudsql/{project id}:{instance id}" part.

Mars
  • 1,422
  • 8
  • 9