我在服务器上看到了错误信息:
$ sudo tail /var/log/php-fpm/5.5/www-error.log
PHP Warning: session_start(): Unable to clear session lock record
当我的服务器存储会话时,它使用了ElastiCache上的memcached(AWS)。
以下是我的环境:
*Memcached(ElastiCache)
-version 1.4.14
-1 Node
-cache.t2.micro
$ nginx -v
nginx version: nginx/1.6.2
# use unix domain socket
(fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;)
$ php-fpm -v
PHP 5.5.24 (fpm-fcgi) (built: Apr 17 2015 18:04:41)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
$ pecl list
Installed packages, channel pecl.php.net:
=========================================
Package Version State
...
memcached 2.2.0 stable
...
这里有数据:
$ cat /etc/php-5.5.ini
...
...
; http://php.net/session.save-handler
; session.save_handler = files
session.hash_bits_per_character = 5
session.hash_function = 0
session.use_trans_sid = 0
session.cache_expire = 180
session.cache_limiter = nocache
session.referer_check =
session.bug_compat_warn = Off
session.bug_compat_42 = Off
session.gc_maxlifetime = 1440
session.gc_divisor = 1000
session.gc_probability = 1
session.serialize_handler = php
session.cookie_httponly =
session.cookie_path = /
session.cookie_lifetime = 0
session.auto_start = 0
session.name = PHPSESSID
session.use_only_cookies = 1
session.use_cookies = 1
...
...
$ cat /etc/php-5.5.d/memcached.ini
extension=memcached.so
session.save_handler=memcached
session.save_path="*******.cache.amazonaws.com:11211"
memcached.sess_locking = On
memcached.sess_lock_wait = 150000
memcached.sess_lock_max_wait = 0;
memcached.sess_lock_expire = 0;
memcached.sess_prefix = "memc.sess.key."
memcached.sess_consistent_hash = Off
memcached.sess_remove_failed = 0
memcached.sess_number_of_replicas = 0
memcached.sess_binary = Off
memcached.sess_randomize_replica_read = Off
memcached.sess_connect_timeout = 1000
memcached.sess_sasl_username = NULL
memcached.sess_sasl_password = NULL
memcached.compression_type = "fastlz"
memcached.compression_factor = "1.3"
memcached.compression_threshold = 2000
memcached.serializer = "igbinary"
memcached.use_sasl = Off
memcached.store_retry_count = 2
$ php -i | grep memcached
/etc/php-5.5.d/memcached.ini,
memcached
memcached support => enabled
libmemcached version => 1.0.8
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => igbinary => igbinary
memcached.sess_binary => no value => no value
memcached.sess_connect_timeout => 1000 => 1000
memcached.sess_consistent_hash => no value => no value
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => 0 => 0
memcached.sess_lock_wait => 150000 => 150000
memcached.sess_locking => no value => no value
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => no value => no value
memcached.sess_remove_failed => 0 => 0
memcached.sess_sasl_password => no value => no value
memcached.sess_sasl_username => no value => no value
memcached.store_retry_count => 2 => 2
memcached.use_sasl => no value => no value
Registered save handlers => files user memcached
session.save_handler => memcached => memcached
$ yum list installed | grep memcached
libmemcached.x86_64 1.0.8-2.6.amzn1 @amzn-main
memcached.x86_64 1.4.15-9.12.amzn1 @amzn-main
php55-pecl-memcached.x86_64 2.2.0-5.14.amzn1 @amzn-updates
$ cat /etc/php-fpm-5.5.d/www.conf
php_value[session.save_handler] = memcached
php_value[session.save_path] = "*******.cache.amazonaws.com:11211"
$ memcached-tool *******.cache.amazonaws.com:11211 display
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
1 96B 0s 1 0 yes 0 0 0
2 120B 55039s 1 4 yes 0 0 0
4 192B 1439s 2 2226 yes 0 0 0
5 240B 0s 1 0 yes 0 0 0
6 304B 14477s 1 1 yes 0 0 0
7 384B 9557s 1 1 yes 0 0 0
8 480B 268s 1 1 yes 0 0 0
9 600B 2014s 1 1 yes 0 0 0
$ memcached-tool *******.cache.amazonaws.com:11211 stats
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 470256
bytes_read 5006739226
bytes_written 2668634300
cas_badval 0
cas_hits 0
cas_misses 0
cmd_config_get 489750
cmd_config_set 1
cmd_flush 1
cmd_get 15601270
cmd_set 33168188
cmd_touch 0
conn_yields 0
connection_structures 260
curr_config 1
curr_connections 9
curr_items 2581
decr_hits 0
decr_misses 0
delete_hits 15438339
delete_misses 10047
evicted_unfetched 0
evictions 0
expired_unfetched 7474894
get_hits 8113028
get_misses 7488242
hash_bytes 524288
hash_is_expanding 0
hash_power_level 16
incr_hits 0
incr_misses 0
libevent 1.4.13-stable
limit_maxbytes 477102080
listen_disabled_num 0
pid 1
pointer_size 64
reclaimed 7476127
reserved_fds 5
rusage_system 1539.028000
rusage_user 838.464000
threads 1
time 1436772530
total_connections 15600239
total_items 31043112
touch_hits 0
touch_misses 0
uptime 7346275
version 1.4.14
奇怪的是,这个错误每周大约发生一次, 但昨天它发生了大约2000次。 当这个错误发生时,服务器也会处于繁忙状态。 随着时间的推移,它会消失。
■Cloud Watch 日志:
![cloud_watch](https://istack.dev59.com/h3Bzg.webp)
![new_relic](https://istack.dev59.com/fQA4O.webp)
更新于2020/10/8
很长时间以后,我解决了这个问题:
当用户访问过多时,似乎 DNS 无法在 AWS EC2 上解析。
这里有修复补丁。 https://aws.amazon.com/jp/premiumsupport/knowledge-center/dns-resolution-failures-ec2-linux/
$ wget https://raw.githubusercontent.com/awslabs/aws-support-tools/master/EC2/AutomateDnsmasq/AutomateDnsmasq.sh
$ chmod +x AutomateDnsmasq.sh
$ sudo ./AutomateDnsmasq.sh
-> Finally, you must restart php server.