一、簡介
定義
Remote DIctionary Server(Redis) 是一個由Salvatore Sanfilippo寫的key-value存儲系統。
Redis是一個開源的使用ANSI C語言編寫、遵守BSD協議、支持網絡、可基於內存亦可持久化的日志型、Key-Value數據庫,並提供多種語言的API。
redis通常被稱為數據結構服務器。
redis是基於key-value的no sql數據庫,因此,先來了解一下關於key相關的知識點
1、任何二進制的序列都可以作為key使用
2、Redis有統一的規則來設計key
3、對key-value允許的最大長度是512MB
五種數據類型
字符串(string), 哈希(map), 列表(list), 集合(set) 和 有序集合(zset[sorted set])
應用場景
1、最常用的就是會話緩存
2、消息隊列,比如支付
3、活動排行榜或計數
4、發布、訂閱消息(消息通知)
5、商品列表、評論列表等
Redis 英文官網:https://redis.io/
Redis 中文官網:https://www.redis.net.cn/
Redis GitHub 開源地址:https://github.com/antirez/redis
但是官方沒有windows版本
WINDOWS下載安裝:https://github.com/microsoftarchive/redis/releases
二、配置文件
創建redis.conf文件:
這是一個配置文件,指定了redis的監聽端口,timeout等。如下面有:port 6379。
配置:
遇到問題:
[18892] 05 Jan 16:02:28.584 # The Windows version of Redis allocates a memory mapped heap for sharing with the forked process used for persistence operations. In order to share this memory, Windows allocates from the system paging file a portion equal to the size of the Redis heap. At this time there is insufficient contiguous free space available in the system paging file for this operation (Windows error 0x5AF). To work around this you may either increase the size of the system paging file, or decrease the size of the Redis heap with the --maxheap flag. Sometimes a reboot will defragment the system paging file sufficiently for this operation to complete successfully. Please see the documentation included with the binary distributions for more details on the --maxheap flag. Redis can not continue. Exiting.
處理方法:
windows硬盤需要配置虛擬內存,如果還有問題,清理磁盤碎片
redis.windows.conf
maxheap 1024000000 daemonize no
更改redis的配置需要修改redis.conf文件,以下是它一些主要的配置注釋:
#是否作為守護進程運行
daemonize no
#Redis 默認監聽端口
port 6379
#客戶端閑置多少秒后,斷開連接
timeout 300
#日志顯示級別
loglevel verbose
#指定日志輸出的文件名,也可指定到標准輸出端口
logfile redis.log
#設置數據庫的數量,默認最大是16,默認連接的數據庫是0,可以通過select N 來連接不同的數據庫
databases 32
#Dump持久化策略
#當有一條Keys 數據被改變是,900 秒刷新到disk 一次
#save 900 1
#當有10 條Keys 數據被改變時,300 秒刷新到disk 一次
save 300 100
#當有1w 條keys 數據被改變時,60 秒刷新到disk 一次
save 6000 10000
#當dump .rdb 數據庫的時候是否壓縮數據對象
rdbcompression yes
#dump 持久化數據保存的文件名
dbfilename dump.rdb
########### Replication #####################
#Redis的主從配置,配置slaveof則實例作為從服務器
#slaveof 192.168.0.105 6379
#主服務器連接密碼
# masterauth <master-password>
############## 安全性 ########### #設置連接密碼 #requirepass <password> ############### LIMITS ############## #最大客戶端連接數 # maxclients 128 #最大內存使用率 # maxmemory <bytes> ########## APPEND ONLY MODE ######### #是否開啟日志功能 appendonly no # AOF持久化策略 #appendfsync always #appendfsync everysec #appendfsync no ################ VIRTUAL MEMORY ########### #是否開啟VM 功能 #vm-enabled no # vm-enabled yes #vm-swap-file logs/redis.swap #vm-max-memory 0 #vm-page-size 32 #vm-pages 134217728 #vm-max-threads 4
主從復制
在從服務器配置文件中配置slaveof ,填寫服務器IP及端口即可,如果主服務器設置了連接密碼,在masterauth后指定密碼就行了。
持久化
- redis提供了兩種持久化文案,Dump持久化和AOF日志文件持久化。
- Dump持久化是把內存中的數據完整寫入到數據文件,由配置策略觸發寫入,如果在數據更改后又未達到觸發條件而發生故障會造成部分數據丟失。
- AOF持久化是日志存儲的,是增量的形式,記錄每一個數據操作動作,數據恢復時就根據這些日志來生成。
三、.命令行操作
使用CMD命令提示符,打開redis-cli連接redis服務器 ,也可以使用telnet客戶端
# redis-cli -h 服務器 –p 端口 –a 密碼
redis-cli.exe -h 127.0.0.1 -p 6379
連接成功后,就可對redis數據增刪改查了,如字符串操作:

以下是一些服務器管理常用命令:
info #查看服務器信息
select <dbsize> #選擇數據庫索引 select 1
flushall #清空全部數據
flushdb #清空當前索引的數據庫
slaveof <服務器> <端口> #設置為從服務器
slaveof no one #設置為主服務器
shutdown #關閉服務
附加幾個 bat 批處理腳本,請根據需要靈活配置
注冊/卸載系統服務
redis-server.exe --service-install redis.windows.conf --loglevel verbose redis-server --service-uninstall
卸載服務:redis-server --service-uninstall
開啟服務:redis-server --service-start
停止服務:redis-server --service-stop
啟動命令
redis-server.exe redis.windows.conf
可以把 redis 的路徑加到系統的環境變量里。命令后面的 redis.conf 可以省略,此時會啟用默認參數。(Redis 默認使用 6379 端口)
安裝RADIS
開啟redis:
1.打開cmd,然后進入redis解壓目錄。
2.執行命令redis-server.exe redis.windows.conf 如果出現如下錯誤

則執行如下命令:
1)redis-cli.exe
2)shutdown
2)exit
3.如果出現錯誤,執行如上命令后,重新執行redis-server.exe redis.windows.conf命令,如果正常開啟,會出現如下圖示:

4.如果沒出現錯誤,在解壓后直接執行redis-server.exe,可看到上面服務端執行成功的截圖,然后再執行redis-cli.exe,就可以進入下面的客戶端界面,添加數據如下所示(常用命令):

推薦:http://www.redis.net.cn/
參考文章
https://www.cnblogs.com/youkanyouxiao/p/9072248.html
https://www.cnblogs.com/sxdcgaq8080/p/7204878.html
https://blog.csdn.net/u010137839/article/details/80210328
https://www.cnblogs.com/cing/p/7724653.html win7下安裝redis
