Mysql高效能之Memcached(1),mysqlmemcached
本文將介紹Memcached的安裝與使用
What is Memcached?
Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
Why Use Memcached?
Benefits of using memcached include:
• Because all information is stored in RAM, the access speed is faster than loading the information
each time from disk.
• Because the “value” portion of the key-value pair does not have any data type restrictions, you can
cache data such as complex structures, documents, images, or a mixture of such things.
• If you use the in-memory cache to hold transient information, or as a read-only cache for information
also stored in a database, the failure of any memcached server is not critical. For persistent data, you
can fall back to an alternative lookup method using database queries, and reload the data into RAM
on a different server.
The typical usage environment is to modify your application so that information is read from the cache
provided by memcached. If the information is not in memcached, then the data is loaded from the
MySQL database and written into the cache so that future requests for the same object benefit from the
cached data.
Case:
Fotolog, as they themselves point out, is probably the largest site nobody has ever heard of, pulling in more page views than even Flickr. Fotolog has 51 instances of memcached on 21 servers with 175G in use and 254G available. As a large successful photo-blogging site they have very demanding performance and scaling requirements. To meet those requirements they've developed a sophisticated approach to using memcached that others can learn from and emulate.
Memcached的下載:
http://www.memcached.org/files/memcached-1.4.21.tar.gz
在RedHat中,系統內建Memcached,可以使用yum進行安裝:
yum install memcached
也可以下載包進行安裝,這裡就詳細說明了。
基本使用:
[root@ogg1 bin]# memcached -hmemcached 1.4.4-p <num> TCP port number to listen on (default: 11211)-U <num> UDP port number to listen on (default: 11211, 0 is off)-s <file> UNIX socket path to listen on (disables network support)-a <mask> access mask for UNIX socket, in octal (default: 0700)-l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)-d run as a daemon-r maximize core file limit-u <username> assume identity of <username> (only when run as root)-m <num> max memory to use for items in megabytes (default: 64 MB)-M return error on memory exhausted (rather than removing items)-c <num> max simultaneous connections (default: 1024)-k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user; under sh this is done with 'ulimit -S -l NUM_KB').-v verbose (print errors/warnings while in event loop)-vv very verbose (also print client commands/reponses)-vvv extremely verbose (also print internal state transitions)-h print this help and exit-i print memcached and libevent license-P <file> save PID in <file>, only used with -d option-f <factor> chunk size growth factor (default: 1.25)-n <bytes> minimum space allocated for key+value+flags (default: 48)-L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk.-D <char> Use <char> as the delimiter between key prefixes and IDs. This is used for per-prefix stats reporting. The default is ":" (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the "stats detail on" command to the server.-t <num> number of threads to use (default: 4)-R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20)-C Disable use of CAS-b Set the backlog queue limit (default: 1024)-B Binding protocol - one of ascii, binary, or auto (default)-I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m)
初始化Memcached:
memcached -u root -d -m 512 -p 11211 -l 192.168.56.12[root@ogg1 bin]# ps -ef |grep memroot 4382 1 0 02:01 ? 00:00:00 memcached -u root -d -m 512 -p 11211 -l 192.168.56.12
查看當前Memcached的狀態:
[root@ogg1 bin]# telnet 192.168.56.12 11211 Trying 192.168.56.12...Connected to 192.168.56.12.Escape character is '^]'.statsSTAT pid 4382STAT uptime 7288STAT time 1418893354STAT version 1.4.4STAT pointer_size 64STAT rusage_user 0.353946STAT rusage_system 0.379942STAT curr_connections 5STAT total_connections 8STAT connection_structures 6STAT cmd_get 0STAT cmd_set 0STAT cmd_flush 0STAT get_hits 0STAT get_misses 0STAT delete_misses 0STAT delete_hits 0STAT incr_misses 0STAT incr_hits 0STAT decr_misses 0STAT decr_hits 0STAT cas_misses 0STAT cas_hits 0STAT cas_badval 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 144STAT bytes_written 1732STAT limit_maxbytes 536870912STAT accepting_conns 1STAT listen_disabled_num 0STAT threads 4STAT conn_yields 0STAT bytes 0STAT curr_items 0STAT total_items 0STAT evictions 0END對應參數解釋:pid memcache伺服器的進程IDuptime 伺服器已經啟動並執行秒數time 伺服器當前的unix時間戳記version memcache版本pointer_size 當前作業系統的指標大小(32位系統一般是32bit)rusage_user 進程的累計使用者時間rusage_system 進程的累計系統時間curr_items 伺服器當前儲存的items數量total_items 從伺服器啟動以後儲存的items總數量bytes 當前伺服器儲存items佔用的位元組數curr_connections 當前開啟著的串連數total_connections 從伺服器啟動以後曾經開啟過的串連數connection_structures 伺服器分配的串連構造數cmd_get get命令(擷取)總請求次數cmd_set set命令(儲存)總請求次數get_hits 總叫用次數get_misses 總未叫用次數evictions 為擷取空閑記憶體而刪除的items數(分配給memcache的空間用滿後需要刪除舊的items來得到空間分配給新的items)bytes_read 總讀取位元組數(請求位元組數)bytes_written 總發送位元組數(結果位元組數)limit_maxbytes 分配給memcache的記憶體大小(位元組)threads 當前線程數