Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO keeps connecting to wrong IP

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?

like image 358
0x400921FB54442D18 Avatar asked Nov 08 '22 16:11

0x400921FB54442D18


1 Answers

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.

like image 70
Sam Avatar answered Nov 14 '22 23:11

Sam