簡介
-
Redis(全稱:Remote Dictionary Server 遠程字典服務)是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日誌型、Key-Value數據庫,並提供多種語言的API
-
redis的出現時間並不長,是NoSQL中的一種,基於鍵-值型的存儲,與memcache類似,但是memcache中只是內存的緩存,而redis不僅是內存中的緩存,還提供持久存儲。
-
官網:https://redis.io/commands
redis 的優勢
1、性能極高 -- SQL語句的讀寫量: 讀取速度 11W/s,寫的速度8.1W/s
2、數據類型
3、原子性,所有的語句是一個整體,要麼執行成功,要麼失敗
redis與mamcache不同之處:
數據結構:Memcache僅能支持簡單的K-V形式,Redis支持的數據更多
多線程:Memcache支持多線程,Redis支持單線程,CPU利用Memcache利用率更高
持久化:Redis支持持久化,Memcache不支持持久化
分佈式:Redis做主從結構,而Memcache服務器需要通過hash一致化來支撐主從結構
虛擬內存:Redis當物理內存使用完時,會將一些很久沒有用的內存交換到磁盤,而Memcache採取的LUR策略,將一部分數據刷新掉
與其他key-value緩存產品的區別
Redis各種數據類型應用和實現方式
String:數據結構是簡單的K-V類型,v可以是數據,也可以是數字(常用操作:set,get,decr,incr,mget)
set:sadd,spop,smembers,sunion,sinter
list :lpush/rpush,lpop/rpop,lpoprpush,lrange
hash:hset,hgetall,hget
zset:zadd,zrank,zrange,zrem,zcard
部署
- 部署環境
操作系統 | ip地址 |
---|---|
CentOS Linux release 7.5.1804 | 192.168.10.11 /24 |
- 下載 redis 安裝包
[root@localhost ~]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz
- 解壓
[root@localhost ~]# tar zvxf redis-2.8.17.tar.gz
- 安裝好編譯環境
[root@localhost ~]# yum -y install gcc c++
- 編譯
[root@localhost ~]# cd redis-2.8.17
[root@localhost redis-2.8.17]# ls
00-RELEASENOTES deps README sentinel.conf
BUGS INSTALL redis.conf src
CONTRIBUTING Makefile runtest tests
COPYING MANIFESTO runtest-sentinel utils
[root@localhost redis-2.8.17]# make
這裏因爲有makefile,所以直接make,若果make 出現一下錯誤,則解決辦法:
[root@localhost redis-2.8.17]# make MALLOC=libc
cd src && make all
make[1]: Entering directory `/root/redis-2.8.17/src'
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-2.8.17/src'
make: *** [all] Error 2
[root@localhost redis-2.8.17]# ls
請在make後面加上MALLOC=libc,出現下一界面就ok
- 測試下編譯環境
[root@localhost ~]# cd redis-2.8.17/src/
[root@localhost src]# make test
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1
[root@localhost src]#
解決方法:
[root@localhost src]# yum -y install tcl
接着等着編譯……
\o/ All tests passed without errors!
Cleanup: may take some time... OK
這裏似乎沒有出錯,but……=如果已近安裝mysql,報錯了,考慮修改一下內容:
[root@localhost redis-2.8.17]# ls
00-RELEASENOTES COPYING Makefile redis.conf sentinel.conf utils
BUGS deps MANIFESTO runtest src
CONTRIBUTING INSTALL README runtest-sentinel tests
[root@localhost redis-2.8.17]# vi tests/integration/replication-2.tcl
搜索after 將原有的1000 改大一點,比如:10000 即可
after 1000---------->10000
- 啓動
[root@localhost src]# ./redis-server
[14813] 30 Oct 23:02:17.786 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
[14813] 30 Oct 23:02:17.787 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.17 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 14813
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[14813] 30 Oct 23:02:17.788 # Server started, Redis version 2.8.17
[14813] 30 Oct 23:02:17.789 # 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.
[14813] 30 Oct 23:02:17.789 * The server is now ready to accept connections on port 6379
ok!步驟到這裏,安裝基本就完成了