Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So here's one for you. Any idea on a way to shorten the time it takes to connect to a MySQL database?

The reason I'm wondering is because I find that just connecting to the DB adds just over a second to the rendering of the page and that seems a bit long considering Apache and MySQL is running on the same machine and the mysqli_connect is connecting to localhost. It's just such a shame when the connection takes a second while any query I end up doing won't add any significant amount of time to the render/load time.

Any ways to shorten the time it takes to open a connection?

share|improve this question
It is not normal to have 1 second connection delay. Can you test if the same delay applies when connecting using standard mysql client? Can you connect from console and let us know if you are still facing the same delay? – Hex Oct 25 '12 at 9:40
Have you tried switching localhost for ? You can also try adding skip-name-resolve to your my.cnf. – James Yale Oct 25 '12 at 9:55
@JamesYale Just tried using and it connected, well the page rendered, a lot faster. I'm a bit baffled by the difference... – Leonick Oct 25 '12 at 10:21
@Hex Connecting with the mysqli client doesn't exactly give me a time to go by but it seemed to connect a bit quicker than in my quick php test page but about the same. Switching to instead of localhost did the same thing in this case, connection next to instant. – Leonick Oct 25 '12 at 10:23
Did you check if you can see any logs regarding the delay? – Hex Oct 25 '12 at 10:37
up vote 0 down vote accepted

With so little information to go on it's hard to offer much but I will tell you that you will be better off setting your configuration to connect to MySQL by IP ( address than by name. If nothing else, you will remove one complete layer from the connection.

share|improve this answer
By specifying the client will use a network connection, but by specifying localhost the client will choose a socket connection over network and thus eliminating the use of the network layer. – pkhamre Oct 25 '12 at 10:02
And yet is is faster in most cases. – John Gardeniers Oct 25 '12 at 22:28
It's faster to access MySQL through network rather than a unix socket? New to me. – pkhamre Oct 26 '12 at 6:07
You didn't read the question properly, which is about the initial connection, not the access time. – John Gardeniers Oct 26 '12 at 7:24
This is indeed a good solution as the slow connection seem to have been due to the lookup being done for localhost. Amazing how slow that was. @pkhamre While it's appreciated that you try to help you really should have checked the question, not only because you're commenting on an answer for a question but also because by doing so you would have seen the tags and known that it wouldn't choose a unix socket since I'm running on Windows. – Leonick Oct 27 '12 at 20:23

Since everything is local, you can skip networking (TCP/IP, DNS, etc) and use a socket or network-pipe connection to MySQL.

share|improve this answer

You should use persistent connexion from your application to the mysql server, this way you won't have to initiate a new TCP connexion each time a request is done.

Have a look at this documentation:

Update: Care to explain why I got that downvote ?

share|improve this answer

Your Answer


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.