PHP PDO MySQL – not using the host specified on DSN

Good evening,

I’ve been trying to struggle on having two database connections in my config file, one being internal and another one being external.

$DB_host = '127.0.0.1';
$DB_port = '3306';
$DB_name = 'users';
$DB_user = 'root';
$DB_pass = 'obliquous';

$DB2_host = '123.123.123.123';
$DB2_port = '3306';
$DB2_name = 'pancakeshop';
$DB2_user = 'potato_shop';
$DB2_pass = 'ambiguous';

try
{
     $db = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
     $db2 = new PDO("mysql:host={$DB2_host};dbname={$DB2_name}",$DB2_user,$DB2_pass);
     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
     echo $e->getMessage();
     die();
}

The one I’ve done with 127.0.0.1 – meaning localhost I’ve specified for the dedicated server – it’s working as expected, although, the one with 123.123.123.123 it’s using the local IP address instead of the external one.

185.113.141.125 is the dedicated server the PHP server is running.

The error it’s being given to me it’s the following:

SQLSTATE[HY000] [1045] Access denied for user ‘potato_shop’@’185.113.141.125’ (using password: YES)

Is there anything wrong on the code syntax?

This is because root accounts is only for the localhost host (at least, in windows).

Be simpler if you simply create a user with privilege rights to pancakeshop

CREATE USER 'not_root'@'123.123.123.123' IDENTIFIED BY 'ambiguous';

GRANT ALL PRIVILEGES ON pancakeshop.* To 'not_root'@'123.123.123.123' IDENTIFIED BY 'ambiguous';
FLUSH PRIVILEGES;