I have seen a number of similar questions on this forum, but nothing worked for me so far.
The setup is as follows: I have two servers, one with AWS and one 'external' one. I want the AWS instance to run a php-scripts that connects to the external MySQL. I have granted the MySQL user the rights to be approached by the external IP's (specified them). The setup works from my local computer using MySQL Workbench (so that one can connect with an accepted IP to the external MySQL.
Because AWS has both an elastic IP and a different IP for the actual $_SERVER['SERVER_ADDR']
I have allowed for both to connect at the moment.
try {
$db_ext = new PDO( 'mysql: host=xxx.xxx.xxx.xxx;
port=3306;
dbname=THE_DB_NAME',
'THE_USER_NAME', 'THE_USER_PASS' );
$db_ext->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch( PDOException $e ){
print $e->getMessage();
die();
}
Now when I run this it gives an error:
SQLSTATE[28000] [1045] Access denied for user 'THE_USER_NAME'@'localhost' (using password: YES)
I am puzzled... (also about that it denies access for 'THE_USER_NAME'@'localhost'
and not for 'THE_USER_NAME'@'IP-OF-SERVER-RUNNING-THE-PHP-SCRIPT'
Question is answered: Thanks Marc B! (I can't believe I didn't see this myself). It was the space between mysql: host
. So this code works to connect from one machine with another's MySQL as long as the IP is allowed.
try {
$db_ext = new PDO( 'mysql:host=xxx.xxx.xxx.xxx;
port=3306;
dbname=THE_DB_NAME',
'THE_USER_NAME', 'THE_USER_PASS' );
$db_ext->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch( PDOException $e ){
print $e->getMessage();
die();
}
I am gonna go back to my desk and be ashamed now ;)