Home » Php » php – Connecting to mysql server(localhost) very slow

php – Connecting to mysql server(localhost) very slow

Posted by: admin July 12, 2020 Leave a comment

Questions:

actually its little complicated:

summary: the connection to DB is very slow.

the page rendering takes around 10 seconds but the last statement on the page is an echo and i can see its output while the page is loading in firefox (IE is same). in google chrome the output becomes visible only when the loading finishes. loading time is approximately the same across browsers.

on debugging i found out that its the DB connectivity that is creating problem. the DB was on another machine. to debug further. i deployed the DB on my local machine .. so now the DB connection is at 127.0.0.1 but the connectivity still takes long time.

this means that the issue is with APACHE/PHP and not with mysql. but then i deployed my code on another machine which connects to DB remotely.and everything seems fine.

basically the application uses couple of mod_rewrite.. but i removed all the .htaccess files and the slow connectivity issue remains..

i installed another APACHE on my machine and used default settings. the connection was still very slow.

i added following statements to measure the execution time

    $stime = microtime();  
    $stime = explode(" ",$stime);  
    $stime = $stime[1] + $stime[0];  

// my code -- it involves connection to DB

    $mtime = microtime();  
    $mtime = explode(" ",$mtime);  
    $mtime = $mtime[1] + $mtime[0]; 

    $totaltime = ($mtime - $stime);
    echo $totaltime;

the output is 0.0631899833679

but firebug Net panel shows total loading time of 10-11 seconds. same is the case with google chrome

i tried to turn off windows firewall.. connectivity is still slow

and i just can’t quite find the reason.. i’ve tried multiple DB servers.. multiple apaches.. nothing seems to be working.. any idea of what might be the problem?

[edit]

please go through the comments section for further details. actually i think im getting near to get the solution. basically im working on developing a framework of my own which includes url rewriting through .htaccess files. i added few css and js files and i noticed that multiple requests were being sent for these files for no good reasons (in firefox). i think the problem is somewhat related to CONTENT-LENGTH header as firefox doesnt receive this header so it keeps waiting for the contents (and may be then there is a timeout).. does it have anything to do with Transfer-Encoding:chunked?

How to&Answers:

I’m late to the party here but I have a solution to this issue for any future visitors to this page.

Simply change:

 $link = mysqli_connect('localhost','username','password','db');

to:

 $link = mysqli_connect('127.0.0.1','username','password','db');

This will improve speeds by as much as 1000% on the Local Host.

Answer:

fast-cgi fixed the problem.. thanks to srisa.. just changed apache to run php files through fast-cgi