最近在使用redis,剛開始學習,一點一點慢慢積累,根據需求打算最終替換個人mysql庫中的一個表,關於redis的介紹google之,下面先開始安裝吧,使用系統爲debian6。 php
下載最新的穩定版redis mysql
wget http://redis.googlecode.com/files/redis-2.4.16.tar.gz安裝
tar xzf redis-2.4.16.tar.gz cd redis-2.4.16 make cp src/redis-server /usr/bin/ cp src/redis-cli /usr/bin/ cp src/redis-check-aof /usr/bin/ cp src/redis-check-dump /usr/bin/ cp src/redis-benchmark /usr/bin/編寫redis啓動腳本:/etc/init.d/redis-server
#! /bin/sh ### BEGIN INIT INFO # Provides: redis-server # Required-Start: $syslog $remote_fs # Required-Stop: $syslog $remote_fs # Should-Start: $local_fs # Should-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: redis-server - Persistent key-value db # Description: redis-server - Persistent key-value db ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/redis-server DAEMON_ARGS=/etc/redis/redis.conf NAME=redis-server DESC=redis-server PIDFILE=/var/run/redis.pid test -x $DAEMON || exit 0 test -x $DAEMONBOOTSTRAP || exit 0 set -e case "$1" in start) echo -n "Starting $DESC: " touch $PIDFILE chown root:root $PIDFILE if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid root:root --exec $DAEMON -- $DAEMON_ARGS then echo "$NAME." else echo "failed" fi ;; stop) echo -n "Stopping $DESC: " if start-stop-daemon --stop --retry 10 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON then echo "$NAME." else echo "failed" fi rm -f $PIDFILE ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2 exit 1 ;; esac exit 0制定配置文件/etc/redis/redis.conf,在redis的源碼包中有一個redis.conf配置文件,拷貝至/etc/redis目錄下,啓動redis:/etc/init.d/redis-server start 便可啓動redis,固然不使用啓動腳本也是能夠的,直接使用redis-server命令啓動redis服務,須要在啓動時手動指定一個redis.conf配置文件便可。
簡單測試 git
root@redis#redis-cli redis 127.0.0.1:6379> set redis test OK redis 127.0.0.1:6379> get redis "test" redis 127.0.0.1:6379>redis.conf詳解
#是否之後臺進程運行,默認是no改成yes daemonize yes #若是之後臺進程運行,須要指定一個pid文件 pidfile /var/run/redis.pid #服務監聽端口 port 6379 #默認是註釋掉的,監聽全部端口,修改 bind 192.168.1.1 #客戶端鏈接空閒多少秒後斷開鏈接,0表示禁用該功能 timeout 0 #日誌級別,有debug、notice、waring、verbose等模式 loglevel verbose #日誌記錄方式,若是之後臺進程運行,日誌輸出至/dev/null logfile stdout #是否把日誌記錄進syslog syslog-enabled no #指定syslog標記,默認註釋 syslog-ident redis #指定記錄進syslog的級別,默認註釋 syslog-facility local0 #設置數據庫的數量,默認爲0 databases 16 ############ SNAPSHOTTING ############ #在多長時間內有多少次更新操做就把數據保存到硬盤 #save <seconds> <changes> 能夠有多條規則 save 900 1 save 300 10 save 60 10000 #當dump數據至.rdb時是否開啓LZF壓縮,爲了節省cpu能夠關閉,默認開啓。 rdbcompression yes #指定本地數據庫文件名 dbfilename dump.rdb #本地數據庫存放路徑 dir /opt ############ REPLICATION ############## #本機爲從服務時設置主的ip和端口 #slaveof <masterip> <masterport> #當主使用requirepass設置了密碼時,從鏈接主須要密碼 #masterauth <master-password> #當從失去到主的鏈接,從的響應能夠有兩種行爲,設置爲yes時仍然響應客戶端請求, #設置爲no時,給客戶端響應error sync slave-serve-stale-data yes #從每隔多長時間ping一次主,單位爲秒 #repl-ping-slave-period 10 #Bulk transfer I/O timeout或者主的data/ping響應超時 #repl-timeout 60 ################ SECURITY ############### #客戶端在執行命令前須要通過密碼驗證 requirepass passwd ################ LIMITS ################ #容許客戶端同時鏈接的最大連接數,默認沒有限制,0表示沒限制,當達到最大限制後 #若是有新的鏈接過來,則會返回max number of clients reached maxclients 128 #設置redis可使用的最大內存,當達到限制後redis會根據策略maxmemmory-policy移除相關key,若是根據策略設置不能移除相關key,redis返回一個錯誤。 maxmemory 1G #當達到最大使用內存後,redis在移除key時使用的策略。 #volatile-lru -> remove the key with an expire set using an LRU algorithm #allkeys-lru -> remove any key accordingly to the LRU algorithm #volatile-random -> remove a random key with an expire set #allkeys->random -> remove a random key, any key #volatile-ttl -> remove the key with the nearest expire time (minor TTL) #noeviction -> don't expire at all, just return an error on write operations maxmemory-policy volatile-lru ################ APPEND ONLY MODE ######### #是否在每次更新操做後把數據當即更新進磁盤中,開啓後redis會把每一個寫操做追加進appendonly.aof文件中,這個緊急在開啓redis服務時被加載 appendonly no #指定更新日誌文件名 appendfilename appendonly.aof #指定更新日誌條件,調用系統的fsync()把數據寫入磁盤中,有三種模式: #no 等待操做系統自己本身同步buffer數據,快 #always 每次更新log後同步數據,慢但安全 #everysec 每秒同步一次,折中作法 appendfsync everysec # no-appendfsync-on-rewrite no #自動重寫aof文件,0表示禁用此功能 auto-aof-rewrite-percentage 100 #定義在重寫aof文件前,aof文件的最小值 auto-aof-rewrite-min-size 64mb ################### SLOW LOG ############## #慢查詢記錄,值記錄命令操做時間,不包括同客戶端鏈接之類的I/O操做,記錄操做時間大於N微秒的,若是設置爲負數,則禁用此功能 slowlog-log-slower-than 10000 #設置慢查詢日誌長度,默認是麼有限制的,可是它會佔用內存,因此最好限制一下 slowlog-max-len 128 ############## VIRTUAL MEMORY ############ #vm機制在redis2.4中強烈不建議使用, #是否開啓vm機制 vm-enabled no #虛擬內存文件存放路徑,多個redis進程不能共享同一個文件 vm-swap-file /tmp/redis.swap #將全部大於vm-max-memory的數據存入虛擬內存,0表示全部數據都存儲在虛擬內存 vm-max-memory 0 #swap文件被分割成pages,對象能夠存儲在多個pages中,可是pages不能被多個對象共享,因此page大小定義得合適,單位bytes vm-page-size 32 #swap文件中的內存頁數,在磁盤上的每8 page會消耗1 byte,swap文件的大小是vm-page-size * vm-pages vm-pages 134217728 #在同時讀寫swap文件的進程數 vm-max-threads 4 ############### ADVANCED CONFIG ######### #當hash中包含超過指定元素個數且最大的元素沒有超過臨界值,hash將以特殊的編碼方式來存儲 hash-max-zipmap-entries 512 #hash中一個元素的最大值 hash-max-zipmap-value 64 #同hash list-max-ziplist-entries 512 list-max-ziplist-value 64 #同hash set-max-intset-entries 512 #同hash set zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes ################# INCLUDES ############ #能夠包含其餘配置文件 #include /path/to/local.conf #include /path/to/other.conf安裝phpredis
下載phpredis擴展 github
git clone https://github.com/nicolasff/phpredis.git安裝
cd phpredis phpize ./configure make make installredis.so 默認被安裝在源碼的modules目錄,拷貝redis.so到適當的位置,debian6中是/usr/lib/php5/20090626/
安裝完畢後,默認是不會開啓phpredis模塊的,還須要手動開啓,在/etc/php5/conf.d目錄下創建redis.ini文件,內容爲:extension=redis.so redis
重啓apache,編寫info()php文件,會看到redis信息,php -m 也能夠看到。 sql
安裝完畢後,就該實戰了,但願個人項目能夠順利完成。 shell