1

When I try to connect to a host on my LAN with mysql_connect();, the connections succeed some of the time and fail at other times. I can always ping the MySQL server successfully, but when I try to use telnet to connect to it through port 3306, it doesn't always work. I can connect to post 3306 the first time, but not after that.

5.1.58-1ubuntu1)[n9pK8'Ym7j1Q'khH Got packets out of order
Lost connection with host

C:\Documents and Settings\Administrator>telnet 10.253.48.49 3306
Connecting to 10.253.48.49... Cannot open connection to the host on port 3306:
Connect failed

Pinging is always successful, however:
Reply from 10.253.48.49: bytes=32 time<1ms TTL=64
Reply from 10.253.48.49: bytes=32 time<1ms TTL=64
Reply from 10.253.48.49: bytes=32 time<1ms TTL=64
Reply from 10.253.48.49: bytes=32 time<1ms TTL=64
Reply from 10.253.48.49: bytes=32 time<1ms TTL=64

Also, other clients on the LAN can connect to the host, even when I can't. I can connect to another host successfully every time. How can I solve this error?

  • Could you post the PHP code you're using? Also, you said that you can connect to another host on the LAN using `telnet`. I assume this is on a different machine, correct? Are both machines running ubuntu, or if not, what system are they running? Are the firewalls configured differently on each host, perhaps? –  Jun 14 '12 at 14:17
  • Also, don't use `mysql_connect()` or other `mysql_*` functions any more. These functions are deprecated and will probably be removed in the future. Take a look at [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) for help in choosing between either PDO or MySQLi, the newer database interfaces that are recommended instead of `mysql_*` Both of these interfaces support [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) as well. –  Jun 14 '12 at 14:22
  • Thank you for your advise.host is different,my machines runs windows,and the host I can't connect well runs ubuntu. Firewall setting is OK. If not,I couldn't even connect to it. – woo's not wei Jun 20 '12 at 06:00
  • did you find a solution, i have the same problem – Smith Sep 11 '16 at 08:17
  • @Smith I can't recall for 4 years passed, may be consider change a machine or reinstall system? – woo's not wei Sep 13 '16 at 03:37

2 Answers2

1

Long shot here, but do you have a duplicate IP address on the network? I.e. one that hosts mysql and one that doesn't and you randomly get connected to the right one?

Try doing an arp -a and checking the MAC address of the IP you believe to be the right one. It might take a few attempts but if you have a duplicate IP, you'll eventually get a different MAC address for the same IP.

FreudianSlip
  • 131
  • 3
  • I checked arp-a and didn't find any duplicate IP or MAC address.If there has a duplicate IP or MAC address in LAN,how could I always ping it successful? – woo's not wei Jun 20 '12 at 05:57
  • Providing at least one of the suspected duplicate IP's were available you'd always manage to ping one of them. The idea is that you ping the suspect IP several times, maybe over the course of an hour or 2, and record the MAC that responded. As soon as you get a reply from the IP that has a different MAC you know you're on the right path. – FreudianSlip Jun 20 '12 at 07:33
  • If the host has a different MAC, why others can always connect to it except my machine? – woo's not wei Jun 20 '12 at 10:32
  • It's a real good question; does your machine have more than 1 network port? – FreudianSlip Jun 20 '12 at 12:24
  • Yes, dose it matter? – woo's not wei Jun 21 '12 at 02:04
  • It could be your routing at fault in that case - i.e. the decision to go down which network adapter. As long as they're on different subnets with relevant (working) subnet masks and the routing is configured correctly then there should be no problem, however if the routing is broken, the system could be confused about which network adapter to send or the destination could be confused as to where to send the replies. – FreudianSlip Jun 21 '12 at 08:02
  • Any joy with the above? – FreudianSlip Jul 15 '12 at 06:39
  • Routing is OK, and two machine are in the same sub net.I can't figure it out anyway. – woo's not wei Jul 17 '12 at 12:59
0

On the host machine for MySQL (which I presume is running Ubuntu, based on your error message), try running this command:

sudo netstat -lntp | grep 3306

You should see the mysqld daemon running. Otherwise, the problem could be that the machine's MySQL daemon isn't running. I think the command on Ubuntu is /etc/init.d/mysql start run as root, which will start the daemon if it's not running. (I don't use Ubuntu, but that's what an google search comes up with. On Arch it's rc.d start mysqld but it doesn't translate to Ubuntu exactly).

If that isn't the problem, i.e. the daemon is running on the host machine, the problem could be that the firewall isn't configured properly and that the port isn't open. Try this command to see if the port is open (both of these commands are run on the host machine running MySQL, not on your local machine where you're using PHP)

nmap -v -sT localhost

If you don't see 3306 listed as open and the machine is using iptables as a firewall, this command should open that port

iptables -I INPUT -p tcp --dport 3306 -d host.machine.ip.address -s your.machine.ip.address -j ACCEPT

which should allow your client machine (the one running mysql_connect(), even though it shouldn't be) to access the MySQL host machine.

Also, as I mentioned in my comment (hat tip to RikudoSennin for this): Please don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you care to learn, here is good PDO tutorial.

EDIT: After a few more online searches, I see that another user on ServerFault responded to this problem with two of the three commands I proposed, so I'm linking to that question since it may be relevant.

  • Thank you for your advise.Host is different,my machines runs 'windows',and the host I can't connect well runs 'ubuntu'. Firewall setting is 'OK'. If not,I couldn't even connect to it at first.'3306' port in that ubuntu host is opened and 'iptables' is 'OK'. – woo's not wei Jun 20 '12 at 06:07