I have a MySQL database, hosted by me on a Windows server, and I would like to query it from a remote webserver. When I try to connect to the MySQL database using PHP (I know, I should be using mysqli):
$connection = @mysql_connect("203.0.113.0:3306", "username", "password");
With or without specifying the port, after a long time of loading, I get this error with an errorno of 2003:
Can't connect to MySQL server on '203.0.113.0' (4)
Here is what I get when I run netstat -n in command prompt on the server that is hosting the MySQL server: http://pastebin.com/pbRJNqCZ. It filled up the screen so I couldn't copy everything, but I know that everything else was (I saw a couple ports with a value of 3306, which is the MySQL port):
TCP 127.0.0.1:port 127.0.0.1:port ESTABLISHED
When I run netstat -a | find "LISTENING" I get: http://pastebin.com/Lqj2BrQK
Here's what I know so far:
- It isn't an error with the MySQL server not being up, because I can connect to it locally.
- It isn't an error with the webserver not being able to connect to MySQL databases, because I can connect to other databases
- It isn't an authentication error (The username and password are correct, and the remote server has permission)
- It isn't a port forwarding error, because the port
3306is fowarded on both TCP & UDP. - It isn't an error with being able to connect to the machine the server is hosted on, because I can ping it fine.
- The server isn't only listening on the localhost interface.
skip-networkingandbind-addressare commented out in mymy.cnffile.
How could I edit my connection code, or edit my MySQL server to fix this error?
Summarizing our discussion/chat:
Bind the network address 0.0.0.0 in my.cnf:
bind-address = 0.0.0.0and ensureskip-networkingis commented out or not present.Check
netstat -a | find "LISTENING"According to your pastebin there is a service listening on 3306. Test if the port is reachable on the NIC address from the server itself. This way an external firewall does not take effect. A simple test is to try a telnet connection to that port. More detailed information can be catched by the tool
nmap. Nmap has displayed the mysql port as filtered. This adverts to a problem with a local packet filter, in this case the Windows firewall.Ensure the Windows firewall is configured to allow public access to TCP port 3306 from all or a specific machine. Setup a rule in public profile or, if both servers are controled by the same domain controller, in domain profile. When the access from local machine is successful try the same from the remote web server.
If you fail to properly configure remote access to MySql port, consider to establish a SSH tunnel between the two machines. Once established you can access to MySql as if it were on the local machine. The port is then forwarded via the tunnel and on the database server side you can access the service on localhost loopback IP.