標籤:官方網站 server redis memcached
Redis
remote directory server (redis),是一個基於key-value索引值對的持久化資料庫儲存系統,redis支援資料的儲存更豐富,包括string,list,setzset等。
這些資料類型都支援push、pop,add,remove及取交集,差集等更豐富的操作,而且這些操作都是原子性的,在此基礎上支援不同的排序方式,所有的資料都是儲存在內從中的,但是redis的持久化服務還會周期性的吧更新資料寫到磁碟以及把修改的操作記錄追加到檔案裡記錄下來,比memcached更優勢的是,redis還支援master-slave同步,這點很類似關係型資料庫mysql。
redis的出現,在一定程度上彌補了memcached這類key-value記憶體快取服務的不足,在部分場合可以對關係型資料庫起到補充作用
官方網站www.redis.cn國內
www.redis.io國外
redis的優點
1,效能很高:redis能支援超過100K(10W)+每秒的讀寫頻率
2,豐富的資料類型:redis支援二進位strings,lists,hashes,sets,及ordered sets等
3,原子:redis的所有操作都是原子型的,同時redis還支援對決幾個操作全並後的原子性執行.
4,豐富的特性:redis還支援pushlish subscribe,通知,key到期等特性
5,redis支援異機主從同步複製
6,與memcached不同,可以持久化儲存資料
#########redis的應用情境
傳統的mysql+memcahed的網站架構遇到的問題:
mysql的資料庫實際上是適合進行海量資料存放區的,加上通過memcached將熱點資料存放到記憶體cache裡,達到加速數訪問的目的,絕大部分公司曾經使用這樣的架構,但隨著業務資料量的不算增加,和訪問量的持續增長買很多問題就會暴漏出來:
1,需要不斷的對mysql進行拆庫,拆表,memcached也需要不斷跟著擴容,擴容和維護工作佔據大量開發營運時間
2,memcached與mysql的資料一致性問題是個老大難
3,mencached的資料命中率會down機,會導致大量訪問直接穿透到資料庫,導致mysql無法支撐.(這個是最致命的)
4,跨機房cache同步一致性問題
############redis的最佳應用情境
1,redis最佳使用情境是全部資料in-memory
2,reids更多情境是作為memcached的替代品來使用
3,當需要處key/value之外的更多資料類型支援時候,使用redis更合適
4,當儲存的資料不能被踢除時候,使用redis更適合
redis作者談redis應用情境http://blog.nosqlfan.com/html/2235.html
redis資料匯總專題:非常全http://blog.nosqlfan.com/html/3537.html
使用redis bitmap進行活躍使用者統計:
http://blog,nosqlfan.com/html/3501.html
Redis營運之道:http://blog.nosqlfan.com/html/2692.html?ref=rediszt
redis資料庫的小結:
1,提高了DB的可擴充行,只需將新增的資料放到新加的伺服器上就可以了
2,提高了DB的可用性,隻影響到訪問的shard伺服器上的資料的使用者
3.提高了DB 的可維護性,對系統的升級和配置可以按shard一個個來高,對服務產生的影響比較小
4,當達到最大記憶體時候,會清空帶有到期時間的key,及時key未到到期時間
5,redis與DB同步寫的問題,先寫DB,後寫redis,因為寫記憶體基本上沒有問題
################################安裝與部署
Redis安裝部署
1. :
$ wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
2. 解壓縮
$ tar xzf redis-2.6.13.tar.gz
3. 編譯
$ cd redis-2.6.13
$ make
$make install
$cp redis.conf /etc/
參數介紹:
make install命令執行完成後,會在/usr/local/bin目錄下產生本個可執行檔,
分別是redis-server、redis-cli、redis-benchmark、redis-check-aof 、redis-check-dump,
它們的作用如下:
redis-server:Redis伺服器的daemon啟動程式
redis-cli:Redis命令列操作工具。也可以用telnet根據其純文字協議來操作
redis-benchmark:Redis效能測試工具,測試Redis在當前系統下的讀寫效能
redis-check-aof:資料修複
redis-check-dump:檢查匯出工具
4. 修改系統設定檔,執行命令
a) echo vm.overcommit_memory=1 >> /etc/sysctl.conf
b) sysctl vm.overcommit_memory=1 或執行echo vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory
使用數字含義:
0,表示核心將檢查是否有足夠的可用記憶體供應用進程使用;如果有足夠的可用記憶體,記憶體申請允許;否則,記憶體申請失敗,並把錯誤返回給應用進程。
1,表示核心允許分配所有的實體記憶體,而不管當前的記憶體狀態如何。
2,表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體
5. 修改redis設定檔
a) $ cd /etc
b) vi redis.conf
c) 修改daemonize yes---目的使進程在後台運行
參數介紹:
daemonize:是否以後台daemon方式運行
pidfile:pid檔案位置
port:監聽的連接埠號碼
timeout:請求逾時時間
loglevel:log資訊層級
logfile:log檔案位置
databases:開啟資料庫的數量
save * *:儲存快照的頻率,第一個*表示多長時間,第三個*表示執行多少次寫操作。在一定時間內執行一定數量的寫操作時,自動儲存快照。可設定多個條件。
rdbcompression:是否使用壓縮
dbfilename:資料快照檔案名稱(只是檔案名稱,不包括目錄)
dir:資料快照的儲存目錄(這個是目錄)
appendonly:是否開啟appendonlylog,開啟的話每次寫操作會記一條log,這會提高資料抗風險能力,但影響效率。
appendfsync:appendonlylog如何同步到磁碟(三個選項,分別是每次寫都強制調用fsync、每秒啟用一次fsync、不調用fsync等待系統自己同步)
6. 啟動redis
a) $ cd /usr/local/bin
b) ./redis-server /etc/redis.conf
7. 檢查是否啟動成功
a) $ ps -ef | grep redis
1,關閉redis服務
[[email protected] ~]# redis-cli
127.0.0.1:6379> shutdown
2,useage
[[email protected] ~]# redis-cli --help
redis-cli 3.0.3
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
-s <socket> Server socket (overrides hostname and port).
-a <password> Password to use when connecting to the server.
-r <repeat> Execute specified command N times.
-i <interval> When -r is used, waits <interval> seconds per command.
It is possible to specify sub-second times like -i 0.1.
-n <db> Database number.
-x Read last argument from STDIN.
-d <delimiter> Multi-bulk delimiter in for raw formatting (default: \n).
-c Enable cluster mode (follow -ASK and -MOVED redirections).
--raw Use raw formatting for replies (default when STDOUT is
not a tty).
--no-raw Force formatted output even when STDOUT is not a tty.
--csv Output in CSV format.
--stat Print rolling stats about server: mem, clients, ...
--latency Enter a special mode continuously sampling latency.
--latency-history Like --latency but tracking latency changes over time.
Default time interval is 15 sec. Change it using -i.
--latency-dist Shows latency as a spectrum, requires xterm 256 colors.
Default time interval is 1 sec. Change it using -i.
--lru-test <keys> Simulate a cache workload with an 80-20 distribution.
--slave Simulate a slave showing commands received from the master.
--rdb <filename> Transfer an RDB dump from remote server to local file.
--pipe Transfer raw Redis protocol from stdin to server.
--pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
no reply is received within <n> seconds.
Default timeout: 30. Use 0 to wait forever.
--bigkeys Sample Redis keys looking for big keys.
--scan List all keys using the SCAN command.
--pattern <pat> Useful with --scan to specify a SCAN pattern.
--intrinsic-latency <sec> Run a test to measure intrinsic system latency.
The test will run for the specified amount of seconds.
--eval <file> Send an EVAL command using the Lua script at <file>.
--help Output this help and exit.
--version Output version and exit.
Examples:
cat /etc/passwd | redis-cli -x set mypasswd
redis-cli get mypasswd
redis-cli -r 100 lpush mylist x
redis-cli -r 100 -i 1 info | grep used_memory_human:
redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
redis-cli --scan --pattern ‘*:12345*‘
[[email protected] ~]# cd /usr/local/bin/
[[email protected] bin]# ./redis-server /etc/redis.conf
[[email protected] bin]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>
127.0.0.1:6379> help get
GET key
summary: Get the value of a key
since: 1.0.0
group: string
127.0.0.1:6379> set db.test.username helloboy
OK
127.0.0.1:6379> get db.test.username
"helloboy"
127.0.0.1:6379> set no002 helloboy
OK
127.0.0.1:6379> get no002
"helloboy"
[[email protected] ~]# redis-cli -h 127.0.0.1 -p 6379 set 002num lisia
OK
[[email protected] ~]# redis-cli -h 127.0.0.1 -p 6379 get 002num
"lisia"
[[email protected] ~]# redis-cli -h 127.0.0.1 -p 6379 get 002num
"lisia"
[[email protected] ~]# redis-cli del 002num
(integer) 1
[[email protected] ~]# redis-cli -h 127.0.0.1 -p 6379 get 002num
(nil)
[[email protected] ~]#
[[email protected] ~]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
set no003 jihui
+OK
get no003
$5
jihui
###############字串類型
這是簡單的redis最簡單的類型,如果你只用這種類型,redis就是一個可以持久化的memcacehd伺服器
下面是字串類型:
[[email protected] ~]# redis-cli set mykey "my binary safe value"
OK
[[email protected] ~]# redis-cli get mykey
"my binary safe value"
"my binary safe value" 如你所示,通常用set和get來設定和擷取字串值
值可以是任何種類的字串,(包括位元據),例如你可以再一個鍵下儲存一副jpeg圖片,值的長度不超過1GB
list:
[[email protected] ~]# redis-cli rpush messages "hello old are you?"
(integer) 1
[A[[email protected] ~]# redis-cli rpush messages "im fine thank you"
(integer) 2
[[email protected] ~]# redis-cli rpush messages "and you "
(integer) 3
讀取list
[[email protected] ~]# redis-cli lrange messages 0 2
1) "hello old are you?"
2) "im fine thank you"
3) "and you "
[[email protected] ~]# redis-cli lrange messages 0 1
1) "hello old are you?"
2) "im fine thank you"
[[email protected] ~]# redis-cli lrange messages 0 0
1) "hello old are you?"
###########################為php安裝redis用戶端
wget https://github.com/nicolasff/phpredis/archive/master.zip
unzip master.zip
1005 cd phpredis-master/
1006 /usr/local/php/bin/phpize
1007 ./configure --with-php-config=/usr/local/php/bin/php-config
1008 make
1009 make install
[[email protected] phpredis-master]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
修改php.ini ,重啟php
[[email protected] phpredis-master]# echo "extension=redis.so" >> /usr/local/php/etc/php.ini
[[email protected] phpredis-master]#
[[email protected] phpredis-master]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
儲存到磁碟
save 900 1 每900秒 1個記錄
save 300 10 每300秒 10個記錄
#####################redis 主從配置:
只需在從redis伺服器配置
slaveof master-ip master-port
slaveof 192.168.0.110 6379
重啟從redis服務即開
###################redis負載平衡
可以使用lvs,像輪詢web伺服器一樣即可
本文出自 “crazy_sir” 部落格,請務必保留此出處http://douya.blog.51cto.com/6173221/1680689
redis --初級筆記