_connection = new PDO("...">

Connecting to another machine, "Cannot connect via connector" Error

I am trying to connect to another 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] Unable to connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Worried, this worked fine with localhost on my dev server — our production setup is LVS with a separate DB server, although I can't get the PDO to connect to it!

Where, oh, where did I mess that up here?

Edit :

It 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_SERVERthis is not a local host, this is the IP address of our primary database server. On our dev server hosting the dev database, PDO works flawlessly.

+3
1

. DSN .

:

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

.

+1

Source: https://habr.com/ru/post/1762982/


All Articles