简体   繁体   English

PHP远程MySQL数据库连接非常慢

[英]PHP remote MySQL database connection is very SLOW

I have 2 servers on different geographical locations Singapore and India. 我在新加坡和印度的不同地理位置上有2台服务器。

I need to connect server 1 from a php script at server 2. script looks like this: 我需要从服务器2的php脚本连接服务器1。脚本看起来像这样:

<?php 

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

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

echo microtime(true)."\n";

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

echo microtime(true)."\n";

?>

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. 如您所见,第二和第三之间的时间大约是2秒,这意味着mysql_pconnect花费了更多的2秒。 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. 另外,如果我在server1上运行此脚本并将其连接到server1本身,则需要20毫秒。

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??? 我该如何调试这个东西???

As you can see from the timings, opening a connection takes 1.4689 seconds, and the query takes 0.2855 seconds. 从计时中可以看到,打开连接需要1.4689秒,而查询需要0.2855秒。 If a one-time DNS lookup was the only issue the query would be over a lot faster: 300ms is a really long time. 如果一次性DNS查找是唯一的问题,那么查询的速度将大大提高:300毫秒是很长的时间。 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. 您可以使用ping来衡量两台计算机之间的网络延迟。

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. 没有真正的解决办法,只是确保一旦获得连接就可以充分利用它,除非绝对必要,否则避免建立新的连接。

First, try to do the same thing with PDO like this php code : 首先,尝试使用PDO像这样的php代码做同样的事情:

echo microtime(true)."\n";
$con = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');    
echo microtime(true)."\n";    
$q = $con->query("select * from tablename where id='35'");    
echo microtime(true)."\n";

If the execution time is still the same, you will have to do a cache system to reduce the number of connections to the database. 如果执行时间仍然相同,则必须执行缓存系统以减少与数据库的连接数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM