一台vps服务器,使用centos 5操作操作系统,销售商声称是“上海戴尔VM/HV(IP)512M内存20G硬盘”,基于xen虚拟化技术,算是比中低端的vps了,上面跑一个ecshop的商城系统。
主要运行的服务为
apache (httpd-2.2.3-53.el5.centos.3/php-5.1.6-27.el5_7.5)
mysqld (mysql-server-5.0.95-1.el5_7.1)
ecshop商城上线后,访问量还并不很大,但web经常卡死,几乎访问不动。ssh登录服务器后,内存用尽,连swap都使用过半。top显示有几十个httpd进程;mysql占用cpu持续超过50%,经常达80%甚至100%, 这个状况,不死就怪了。
起初以为是apache子进程太多了,修改apache配置,只开8个子进程;同时还注意到有个yum 进程在运行,于是停掉apache,mysql,运行yum update ,升级一下系统。当时是周五下午,搞好后看起来比较正常了,
然而到了周一,问题依旧,apache进程数多达100多个!
网上有人说apache进程数过多,很可能不是apache的问题,而是web应用的问题,导致一些子进程假死或死掉。通过phpmyadmin查看mysql的进程,很多类似如下的语句:
SELECT a.attr_name
FROM `ecs_attribute` AS a, `ecs_goods_attr` AS ga, `ecs_goods` AS g
WHERE (
g.cat_id
IN ('105', '171', '173', '106', '111', '108', '113', '151', '112', '157', '153', '152', '110', '150', '147', '155', '148', '156', '172', '109', '149', '114')
OR g.goods_id
IN ('')
)
AND a.attr_id = ga.attr_id
AND g.goods_id = ga.goods_id
AND g.is_delete =0
AND g.is_on_sale =1
AND g.is_alone_sale =1
AND a.attr_id = '219'
而且查询执行时间多数比较长,经常出现多个耗时达5秒以上的语句在执行。状态经常是 Copying to tmp table,或sending data. 怀疑mysql问题比较大,开始关注mysql配置,通过phpmyadmin查看mysql变量,与一台运行平稳的实体服务器比对,主要看是缓存、临时表等变量。果然,这些值都相当的小。
查看mysql配置文件如下:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
参看windows版mysql官方自带的配置文件(\MySQL\MySQL Server 5.0\my-large.ini),修改配置文件如下:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
myisam_sort_buffer_size=64M
key_buffer = 64M
table_cache = 128
read_buffer_size = 1M
read_rnd_buffer_size = 1M
sort_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
重启mysql,apache,再通过phpmyadmin看查询进程,比之前大为改观。通过几个小时的观查,已经较少出现查询耗时超过5秒的进程。基本上每2、3秒重新一下一下phpmyadmin的“进程”,有一半以上机会看不到正在执行的语句。也就是说,大多数语句都可以很快执行完,以致于较很少有机会看到它们。这个现象与那台参考的实体服务器上的mysql相同。
通过free查看内存使用情况:
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 503 469 33 0 25 305
-/+ buffers/cache: 138 364
Swap: 1023 23 1000
swap几乎没有使用,物理内存(除去缓存)占用并也不高。
通过top查看进程:
top - 13:19:21 up 165 days, 3:11, 2 users, load average: 0.47, 0.36, 0.52
Tasks: 101 total, 1 running, 98 sleeping, 2 stopped, 0 zombie
Cpu(s): 20.3%us, 12.5%sy, 0.0%ni, 62.1%id, 0.0%wa, 1.0%hi, 4.1%si, 0.0%st
Mem: 515268k total, 504416k used, 10852k free, 26712k buffers
Swap: 1048568k total, 23712k used, 1024856k free, 314496k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6848 mysql 23 0 225m 56m 4432 S 32.1 11.3 33:48.61 mysqld
7399 apache 15 0 33516 13m 4384 S 0.0 2.7 0:09.48 httpd
7401 apache 15 0 33328 13m 3884 S 2.6 2.6 0:08.00 httpd
7406 apache 15 0 32864 12m 3848 S 0.0 2.5 0:07.92 httpd
7429 apache 15 0 32704 12m 3528 S 0.0 2.4 0:00.57 httpd
7400 apache 15 0 32672 12m 3740 S 5.6 2.4 0:09.77 httpd
7428 apache 15 0 32684 12m 3544 S 0.0 2.4 0:01.34 httpd
7404 apache 15 0 32348 12m 3736 S 0.0 2.4 0:08.98 httpd
7407 apache 15 0 32100 12m 3884 S 0.0 2.4 0:08.02 httpd
7411 apache 15 0 32436 12m 3848 S 0.0 2.4 0:06.43 httpd
7405 apache 15 0 32184 11m 3740 S 0.0 2.4 0:09.60 httpd
7408 apache 15 0 32432 11m 3588 S 0.0 2.4 0:07.21 httpd
7427 apache 15 0 32368 11m 3540 S 0.0 2.4 0:00.98 httpd
6874 root 18 0 27512 9448 5672 S 0.0 1.8 0:00.40 httpd
1765 root 34 19 27884 3200 1148 S 0.0 0.6 9:33.82 yum-updatesd
7112 root 18 0 10216 2980 2372 S 0.0 0.6 0:00.20 sshd
7024 root 15 0 7620 2664 1952 T 0.0 0.5 0:00.10 vim
7114 root 15 0 6656 1608 1188 S 0.0 0.3 0:00.05 sftp-server
12180 root 15 0 25424 1188 804 S 0.0 0.2 0:34.97 snmpd
5411 root 15 0 8520 1124 1120 T 0.0 0.2 0:00.01 mysql
5085 root 15 0 2688 1028 800 S 0.0 0.2 0:00.20 bash
6801 root 22 0 2504 1000 856 S 0.0 0.2 0:00.00 mysqld_safe
7432 root 18 0 2292 1000 792 R 0.3 0.2 0:00.24 top
1429 dbus 15 0 2964 792 544 S 0.0 0.2 2:55.54 dbus-daemon
5077 root 15 0 10220 792 668 S 0.3 0.2 0:04.47 sshd
12307 root 15 0 9348 576 408 S 0.0 0.1 0:05.80 sendmail
12197 root 20 0 13564 572 412 S 0.0 0.1 0:04.28 snmptrapd
...
mysql的内存占用也不高,httpd进程数目也正常。
也就是说,改后的配置参数,整个vps服务器运行状态还是比较正常。
只是因为vps本身性能并不高,而访问量较多时候load average值达5以上;不过即便这时,网站还能访问;之前假死时,页面超过半分钟都打不开的。