Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Jun 15 '12 at 9:10

This question came from our site for professional and enthusiast programmers.

    
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? –  pythonscript 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 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 as well. –  pythonscript 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 6:00

2 Answers 2

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.

share|improve this answer
    
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 5: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 7: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 2:04

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.

share|improve this answer
    
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 6:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.