Connecting to mysql server (localhost) is very slow

actually its a little complicated:

summary: connecting to the database is very slow.

page rendering takes about 10 seconds, but the last statement on the page is an echo, and I can see its output while the page loads in firefox (IE is the same). in google chrome, the output becomes visible only when the download is complete. Download time is approximately the same in all browsers.

during debugging, I found out that connecting it to the database creates a problem. The database was on another machine. for further debugging. I deployed the database on my local computer .. so now the connection to the database is at 127.0.0.1, but the connection still takes a lot of time.

this means that the problem is with APACHE / PHP, not mysql. but then I deployed my code on another computer that connects to the database remotely. And everything seems beautiful.

basically the application uses a couple of mod_rewrite .. but I deleted all the .htaccess files and the problem of slow connection remained.

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

I added the following statements for measuring runtime

$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; 

output 0.0631899833679

but the Firebug Net panel shows a total boot time of 10-11 seconds. the same thing happens with google chrome

I tried disabling the windows firewall .. the connection is still slow

and I just can’t find the reason .. I tried several DB servers .. several Apaches .. nothing seems to work .. any idea what could be the problem?

[edit]

Please see the comments section for more information. in fact, I think I'm getting closer to a solution. I am mainly working on developing my own structure, which includes rewriting url through .htaccess files. I added some css and js files, and I noticed that several requests are sent for these files for no good reason (in firefox). I think the problem is with the CONTENT-LENGTH header, since firefox does not receive this header, so it continues to wait for the content (and maybe then there is a timeout). Does this have anything to do with Transfer-Encoding: chunked?

+7
source share
3 answers

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

-2
source

I'm late for the party, but I have a solution to this problem for future visitors to this page.

Just change:

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

in

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

This will improve speed by 1000% on the local host.

+25
source

First, forget about redherrings, php connects directly to mysql, it does not use apache..htaccess is also not connected.

Can you provide a code on how you connect to db? Of course, we do not want your password.

Can you just connect to the db part?

What queries do you fulfill?

The time of each of them. You must isolate the actual line of code causing the problem.

DC

0
source

All Articles