疑难杂症....百思不得其解~~
三台服务器 : 1测试 1web 1数据库
全内网,相互访问速度10.7m/s
web: nginx + php
mysql : version - 5.6.3-m6-log
测试程序 webbench
问题
------------------------------
webbench 并发超过1000 访问 web上的html静态页(或纯php不带db操作页) ,此时一切正常,在我笔记本上手动刷新html和不带db操作的php页,都很正常。但只要刷新带DB操作的php页面就一直在loading直至timeout could not connect db server出现。此时我去看db sever的负载和mysql的连接数全部很低,甚至mysql的连接数为1,这个1是我在笔记本上用软件连上数据库。
在mysql上使用show processlist 看连接状况。凡是loading中的连接都处于 writing to net | connect | unauthenticated ,一直都是这样直至连接关闭。我想这说明php的连接连mysql没有成功。
以为是php的问题,后来在机器 上放了个cgi的程序来连。。并发到1000 .也是一样。html和纯php页可以打开。一做数据库操作就连不上...
直到我将并发降到700后,才能正常访问带db操作的页面,延迟1~2秒(用 webbench直接并发测带db操作的页面,大约也是700个并发后,我刷新笔记本上的db操作页面也会连接不上db)
我的问题很奇怪。道理说我1000并发html静态的时候php应该可以连得上数据库.可为什么 连不上呢。。
首先三台机器的内核参数均调整过如下(最大打开文件数已经修改过):
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog = 4096
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 32768 65000
net.ipv4.tcp_keepalive_time = 12000
net.ipv4.tcp_max_tw_buckets = 180000
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem = 32768 436600 873200
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_fin_timeout = 5
web nginx.conf如下
------------------------------------------------------
worker_processes 4;
worker_rlimit_nofile 65536;
worker_cpu_affinity 0001 0010 0100 1000;
events {
use epoll;
worker_connections 51200 ;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
client_max_body_size 1024m;
sendfile on;
keepalive_timeout 60;
gzip on;
server {
listen 80;
server_name localhost;
location / {
root /appdata/web;
index index.html index.php;
}
location ~ \.html$ {
root /appdata/web;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /appdata/web;
}
location ~ \.php$ {
root /appdata/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
}
}
php-fpm.conf如下:
----------------------------------------
pm.max_children = 200
pm = dynamic
listen = 127.0.0.1:9000
pm.max_spare_servers = 40
pm.start_servers =35
pm.min_spare_servers = 20
rlimit_files = 65536
my.cnf 如下
----------------
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 100M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
max_user_connections = 0
back_log = 1000
#skip-grant-tables
max_connections=2000
log-bin=mysql-bin
server-id = 1
interactive_timeout=20
wait_timeout=20
#skip-name-resolve
connect_timeout=20
[mysqldump]
##quick
##max_allowed_packet = 16M
[mysql]
##no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
##key_buffer_size = 256M
##sort_buffer_size = 256M
##read_buffer = 2M
##write_buffer = 2M
[mysqlhotcopy]
##interactive-timeout
[解决办法]
应该查看下1000个并发访问的时候,服务器资源是不是被占用了。
没有剩余的资源去连接DB。
也就是你要去top他的内存是不是已经达到分配的内存。
[解决办法]
个人觉得是这样子:
1、你查看你的mysql的最大连接数是多少;
2、并发1000会不会瞬间堵塞3306端口,处理不过来影响;
3、是不是并发1000时候看看你的web服务器的资源情况,例如:IO、CPU、网卡吞吐量、内存等。
4、建议:
a、修改一下mysql的最大连接数,好像3000;
b、建议异步执行db或者排队方式db;
c、分析web服务器的情况,在针对性处理。