6

When I try to connect to my external server (which has allowed external connections) from my WAMP Server installation using this code:

const SERVER = 'xxx.xxx.xxx.xx'; // Redacted, the target servers IP
const PORT = 3306; 
const DATABASE = 'xxx'; // Redacted
const USERNAME = 'xxx'; // Redacted
const PASSWORD = 'xxx'; // Redacted
const TIMEOUT = 5;

try
{
    $pdo = new PDO('mysql:dbname=' . DATABASE . ';host=' . SERVER . ';port=' . PORT, USERNAME, PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => TIMEOUT));
}
catch (PDOException $e)
{
    echo $e->getMessage();
}

I get the following error message: SQLSTATE[HY000] [1045] Access denied for user 'main'@'xxx.xxx.xxx.xx' (using password: YES) where the IP address is my external (not LAN) IP address instead of the one that I specified.

I have tried this code before and it worked without any problems, but now suddenly for reasons that remain unknown to me, it attempts to connect to my own IP and fails. What am I doing wrong?

0x400921FB54442D18
  • 725
  • 1
  • 5
  • 18
  • Is the external server outside your LAN or in your LAN? If it is elsewhere on the Internet off your LAN, you need to `GRANT` to users on your external (router, NAT) IP address. – Michael Berkowski Sep 30 '17 at 21:43
  • @MichaelBerkowski I've already done that, and as I said in the post, everything worked fine until now. – 0x400921FB54442D18 Sep 30 '17 at 22:29
  • 2
    _it attempts to connect to my own IP and fails_ The error `Access denied for user 'main'@'xxx.xxx.xxx.xx'` is indicating that MySQL's host-based authentication is rejecting access from your external (NAT/router) IP, not that PDO is attempting to _connect_ to that IP. Assuming you have other administrative access to the MySQL server, you need to check the grants for your user `main`. MySQL auth is a combination of username, password, and _host of connection_ and even the same user can have 2 different passwords from 2 different origin hosts. – Michael Berkowski Sep 30 '17 at 23:30
  • How, exactly did you allow external connections to your remote database server – RiggsFolly Oct 02 '17 at 12:03

2 Answers2

2

I was having exactly the same issue with my root and other users. I was trying to connect to remote server A from another remote server B using PDO(). Although, I was able to login to mysql remote server A from remote server B using CLI but not through PDO.

Both remote server were running:

  • Ubuntu 20.04
  • Distrib 10.3.25-MariaDB using readline 5.2

The problem was special characters in my root and user password. There were #, (, !, ). So I changed my passwords and it started working.

I literally scanned the whole internet for two days to solve this problem. Didn't know it could be password.

Sam
  • 925
  • 1
  • 12
  • 28
0

Once i stored db credentials in such way:

$ cat testing.properties 
db.user = root
db.host = qwe-test.cb6vlepud6c.eu-west-1.rds.amazonaws.com
db.pass = Lix8+GPOf;SHQakU

And read them with parse_ini_file:

$ php -r 'var_dump(parse_ini_file("testing.properties"));'
array(3) {
  ["db.user"]=>
  string(4) "root"
  ["db.host"]=>
  string(51) "qwe-test.cb6vlepud6c.eu-west-1.rds.amazonaws.com"
  ["db.pass"]=>
  string(9) "Lix8+GPOf"
}

So you can see that password not correct. That was my case and i saw next error: Access denied for user 'root'@'172.31.40.198'. So domain name qwe-test.cb6vlepud6c.eu-west-1.rds.amazonaws.com was replaced with host IP-address 172.31.40.198 because of incorrect password.

zeleniy
  • 2,232
  • 19
  • 26