Redis簡介
Redis功能簡介
Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作數據庫、緩存和消息中間件。 相比於傳統的關系型數據庫,Redis的存儲方式是key-value型的,說到key-value,我們肯定能想到JSON,但是JSON中value是不區分數據類型的,Redis支持多種類型的數據結構,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與范圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢,能夠更好的幫助我們進行數據的存儲檢索。此外Redis 內置了 復制(replication),LUA腳本(Lua scripting), LRU驅動事件(LRU eviction),事務(transactions) 和不同級別的 磁盤持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)。
Redis常見應用場景
- String:緩存、計數器、分布式鎖等。
- List:鏈表、隊列、微博關注人時間軸列表等。
- Hash:用戶信息、Hash 表等。
- Set:去重、贊、踩、共同好友等。
- Zset:訪問量排行榜、點擊量排行榜等。
關於Redis的更多內容可以訪問Redis的中文網站:http://www.redis.cn/
安裝Redis
介紹完redis接下來就要開始安裝,需要先下載redis,可以去官網下載通過ftp工具上傳,也可以通過wget命令直接在CentOS中獲取。wget 是一個從網絡上自動下載文件的自由工具,支持通過 HTTP、HTTPS、FTP 三個最常見的 TCP/IP協議 下載,並可以使用 HTTP 代理。這里用wget去獲取,使用wget需要先去安裝這個工具。
yum install wget -y
然后在需要下載的目錄執行wget命令獲取redis,這里將文件保存在/usr/local/src目錄
cd /usr/local/src wget http://download.redis.io/releases/redis-5.0.5.tar.gz
下載完成后使用tar命令解壓縮
tar -zxvf redis-5.0.5.tar.gz
執行ls可以看到解壓后的目錄
[root@aliyun src]# ls redis-5.0.5 redis-5.0.5.tar.gz
切換到redis解壓后的目錄,里面的文件可以重點看一下README.md文件,關於redis的介紹還有一些操作啊,這里我就不做展示。
[root@aliyun src]# cd redis-5.0.5 [root@aliyun redis-5.0.5]# ls 00-RELEASENOTES COPYING Makefile redis.conf runtest-moduleapi src BUGS deps MANIFESTO runtest runtest-sentinel tests CONTRIBUTING INSTALL README.md runtest-cluster sentinel.conf utils
然后執行make命令,在這里介紹一下make,make 是一個命令工具,它解釋 Makefile 中的指令(應該說是規則),在 Makefile文件中描述了整個工程所有文件的編譯順序、編譯規則。常用的命令有:
make all:編譯程序、庫、文檔等(等同於make) make install:安裝已經編譯好的程序。復制文件樹中到文件到指定的位置 make unistall:卸載已經安裝的程序。 make clean:刪除由make命令產生的文件 make distclean:刪除由./configure產生的文件 make check:測試剛剛編譯的軟件(某些程序可能不支持) make installcheck:檢查安裝的庫和程序(某些程序可能不支持) make dist:重新打包成packname-version.tar.gz
提起編譯由於Redis是由C編寫的,如果本地環境沒有安裝過編譯器,這里要先安裝gcc編譯器
yum install gcc -y
然后執行make命令,在這里可以通過設置PREFIX的值來指定安裝目錄,在這里將redis安裝在/usr/local/redis/目錄
make install PREFIX=/usr/local/redis
等待執行完,這時redis已經被安裝到了指定目錄,切換后安裝后的目錄
[root@aliyun bin]# cd /usr/local/redis/bin/ [root@aliyun bin]# ls dump.rdb redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
執行./redis-server
,看到下面啟動界面,redis已經成功安裝,並且啟動。
[root@aliyun bin]# ./redis-server 29470:C 27 Nov 2019 08:59:14.990 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29470:C 27 Nov 2019 08:59:14.990 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=29470, just started 29470:C 27 Nov 2019 08:59:14.990 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 29470 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 29470:M 27 Nov 2019 08:59:14.991 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 29470:M 27 Nov 2019 08:59:14.991 # Server initialized 29470:M 27 Nov 2019 08:59:14.991 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 29470:M 27 Nov 2019 08:59:14.991 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 29470:M 27 Nov 2019 08:59:14.991 * DB loaded from disk: 0.000 seconds 29470:M 27 Nov 2019 08:59:14.991 * Ready to accept connections
但是這樣是有問題滴,如果當前窗口停止redis進程就被殺死,而且不方便於管理,接下來就把redis的管理服務化,需要下面兩步操作。
- 配置REDIS_HOME
配置Redis的家目錄后,可以在系統的任何地方使用redis-cli進入redis,在系統配置文件中配置環境變量,指定redis的安裝目錄,將以下代碼配置在/etc/profile文件末尾處:
# REDIS export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin
然后讓系統重新解析配置文件,否則不生效,執行
source /etc/profile
- 生成redis服務化管理腳本
利用redis自帶的工具,可以生成redis服務化管理腳本,切換至redis解壓目錄下的utils文件夾,cd /usr/local/src/redis-5.0.5
執行./install_server.sh,此處為了便於管理都是用redis的默認配置,redis根據端口創建實例,默認端口6379,如果想要創建多實例,可以再次執行./install_server.sh,指定不同的端口,redis會默認生成根據端口號命名的配置文件以及路徑。
[root@aliyun utils]# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/redis/bin/redis-server] Selected config: Port : 6379 Config file : et/etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/redis/bin/redis-server Cli Executable : /usr/local/redis/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort.
點擊回車,可以看到
Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
redis默認幫將服務添加入開機啟動項,將服務管理腳本命名為redis_6379,並且幫我們啟動了服務,這時就可以通過執行以下命令來管理redis服務,如果指定多個端口創建實例,以此類推。
service redis_6379 status 查看端口號6379對應redis實例的運行狀態
service redis_6379 stop 停止端口號6379對應redis實例服務
service redis_6379 start 啟動端口號6379對應redis實例服務
如果想要通過windows上的客戶端工具RedisDesktopManager來連接redis還需要修改以下配置,在執行./install_server.sh之前,先在解壓后的目錄中修改redis.conf配置文件,做如下更改:
bind 127.0.0.1 改為 #bind 127.0.0.1 daemonize no 改為 daemonize yes protected-mode yes 改為 protected-mode no
如果沒有更改,停止redis服務,修改配置,重新執行./install_server.sh即可。
Redis目錄介紹
安裝Redis時指定安裝路徑為/usr/local/redis,切換到/usr/local/redis/bin,bin下面一般為一些可執行命令,其中包含的文件有
[root@aliyun bin]# ls dump.rdb redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
文件描述
- redis-benchmark 性能測試工具,例如:redis-benchmark -n 10000 -c 10 , 10個客戶端,並發10000個SETs/GETs查詢
- dump.rdb 內存數據持久化文件
- redis-check-aof 更新日志檢查
- redis-cli 客戶端工具
- redis-server 服務器程序
卸載Redis
卸載redis其實很簡單,只需以下操作
停止redis對應實例的服務
刪除redis安裝文件,此處目錄為 rm -rf /usr/local/redis
刪除redis服務腳本,以6379實例為例,rm-rf /etc/init.d/redis_6379
刪除redis配置文件,以6379實例為例,rm -rf /etc/redis/6379.conf
刪除/etc/profile中的REDIS_HOME
到此關於Redis的基本安裝卸載就結束了。