douyin4561 2013-07-16 12:35
浏览 102
已采纳

PHP远程MySQL数据库连接很慢

I have 2 servers on different geographical locations Singapore and India.

I need to connect server 1 from a php script at server 2. script looks like this:

<?php 

echo microtime(true)."
";
$con = mysql_pconnect('server1','username','password');

$db = mysql_select_db('database_name',$con);                      

echo microtime(true)."
";

$q = "select * from tablename where id='35'";
$result = mysql_query($q);

echo microtime(true)."
";

?>

The out put of this script is like this:

1373977322.9081
1373977324.377
1373977324.6625

As you can see the time between the 2nd and 3rd is around 2 seconds, which means mysql_pconnect is taking more 2 seconds. And time between 3rd and 4th(select query) is very less.

Also if I run this script on server1 connecting it to server1 itself it takes 20 ms.

Can't figure out why connection time is soo long. I have also tried some things like skip-name-resolve and persistent connections. but :(

How can I debug this thing???

  • 写回答

2条回答 默认 最新

  • douhu2890 2013-07-16 12:57
    关注

    As you can see from the timings, opening a connection takes 1.4689 seconds, and the query takes 0.2855 seconds. If a one-time DNS lookup was the only issue the query would be over a lot faster: 300ms is a really long time. This means the problem must be somewhere else.

    When the connection is first opened the client and the server go through a negotiation where one asks the other a question and then waits for a reply, many times. If the network has high latency each question-answer cycle takes a non-trivial amount of time, and that adds up. You could use ping to measure the network latency between the two machines.

    That's why you see practically no delay for local connections (low latency) and why queries run fast once the connection is established (no negotiation). There's no real fix, just make sure that once you get a connection you make the most of it and avoid making new connections unless absolutely necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题