I am trying to connect to an external database using PDO.
$dbh = new PDO('mysql:host=hotsname;port=3309;dbname=dbname', 'root', 'root');
However this is not working on one particular server I have.
I thought maybe only a certain host was allowed, but I checked the mysql rules and also tried this code on a server I have personally and the connection worked.
So I know the code works and that their is no block on my side and that the firewall is accepting requests to port 3309 and passing it to the correct server on port 3306.
So the issue is solely on one server. As a test I thought I'd open port 3306 to test the code without specifying a port.
$dbh = new PDO('mysql:host=hotsname;dbname=dbname', 'root', 'root');
This worked straight away.
So my question is, why when adding the port does this cause it not to be able to connect to the database. It is only this particular server that has this issue.
Many Thanks for your time.
Update
The error given is simply "Cannot connect to mysql server on xxx.xxx.xxx.xxx".
I have gotten slightly further now. The code is now working providing I have a firewall rule to allow incoming on 3306 to the server using 3306. So even though I have specified a port, it seems to ignore this and force it to go out of 3306.
skip-networkingnot enabled in my.cnf)skip-networkingis disabled. The connecting user has permissions as proven by me using the same code on other servers successfully as well as checking the mysql table and ensuring the allowed host is wildcard%. It certainly seems as though the server I am having trouble with just ignores the port completely and only uses 3306. It is a hosted server with cpanel.mysql -hhostname -P3309 -uroot -prootBeyond that you may need to talk with the host's support. Your PDO is correct the way you have it.