단독서버에서 웹서버를 돌릴경우, memcached를 unix socket으로 실행하면
tcp나 udp를 이용하는것 보다는 성능향상을 기대할수 있다.
단, unix socket과 tcp/udp는 동시에 실행할 수는 없다.
client로 php를 연동하는것을 기준으로 한다.
OS: centos 64bit
webserver: nginx
cgi: php-fpm
yum으로 설치된 환경임
root@mail~]# yum install memcached
먼저 memcached 의 설정파일을 수정한다.
vi /etc/sysconfig/memcached
==========================================================
PORT="11211"
USER="웹서버실행유저(nginx)"
MAXCONN="1024"
CACHESIZE="1024"
OPTIONS=" -s /var/run/memcached/local.sock -a 0777"
root@mail~]# memcached -h
memcached 1.4.5
...
-l <addr> 바인드할 주소
-p <num> TCP 포트 번호 (기본값: 11211)
-U <num> UDP 포트 번호 (기본값: 11211, 0 인경우 사용안함)
-s <file> UNIX 소켓 경로 (네트워크 지원 안함)
-d 데몬으로 실행
-u <username> 전환할 사용자 이름(루트로 실행시)
-m <num> 최대 메모리(MB단위, 기본값: 64)
-M 데이터 저장시 메모리가 부족할 경우 오류를 반환(기본값은 오래된 데이터를 삭제)
-c <num> 최대 접속 개수 (기본값: 1024)
-P <file> PID 파일 저장 위치. -d 옵션 사용시 사용
-f <factor> 증가 팩터값. (기본값: 1.25)
-n <bytes> 키+값+플래그를 저장할 최소 단위(기본값: 48)
-L large memory pages 사용(가능한경우)
-t <num> 사용할 쓰레드 개수 (기본값: 4)
-v 로그 보임
-vv 자세한 로그 보임
-vvv 매우 자세한 로그 보임
-h 이도움말 표시후 나가기
-i memcached와 libevent 라이센스 표시
-P <file> -d 옵션과 함께사용할때만, <file>로 PID 저장
root@mail~]# service memcached start
php 세션 핸들러로 memcache를 사용하기
php 5.2.5 이하 버전에서는 버그가 있으므로 unix socket 으로 세션핸들러를 사용할수 없으므로 주의
root@mail~]# yum install php-pecl-memcache
/etc/php.d/memcache.ini 를 아래 내용으로 참고 하여 수정한다.
=============================================================
; ----- Enable memcache extension module
extension=memcache.so
; ----- Options for the memcache module
; Whether to transparently failover to other servers on errors
;memcache.allow_failover=1
; Data will be transferred in chunks of this size
;memcache.chunk_size=32768
; Autocompress large data
;memcache.compress_threshold=20000
; The default TCP port number to use when connecting to the memcached server
;memcache.default_port=11211
; Hash function {crc32, fnv}
;memcache.hash_function=crc32
; Hash strategy {standard, consistent}
;memcache.hash_strategy=consistent
; Defines how many servers to try when setting and getting data.
;memcache.max_failover_attempts=20
; The protocol {ascii, binary} : You need a memcached >= 1.3.0 to use the binary protocol
; protocol을 변경하지 말것
;memcache.protocol=ascii
; Redundancy : When enabled the client sends requests to N servers in parallel
;memcache.redundancy=1
;memcache.session_redundancy=2
; Lock Timeout
;memcache.lock_timeout = 15
; ----- Options to use the memcache session handler
; Use memcache as a session handler
session.save_handler=memcache
; Defines a comma separated of server urls to use for session storage
; 단독서버일 경우 효율적인 자원사용을 위해 unix소켓 모드로 memcached를 띄웠다.
;session.save_path="tcp://localhost:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
session.save_path="unix:///tmp/memcached.sock?persistent=1&weight=1&timeout=1&retry_interval=15"
session.serialize_handler=igbinary
root@mail~]# service php-fpm restart
; Enable memcached extension module
extension=memcached.so
; ----- Options to use the memcached session handler
; RPM note : save_handler and save_path are defined
; for mod_php, in /etc/httpd/conf.d/php.conf
; for php-fpm, in /etc/php-fpm.d/*conf
; Use memcache as a session handler
session.save_handler=memcached
; Defines a comma separated list of server urls to use for session storage
session.save_path="/var/run/memcached/local.sock"
; Use session locking
; valid values: On, Off
; the default is On
memcached.sess_locking = On
; Session spin lock retry wait time in microseconds.
; Be carefull when setting this value.
; Valid values are integers, where 0 is interpreted as
; the default value. Negative values result in a reduces
; locking to a try lock.
; the default is 150000
memcached.sess_lock_wait = 150000
; memcached session key prefix
; valid values are strings less than 219 bytes long
; the default value is "memc.sess.key."
memcached.sess_prefix = "memc.sess.key."
; memcached session consistent hash mode
; if set to On, consistent hashing (libketama) is used
; for session handling.
; When consistent hashing is used, one can add or remove cache
; node(s) without messing up too much with existing keys
; default is Off
memcached.sess_consistent_hash = Off
; Allow failed memcached server to automatically be removed
memcached.sess_remove_failed = 1
; Write data to a number of additional memcached servers
; This is "poor man's HA" as libmemcached calls it.
; If this value is positive and sess_remove_failed is enabled
; when a memcached server fails the session will continue to be available
; from a replica. However, if the failed memcache server
; becomes available again it will read the session from there
; which could have old data or no data at all
memcached.sess_num_replicas = 0;
; memcached session binary mode
; libmemcached replicas only work if binary mode is enabled
memcached.sess_binary = Off
; memcached session number of replicas
memcached.sess_number_of_replicas = 0
; memcached session binary mode
; libmemcached replicas only work if binary mode is enabled
memcached.sess_binary = Off
; memcached session number of replicas
memcached.sess_number_of_replicas = 0
; memcached session replica read randomize
memcached.sess_randomize_replica_read = Off
; Set the compression type
; valid values are: fastlz, zlib
; the default is fastlz
memcached.compression_type = "fastlz"
; Compression factor
; Store compressed value only if the compression
; factor (saving) exceeds the set limit.
;
; store compressed if:
; plain_len > comp_len * factor
;
; the default value is 1.3 (23% space saving)
memcached.compression_factor = "1.3"
; The compression threshold
;
; Do not compress serialized values below this threshold.
; the default is 2000 bytes
memcached.compression_threshold = 2000
; Set the default serializer for new memcached objects.
; valid values are: php, igbinary, json, json_array
;
; json - standard php JSON encoding. This serializer
; is fast and compact but only works on UTF-8
; encoded data and does not fully implement
; serializing. See the JSON extension.
; json_array - as json, but decodes into arrays
; php - the standard php serializer
; igbinary - a binary serializer
;
; The default is igbinary if available and php otherwise.
memcached.serializer = "igbinary"