My colleague Harald Zeitlhofer has blogged about this DNS Lookup problem in PHP mysql_connect before. But it keeps coming up in stories I hear from PHP applications that connect to a local MySQL instance. Therefore I thought it is time to share another Share Your PurePath story with you. This time brought in from my colleague Nalin Agrawal, Senior Solution Consultant, out of our Dynatrace Malaysia office.
With a simple change of the connect string from localhost to 127.0.0.1 in mysql_connect Nalin could achieve the following performance improvements when compared the performance of the app under load before and after the change.
- 22% more throughput
- 98% performance improvement
- 50% fewer errors!
Let’s have a quick look behind the scenes and how you can do a quick sanity check on your local app within 5 minutes using Dynatrace Free Trial!
Background: Eliminating DNS lookup problem in PHP mysql_connect
In case you run your PHP application on the same machine as your MySQL Database, and connect to it via mysql_connect using “localhost” in your connection string, you might suffer an unnecessary performance hit when your PHP code tries to establish a connection. There is a known behavior of PHP which is resolving “localhost” via DNS which could add some time to your mysql_connect call. The solution is simple: just us your local IP address – or even 127.0.0.1 (the best place on earth!) to work around this behavior and potentially shave off seconds in execution time.
Do a 5-minute sanity check on your app!
You can follow the simple steps that Nalin took using our Dynatrace Free Trial. If you sign up for the 30-day trial you can keep it afterwards to analyze your local apps. This is our quality contribution offering for developers, testers and architects. Here are the steps:
#2: Download and Install Dynatrace as instructed on the Free Trial Community Page
#3: Check out my YouTube Tutorial on how to enable Dynatrace for PHP Apps
#4: Now just open your PurePath Dashlet and verify your mysql_connect connection string and execution time
Nalin found the following in his PurePath’s showing the first mysql_connect that used localhost to take more than 1s to connect to the local instance of MySQL:
After he changed the connection string to 127.0.0.1 the improvements were pretty obvious: 3.4ms instead of 1000ms!
Share Your Findings
If you have your own findings or stories to share let me know. Please take advantage of the Share Your PurePath program, check out our YouTube Tutorial Channel, and spread the word about Dynatrace Personal License – our contribution to building better performing software right from the start!