1

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?

João Rodrigues
  • 25
  • 1
  • 10
  • *"the one with 123.123.123.123 it's using the local IP address instead of the external one"* <- the error message does **not** indicate this at all – Phil Aug 29 '17 at 01:24
  • fixed @Phil I meant the Dedicated server IP address. – João Rodrigues Aug 29 '17 at 01:37
  • I still see nothing you've presented to suggest the `$db2` connection is using a local IP. Can you please clarify **exactly** what you're trying to do, what you expect to see and what you're actually seeing? And don't use words like *"dedicatedServerIP"*. Show the error message as displayed. If you have to obfuscate details like IP addresses, **make the same changes in your example code** – Phil Aug 29 '17 at 01:43
  • I think it's clearer now. – João Rodrigues Aug 29 '17 at 01:45
  • No, it isn't. Where in your code is `185.113.141.25`? – Phil Aug 29 '17 at 01:46
  • http://i.imgur.com/9X4e6Oz.png The non-obfuscated code and the error. – João Rodrigues Aug 29 '17 at 01:50
  • where did you set the account `wizard_*blurred*`? in `localhost` or in `185.113.141.25`?? You have to create the `wizard_*blurred*` account in the host you are referring to. – Malcolm Salvador Aug 29 '17 at 01:52
  • Try not using variables. Change it to `new PDO('mysql:host=123.123.123.123;dbname=pancakeshop', 'potato_shop', 'ambiguous')`. If that literally produces the error *"Access denied for user 'potato_shop'@'185.113.141.25'"* then it would seem your application host is doing something odd on a network level. – Phil Aug 29 '17 at 01:52
  • The same happens @Phil – João Rodrigues Aug 29 '17 at 01:53
  • Have you uploaded your changes each time? Is your PHP host using [bytecode caching](https://stackoverflow.com/questions/5612945/what-is-a-bytecode-cache-and-how-can-i-use-one-in-php) by any chance? Seeing as how your PHP host and the external DB are on the same subnet, you may need to use a different IP address when connecting over their network – Phil Aug 29 '17 at 01:58
  • They cannot establish a local network connection as they are not in the same gateway on the datacenter. This is a VPS and the main .225 is a distinct web hosting. I've uploaded my changes each time and I don't think it's using bytecode caching unless it is on by default. – João Rodrigues Aug 29 '17 at 02:07
  • Still didn't figured out how to fix it. – João Rodrigues Aug 29 '17 at 14:20

1 Answers1

1

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;
Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40