Redis介紹及入門安裝及使用
什么是Redis
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster。
根據(jù)Redis的官網(wǎng)介紹,我們可以得知Redis有以下功能及特性:
- Redis是一個開源的、基于BSD協(xié)議的內(nèi)存數(shù)據(jù)結(jié)構(gòu)存儲,可以用來當(dāng)做數(shù)據(jù)庫、緩存、消息代理。
- 支持的數(shù)據(jù)格式包括:strings、hashes、lists、sets、sorted sets with range queries、bitmaps, hyperloglogs、geospatial indexes with radius queries 、streams。
- Redis本身提供復(fù)制、Lua腳本、LRU清除、事務(wù)、及不同級別的持久機(jī)制,通過Sentinel和Cluster來保證高可用性。
為什么要選擇Redis
- 性能優(yōu)異,Redis官方驗證了在不同CPU下的Redis性能,感興趣的同學(xué)請移步,https://redis.io/topics/benchmarks ,每秒10w+的讀寫操作是完全是小菜一碟,當(dāng)然在實際中因為服服務(wù)器的CPU/環(huán)境會有些許出入,但足以應(yīng)對覺得大多數(shù)的復(fù)雜應(yīng)用場景。
- 支持復(fù)雜的數(shù)據(jù)結(jié)構(gòu),除了常用的KEY -Value之外,還支持其他多種復(fù)雜的數(shù)據(jù)結(jié)構(gòu),比如Set 、Lists、bitmaps、hyperloglogs 、 geospatial 、stream。
- 提供了持久化的功能,方便快速恢復(fù)數(shù)據(jù)。
- 支持讀寫分離、主從復(fù)制、哨兵模式、集群模式,保證高可用。
- 對存儲的Value大小沒有內(nèi)容限制。方便存儲較大的數(shù)據(jù)。
安裝Redis
官網(wǎng)最新穩(wěn)定的版本為:[Redis 5.0.6 is the latest stable version]:http://download.redis.io/releases/redis-5.0.6.tar.gz 。
Redis的安裝
$ wget http://download.redis.io/releases/redis-5.0.6.tar.gz
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6
$ make
如果在執(zhí)行make指令時出現(xiàn)如下錯誤:
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/opt/software/redis/redis-5.0.6/src'
make: *** [all] Error 2
說明沒有安裝gcc,因為Redis是C實現(xiàn)的,所以需要安裝gcc來進(jìn)行編譯。
安裝gcc指令:
yum install gcc
安裝完成后執(zhí)行以下操作:
# 刪除上次安裝的Redis目錄及子目錄
$ rm -rf redis-5.0.6
# 重新解壓和編譯
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6
$ make
啟動服務(wù)端
# 啟動服務(wù)端
[enjoyitlife@localhost redis-5.0.6]$ src/redis-server
# 啟動成功會顯示如下信息
[enjoyitlife@localhost redis-5.0.6]$ src/redis-server
11872:C 20 Nov 2019 07:13:57.586 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11872:C 20 Nov 2019 07:13:57.586 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=11872, just started
11872:C 20 Nov 2019 07:13:57.586 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
11872:M 20 Nov 2019 07:13:57.589 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
11872:M 20 Nov 2019 07:13:57.590 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
11872:M 20 Nov 2019 07:13:57.590 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 11872
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
11872:M 20 Nov 2019 07:13:57.594 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11872:M 20 Nov 2019 07:13:57.595 # Server initialized
11872:M 20 Nov 2019 07:13:57.595 # 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.
11872:M 20 Nov 2019 07:13:57.595 # 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.
11872:M 20 Nov 2019 07:13:57.595 * Ready to accept connections
客戶端連接
# 新開一個Shell 窗口
[enjoyitlife@localhost ~]$ cd /opt/software/redis/redis-5.0.6
[enjoyitlife@localhost redis-5.0.6]$ src/redis-cli
# 表示連接成功
127.0.0.1:6379>
命令測試
127.0.0.1:6379> set name enjoyitlife
OK
127.0.0.1:6379> get name
"enjoyitlife"
127.0.0.1:6379>
設(shè)置Redis后臺運(yùn)行
# 修改redis.conf 中 daemonize no 改成 yes
daemonize yes
# 可以先將文件改下名,然后在啟動的時候 以指定的文件啟動
[enjoyitlife@localhost redis-5.0.6]$ cp redis.conf redis6379.conf
[enjoyitlife@localhost redis-5.0.6]$ src/redis-server redis6379.conf
# 此時就不會出現(xiàn)Redis的Logo圖片了,而是如下說明, 表明后臺啟動Redis成功
[zpenjoy@localhost redis-5.0.4]$ src/redis-server redis6379.conf
11920:C 20 Nov 2019 07:21:21.021 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11920:C 20 Nov 2019 07:21:21.022 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=11920, just started
11920:C 20 Nov 2019 07:21:21.022 # Configuration loaded
停止Redis服務(wù)
# 可以通過關(guān)閉進(jìn)程的方式 強(qiáng)制關(guān)閉 不建議使用該方式
[enjoyitlife@localhost ~]$ ps -ef|grep redis
[enjoyitlife@localhost redis-5.0.6]$ ps -ef|grep redis
enjoyit+ 11921 1 0 07:21 ? 00:00:00 src/redis-server 127.0.0.1:6379
enjoyit+ 11929 11898 0 07:23 pts/1 00:00:00 src/redis-cli
enjoyit+ 11956 11933 0 07:25 pts/0 00:00:00 grep --color=auto redis
[enjoyitlife@localhost redis-5.0.6]$ kill -9 11921
# 推薦方式
./src/redis-cli shutdown
好了,Redis基本介紹及安裝就介紹到這里了,謝謝閱讀。