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

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 127.0.0.1 ? You can also try adding skip-name-resolve to your my.cnf. –  James Yale Oct 25 '12 at 9:55
    
@JamesYale Just tried using 127.0.0.1 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 127.0.0.1 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
show 3 more comments

3 Answers

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 (127.0.0.1) address than by name. If nothing else, you will remove one complete layer from the connection.

share|improve this answer
3  
By specifying 127.0.0.1 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
1  
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
show 6 more comments

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

http://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_skip-networking

share|improve this answer
add comment

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: http://php.net/manual/en/mysqli.persistconns.php

Update: Care to explain why I got that downvote ?

share|improve this answer
add comment

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.