首页 > 代码库 > mysql长连接

mysql长连接

长连接是干嘛的:  它是做连接复用的;

    在openresty中的lua-resty-mysql 里

    connect方法去连接mysql时会去ngx_lua cosocket连接池中寻找是否有可用连接

    如果有就直接复用;

    如果没有就与mysql建立新连接,连接成功后立即将这个连接放入ngx_lua cosocket连接池中。

 

本人用ngx-lua + mysql 做的一个测试

  

开启长连接:

技术分享

[root@www ~]#  webbench -c 100 -t 60 "http://www.new.cn/auth/test"
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.new.cn/auth/test
100 clients, running 60 sec.

Speed=47102 pages/min, 392419 bytes/sec.
Requests: 45901 susceed, 1201 failed.
[root@www ~]#  webbench -c 1000 -t 60 "http://www.new.cn/auth/test"
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.new.cn/auth/test
1000 clients, running 60 sec.

Speed=228165 pages/min, 1930685 bytes/sec.
Requests: 225810 susceed, 2355 failed.

 

 

 

 

 

关闭长连接:

技术分享

 

[root@www ~]#  webbench -c 100 -t 60 "http://www.new.cn/auth/test"  
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.new.cn/auth/test
100 clients, running 60 sec.

Speed=32749 pages/min, 273873 bytes/sec.
Requests: 32191 susceed, 558 failed.


[root@www ~]#  webbench -c 1000 -t 60 "http://www.new.cn/auth/test"
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.new.cn/auth/test
1000 clients, running 60 sec.

Speed=46504 pages/min, 280990 bytes/sec.
Requests: 34301 susceed, 12203 failed.

 

mysql长连接