views:

25

answers:

1

I'm trying to connect to a different machine:

  $this->_connection = new PDO("mysql: host=MYSQL_SERVER; dbname=MYSQL_DATABASE",MYSQL_USER, MYSQL_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

But PDO barfs:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Infuriatingly, this worked fine with localhost on my dev server - our production setup is an LVS with a separate DB server though, and I can't seem to get PDO to connect to it!

Where, oh where have I bungled what here?

Edit:

This works:

mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD) or die(mysql_error());
mysql_select_db(MYSQL_DATABASE) or die(mysql_error());;
echo 'Connected to database <br/>';

Note: MYSQL_SERVER is not localhost, it is the IP of our database master server. On our dev server, which hosts the dev database, PDO works flawlessly.

+1  A: 

It's very simple. Your DSN is flawed.

Use this:

  $this->_connection = new PDO("mysql:host=EXTERNAL_IP;dbname=DB", USERNAME, PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

Try it and report back.

hopeseekr
Forgive me if I'm being dense, but I don't see how that is different to what I've posted in my question
Michael Robinson
I believe the space after the ; to be significant.
hopeseekr
Wow. Thank you very much - I've had about 2 hours sleep in the last 48, you can't know how much I appreciate such a simple solution. :) :D
Michael Robinson
AWESOME, Bro!!BTW, the reason is cuz the MySQL library **sucks** (due to MySQL devs). See http://stackoverflow.com/questions/237367/why-is-php-pdo-dsn-a-different-format-for-mysql-versus-postgresql
hopeseekr