2

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?

8
  • 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, 2012 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, 2012 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, 2012 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, 2012 at 10:23
  • Did you check if you can see any logs regarding the delay?
    – Hex
    Oct 25, 2012 at 10:37

3 Answers 3

1

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.

11
  • 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, 2012 at 10:02
  • And yet is is faster in most cases. Oct 25, 2012 at 22:28
  • It's faster to access MySQL through network rather than a unix socket? New to me.
    – pkhamre
    Oct 26, 2012 at 6:07
  • You didn't read the question properly, which is about the initial connection, not the access time. Oct 26, 2012 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, 2012 at 20:23
0

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

-1

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 ?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .