CentOS Ï Redis 2.2 °²×°ÅäÖÃÏê½â
Ò». ¹ØÓÚ Redis
Redis
ÊÇÒ»Öָ߼¶ key-value Êý¾Ý¿â¡£Ëü¸ú memcached
ÀàËÆ£¬²»¹ýÊý¾Ý¿ÉÒԳ־û¯£¬¶øÇÒÖ§³ÖµÄÊý¾ÝÀàÐͺܷḻ¡£ËüÔÚ±£³Ö¼üÖµÊý¾Ý¿â¼òµ¥¿ì½ÝÌصãµÄͬʱ£¬ÓÖÎüÊÕÁ˲¿·Ö¹ØϵÊý¾Ý¿âµÄÓŵ㡣´Ó¶øʹËüµÄλÖô¦ÓÚ¹ØϵÊý
¾Ý¿âºÍ¼üÖµÊý¾Ý¿âÖ®¼ä¡£Redis²»½öÄܱ£´æStringsÀàÐ͵ÄÊý¾Ý£¬»¹Äܱ£´æListsÀàÐÍ£¨ÓÐÐò£©ºÍSetsÀàÐÍ£¨ÎÞÐò£©µÄÊý¾Ý£¬¶øÇÒ»¹ÄÜÍê³ÉÅÅÐò
£¨SORT£©µÈ¸ß¼¶¹¦ÄÜ£¬ÔÚʵÏÖINCR£¬SETNXµÈ¹¦ÄܵÄʱºò£¬±£Ö¤ÁËÆä²Ù×÷µÄÔ×ÓÐÔ£¬³ý´ËÒÔÍ⣬»¹Ö§³ÖÖ÷´Ó¸´ÖƵȹ¦ÄÜ¡£Redis¿ÉÒÔ±»¿´³ÉÊÇÒ»¸ö
Êý¾Ý½á¹¹·þÎñÆ÷¡£
RedisµÄËùÓÐÊý¾Ý¶¼ÊDZ£´æÔÚÄÚ´æÖУ¬È»ºó²»¶¨ÆÚµÄͨ¹ýÒì²½·½Ê½±£´æµ½´ÅÅÌÉÏ(Õâ³ÆΪ¡°°ë³Ö¾Ã»¯Ä£Ê½¡±)£»Ò²¿ÉÒÔ°Ñÿһ´ÎÊý¾Ý±ä»¯¶¼Ð´Èëµ½Ò»¸öappend only file(aof)ÀïÃæ(Õâ³ÆΪ¡°È«³Ö¾Ã»¯Ä£Ê½¡±)¡£
¹Ù·½ÍøÕ¾£º http://redis.io/
PPT½éÉÜ£º http://simonwillison.net/static/2010/redis-tutorial/
¶þ. °²×° Redis
RedisµÄ´úÂë×ñÑANSI-C±àд£¬¿ÉÒÔÔÚËùÓÐPOSIXϵͳ£¨ÈçLinux, *BSD, Mac OS X, SolarisµÈ£©ÉÏ°²×°ÔËÐС£¶øÇÒRedis²¢²»ÒÀÀµÈκηDZê×¼¿â£¬Ò²Ã»ÓбàÒë²ÎÊý±ØÐèÌí¼Ó¡£±àÒë°²×°Redis£¬ÎÒÃÇΨһÐèÒªµÄ¾ÍÊÇmake¡£
1. ÏÂÔØredis×îа汾2.2.12
cd /usr/local/src
wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz
2. ±àÒë°²×°redis
tar zxvf redis-2.2.12.tar.gz
cd redis-2.2.12
make
makeÃüÁîÖ´ÐÐÍê³Éºó£¬»áÔÚµ±Ç°Ä¿Â¼ÏÂÉú³É4¸ö¿ÉÖ´ÐÐÎļþ£¬·Ö±ðÊÇredis-server¡¢redis-cli¡¢redis-benchmark¡¢redis-stat£¬ËüÃǵÄ×÷ÓÃÈçÏ£º
redis-server£ºRedis·þÎñÆ÷µÄdaemonÆô¶¯³ÌÐò
redis-cli£ºRedisÃüÁîÐвÙ×÷¹¤¾ß¡£µ±È»£¬ÄãÒ²¿ÉÒÔÓÃtelnet¸ù¾ÝÆä´¿Îı¾ÐÒéÀ´²Ù×÷
redis-benchmark£ºRedisÐÔÄܲâÊÔ¹¤¾ß£¬²âÊÔRedisÔÚÄãµÄϵͳ¼°ÄãµÄÅäÖÃϵĶÁдÐÔÄÜ
redis-stat£ºRedis״̬¼ì²â¹¤¾ß£¬¿ÉÒÔ¼ì²âRedisµ±Ç°×´Ì¬²ÎÊý¼°ÑÓ³Ù×´¿ö
3. ½¨Á¢RedisĿ¼£¨½¨Ò飬·Ç±ØÐ裩
Ö´ÐÐÒÔÏÂÃüÁÁ¢Ïà¹ØĿ¼²¢¿½±´Ïà¹ØÎļþÖÁĿ¼ÖУº
sudo -s
mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/etc
mkdir -p /usr/local/redis/var
cp redis-server redis-cli redis-benchmark redis-stat /usr/local/redis/bin/
cp redis.conf /usr/local/redis/etc/
½¨Á¢RedisĿ¼£¬Ö»ÊÇΪÁ˽«RedisÏà¹ØµÄ×ÊÔ´¸üºÃµÄͳһ¹ÜÀí¡£ÄãÒ²¿ÉÒÔʹÓÃ
make install
°²×°ÔÚϵͳĬÈÏĿ¼
Èý. ÅäÖÃ Redis
³É ¹¦°²×°Redisºó£¬Ö±½ÓÖ´ÐÐredis-server¼´¿ÉÔËÐÐRedis£¬´ËʱËüÊÇ°´ÕÕĬÈÏÅäÖÃÀ´ÔËÐеģ¨Ä¬ÈÏÅäÖò»ÊǺǫ́ÔËÐУ©¡£Èç¹ûÎÒÃÇÏ£Íû Redis°´ÎÒÃǵÄÒªÇóÔËÐУ¬ÔòÐèÒªÐÞ¸ÄÅäÖÃÎļþ£¬RedisµÄÅäÖÃÎļþ¾ÍÊÇÉÏÃæµÚ¶þ¸öcp²Ù×÷µÄredis.confÎļþ£¬Ä¿Ç°Ëü±»ÎÒÃÇ¿½±´µ½ÁË /usr/local/redis/etc/Ŀ¼Ï¡£ÐÞ¸ÄËü¾Í¿ÉÒÔÅäÖÃredis serverÁË¡£
1. redis.conf ÅäÖòÎÊý£º
#ÊÇ·ñ×÷ΪÊØ»¤½ø³ÌÔËÐÐ
daemonize yes
#ÈçÒÔºǫ́½ø³ÌÔËÐУ¬ÔòÐèÖ¸¶¨Ò»¸öpid£¬Ä¬ÈÏΪ/var/run/redis.pid
pidfile redis.pid
#°ó¶¨Ö÷»úIP£¬Ä¬ÈÏֵΪ127.0.0.1
#bind 127.0.0.1
#RedisĬÈϼàÌý¶Ë¿Ú
port 6379
#¿Í»§¶ËÏÐÖöàÉÙÃëºó£¬¶Ï¿ªÁ¬½Ó£¬Ä¬ÈÏΪ300£¨Ã룩
timeout 300
#ÈÕÖ¾¼Ç¼µÈ¼¶£¬ÓÐ4¸ö¿ÉÑ¡Öµ£¬debug£¬verbose£¨Ä¬ÈÏÖµ£©£¬notice£¬warning
loglevel verbose
#Ö¸¶¨ÈÕÖ¾Êä³öµÄÎļþÃû£¬Ä¬ÈÏֵΪstdout£¬Ò²¿ÉÉèΪ/dev/nullÆÁ±ÎÈÕÖ¾
logfile stdout
#¿ÉÓÃÊý¾Ý¿âÊý£¬Ä¬ÈÏֵΪ16£¬Ä¬ÈÏÊý¾Ý¿âΪ0
databases 16
#±£´æÊý¾Ýµ½diskµÄ²ßÂÔ
#µ±ÓÐÒ»ÌõKeysÊý¾Ý±»¸Ä±äÊÇ£¬900Ãëˢе½diskÒ»´Î
save 900 1
#µ±ÓÐ10ÌõKeysÊý¾Ý±»¸Ä±äʱ£¬300Ãëˢе½diskÒ»´Î
save 300 10
#µ±ÓÐ1wÌõkeysÊý¾Ý±»¸Ä±äʱ£¬60Ãëˢе½diskÒ»´Î
save 60 10000
#µ±dump .rdbÊý¾Ý¿âµÄʱºòÊÇ·ñѹËõÊý¾Ý¶ÔÏó
rdbcompression yes
#±¾µØÊý¾Ý¿âÎļþÃû£¬Ä¬ÈÏֵΪdump.rdb
dbfilename dump.rdb
#±¾µØÊý¾Ý¿â´æ·Å·¾¶£¬Ä¬ÈÏֵΪ ./
dir /usr/local/redis/var/
########### Replication #####################
#RedisµÄ¸´ÖÆÅäÖÃ
# slaveof <masterip> <masterport> µ±±¾»úΪ´Ó·þÎñʱ£¬ÉèÖÃÖ÷·þÎñµÄIP¼°¶Ë¿Ú
# masterauth <master-password> µ±±¾»úΪ´Ó·þÎñʱ£¬ÉèÖÃÖ÷·þÎñµÄÁ¬½ÓÃÜÂë
#Á¬½ÓÃÜÂë
# requirepass foobared
#×î´ó¿Í»§¶ËÁ¬½ÓÊý£¬Ä¬Èϲ»ÏÞÖÆ
# maxclients 128
#×î´óÄÚ´æʹÓÃÉèÖ㬴ﵽ×î´óÄÚ´æÉèÖúó£¬Redis»áÏȳ¢ÊÔÇå³ýÒѵ½ÆÚ»ò¼´½«µ½ÆÚµÄKey£¬µ±´Ë·½·¨´¦Àíºó£¬Èε½´ï×î´óÄÚ´æÉèÖ㬽«ÎÞ·¨ÔÙ½øÐÐдÈë²Ù×÷¡£
# maxmemory <bytes>
#ÊÇ·ñÔÚÿ´Î¸üвÙ×÷ºó½øÐÐÈÕÖ¾¼Ç¼£¬Èç¹û²»¿ªÆô£¬¿ÉÄÜ»áÔڶϵçʱµ¼ÖÂÒ»¶Îʱ¼äÄÚµÄÊý¾Ý¶ªÊ§¡£ÒòΪredis±¾Éíͬ²½Êý¾ÝÎļþÊÇ°´ÉÏÃæsaveÌõ¼þÀ´Í¬²½µÄ£¬ËùÒÔÓеÄÊý¾Ý»áÔÚÒ»¶Îʱ¼äÄÚÖ»´æÔÚÓÚÄÚ´æÖС£Ä¬ÈÏֵΪno
appendonly no
#¸üÐÂÈÕÖ¾ÎļþÃû£¬Ä¬ÈÏֵΪappendonly.aof
#appendfilename
#¸üÐÂÈÕÖ¾Ìõ¼þ£¬¹²ÓÐ3¸ö¿ÉÑ¡Öµ¡£no±íʾµÈ²Ù×÷ϵͳ½øÐÐÊý¾Ý»º´æͬ²½µ½´ÅÅÌ£¬always±íʾÿ´Î¸üвÙ×÷ºóÊÖ¶¯µ÷ÓÃfsync()½«Êý¾Ýдµ½´ÅÅÌ£¬everysec±íʾÿÃëͬ²½Ò»´Î£¨Ä¬ÈÏÖµ£©¡£
# appendfsync always
appendfsync everysec
# appendfsync no
################ VIRTUAL MEMORY ###########
#ÊÇ·ñ¿ªÆôVM¹¦ÄÜ£¬Ä¬ÈÏֵΪno
vm-enabled no
# vm-enabled yes
#ÐéÄâÄÚ´æÎļþ·¾¶£¬Ä¬ÈÏֵΪ/tmp/redis.swap£¬²»¿É¶à¸öRedisʵÀý¹²Ïí
vm-swap-file logs/redis.swap
#
½«ËùÓдóÓÚvm-max-memoryµÄÊý¾Ý´æÈëÐéÄâÄÚ´æ,ÎÞÂÛvm-max-memoryÉèÖöàС,ËùÓÐË÷ÒýÊý¾Ý¶¼ÊÇÄÚ´æ´æ´¢µÄ
(RedisµÄË÷ÒýÊý¾Ý¾ÍÊÇkeys),Ò²¾ÍÊÇ˵,µ±vm-max-memoryÉèÖÃΪ0µÄʱºò,ÆäʵÊÇËùÓÐvalue¶¼´æÔÚÓÚ´ÅÅÌ¡£Ä¬ÈÏֵΪ0¡£
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
############# ADVANCED CONFIG ###############
glueoutputbuf yes
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
#ÊÇ·ñÖØÖÃHash±í
activerehashing yes
×¢Ò⣺Redis¹Ù·½Îĵµ¶ÔVMµÄʹÓÃÌá³öÁËһЩ½¨Òé:
** µ±ÄãµÄkeyºÜС¶øvalueºÜ´óʱ,ʹÓÃVMµÄЧ¹û»á±È½ÏºÃ.ÒòΪÕâÑù½ÚÔ¼µÄÄÚ´æ±È½Ï´ó.
** µ±ÄãµÄkey²»Ð¡Ê±,¿ÉÒÔ¿¼ÂÇʹÓÃһЩ·Ç³£·½·¨½«ºÜ´óµÄkey±ä³ÉºÜ´óµÄvalue,±ÈÈçÄã¿ÉÒÔ¿¼Âǽ«key,value×éºÏ³ÉÒ»¸öеÄvalue.
** ×îºÃʹÓÃlinux ext3 µÈ¶ÔÏ¡ÊèÎļþÖ§³Ö±È½ÏºÃµÄÎļþϵͳ±£´æÄãµÄswapÎļþ.
** vm-max-threadsÕâ¸ö²ÎÊý,¿ÉÒÔÉèÖ÷ÃÎÊswapÎļþµÄÏß³ÌÊý,ÉèÖÃ×îºÃ²»Òª³¬¹ý»úÆ÷µÄºËÊý.Èç¹ûÉèÖÃΪ0,ÄÇôËùÓжÔswapÎļþµÄ²Ù×÷¶¼ÊÇ´®ÐеÄ.¿ÉÄÜ»áÔì³É±È½Ï³¤Ê±¼äµÄÑÓ³Ù,µ«ÊǶÔÊý¾ÝÍêÕûÐÔÓкܺõı£Ö¤.
2. µ÷ÕûϵͳÄں˲ÎÊý
Èç¹ûÄÚ´æÇé¿ö±È½Ï½ôÕŵĻ°£¬ÐèÒªÉ趨Äں˲ÎÊý£º
echo 1 > /proc/sys/vm/overcommit_memory
ÕâÀï˵һÏÂÕâ¸öÅäÖõĺ¬Ò壺/proc/sys/vm/overcommit_memory
¸ÃÎļþÖ¸¶¨ÁËÄÚºËÕë¶ÔÄÚ´æ·ÖÅäµÄ²ßÂÔ£¬ÆäÖµ¿ÉÒÔÊÇ0¡¢1¡¢2¡£
0£¬±íʾÄں˽«¼ì²éÊÇ·ñÓÐ×ã¹»µÄ¿ÉÓÃÄڴ湩ӦÓýø³ÌʹÓã»Èç¹ûÓÐ×ã¹»µÄ¿ÉÓÃÄڴ棬ÄÚ´æÉêÇëÔÊÐí£»·ñÔò£¬ÄÚ´æÉêÇëʧ°Ü£¬²¢°Ñ´íÎ󷵻ظøÓ¦Óýø³Ì¡£
1£¬±íʾÄÚºËÔÊÐí·ÖÅäËùÓеÄÎïÀíÄڴ棬¶ø²»¹Üµ±Ç°µÄÄÚ´æ״̬ÈçºÎ¡£
2£¬±íʾÄÚºËÔÊÐí·ÖÅ䳬¹ýËùÓÐÎïÀíÄÚ´æºÍ½»»»¿Õ¼ä×ܺ͵ÄÄÚ´æ
Redis ÔÚdumpÊý¾ÝµÄʱºò£¬»áfork³öÒ»¸ö×Ó½ø³Ì£¬ÀíÂÛÉÏchild½ø³ÌËùÕ¼ÓõÄÄÚ´æºÍparentÊÇÒ»ÑùµÄ£¬±ÈÈçparentÕ¼ÓõÄÄÚ´æΪ8G£¬Õâ¸öʱºò ҲҪͬÑù·ÖÅä8GµÄÄÚ´æ¸øchild, Èç¹ûÄÚ´æÎÞ·¨¸ºµ££¬ÍùÍù»áÔì³Éredis·þÎñÆ÷µÄdown»ú»òÕßIO¸ºÔعý¸ß£¬Ð§ÂÊϽµ¡£ËùÒÔÕâÀï±È½ÏÓÅ»¯µÄÄÚ´æ·ÖÅä²ßÂÔÓ¦¸ÃÉèÖÃΪ 1£¨±íʾÄÚºËÔÊÐí·ÖÅäËùÓеÄÎïÀíÄڴ棬¶ø²»¹Üµ±Ç°µÄÄÚ´æ״̬ÈçºÎ£©
ËÄ. ÔËÐÐ Redis
1. ÔËÐзþÎñ
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
¼´¿ÉÔÚºǫ́Æô¶¯redis·þÎñ£¬È·ÈÏÔËÐÐÁËÖ®ºó£¬¿ÉÒÔÓÃredis-benchmarkÃüÁî²âÊÔ¿´¿´£¬»¹¿ÉÒÔͨ¹ýredis-cliÃüÁîʵ¼Ê²Ù×÷һϣ¬±ÈÈ磺
/usr/local/redis/bin/redis-cli set foo bar
OK
/usr/local/redis/bin/redis-cli get foo
bar
2. ¹Ø±Õ·þÎñ
redis-cli shutdown
Èç¹û¶Ë¿Ú±ä»¯¿ÉÒÔÖ¸¶¨¶Ë¿Ú:
redis-cli -p 6380 shutdown
3. ±£´æ/±¸·Ý
Êý¾Ý±¸·Ý¿ÉÒÔͨ¹ý¶¨ÆÚ±¸·Ý¸ÃÎļþʵÏÖ¡£
ÒòΪredisÊÇÒ첽дÈë´ÅÅ̵ģ¬Èç¹ûÒªÈÃÄÚ´æÖеÄÊý¾ÝÂíÉÏдÈëÓ²ÅÌ¿ÉÒÔÖ´ÐÐÈçÏÂÃüÁ
redis-cli save »òÕß redis-cli -p 6380 save£¨Ö¸¶¨¶Ë¿Ú£©
×¢Ò⣬ÒÔÉϲ¿Êð²Ù×÷ÐèÒª¾ß±¸Ò»¶¨µÄȨÏÞ£¬±ÈÈ縴ÖƺÍÉ趨Äں˲ÎÊýµÈ¡£
Ö´ÐÐredis-benchmarkÃüÁîʱҲ»á½«ÄÚ´æÊý¾ÝдÈëÓ²ÅÌ¡£
4. ͬ²½»úÖÆ
redisʵÏÖµÄͬ²½»úÖÆÏà¶Ô¼òµ¥£¬È±ÉÙͬ²½»úÖƳ£¼ûµÄcheck pointºÍУÑé»úÖÆ¡£
ÔÚ ÔËÐÐʱ£¬Èç¹ûmaster -> slaveͬ²½ÇëÇóת·¢±»¶ªÆú, slave½«ÎÞ·¨»Ö¸´¸ÃÇëÇóµÄÏà¹ØÐÅÏ¢£¬Ö±µ½slaveÖØÆôʱ´ÓmasterÈ«Á¿¼ÓÔØÊý¾Ýʱ²ÅÄÜÐÞ¸´¡£Òò´Ë£¬½¨ÒéʹÓÃredis¾¡Á¿ÀûÓÃÆä key/valueºÍvalueÖ§³Ö¶àÖÖÀàÐ͵ÄÌØÐÔ£¬´æ´¢Ò»Ð©Ïà¶Ô²»ÖØÒªµÄÊý¾Ý¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ