除了5种数据结构,Redis还提供了许多额外的功能:
Redis的简单主要表现在三个方面。
Redis提供了简单的TCP通信协议,很多编程语言可以很方便地接入到Redis,并且由于Redis受到社区和各大公司的广泛认可,所以支持Redis的客户端语言也非常多,几乎涵盖了主流的编程语言,例如Java、PHP、Python、C、C++、Nodejs等。
通常看,将数据放在内存中是不安全的,一旦发生断电或者机器故障,重要的数据可能就会丢失,因此Redis提供了两种持久化方式:RDB和AOF(这块后续会单独写篇文章讨论),即可以用两种策略将内存的数据保存到硬盘中(如图所示)这样就保证了数据的可持久性。
Redis提供了复制功能,实现了多个相同数据的Redis副本(如图所示),复制功能是分布式Redis的基础。
Redis从2.8版本正式提供了高可用实现Redis Sentinel,它能够保证Redis节点的故障发现和故障自动转移。Redis从3.0版本正式提供了分布式实现Redis Cluster,它是Redis真正的分布式实现,提供了高可用、读写和容量的扩展性。
实质就是链表(双向循环列表)
value可以重复,可以通过下标取出对应的value值
左右两边都能进行插入和删除数据。
使用技巧
应用场景
保存多个字符串的元素
与列表区别
应用场景
官方下载地址
中文网站下载地址
安装教程非常简单,官网上也有详细讲解,以5.0.5版本为例
将下载好的压缩包放到linux上某个路径下,执行解压命令,我默认解压到/opt/install目录下
[root@iz2zehcv8qx0677zmjq8wnz software]# tar xzf redis-5.0.5.tar.gz -C /opt/install/
[root@iz2zehcv8qx0677zmjq8wnz software]# cd /opt/install/
[root@iz2zehcv8qx0677zmjq8wnz install]# ll
total 4
drwxrwxr-x 6 root root 4096 May 16 2019 redis-5.0.5
进入解压后的文件夹中
cd redis-5.0.5
#执行make
make
等候安装完成
进入src目录执行安装命令
[root@iz2zehcv8qx0677zmjq8wnz redis-5.0.5]# cd src/
[root@iz2zehcv8qx0677zmjq8wnz src]# make install
CC Makefile.dep
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
启动命令默认安装在/usr/local/bin/
目录下
[root@iz2zehcv8qx0677zmjq8wnz src]# cd /usr/local/bin/
[root@iz2zehcv8qx0677zmjq8wnz bin]# ll
total 32744
-rwxr-xr-x 1 root root 4365984 Jan 10 18:20 redis-benchmark
-rwxr-xr-x 1 root root 8116520 Jan 10 18:20 redis-check-aof
-rwxr-xr-x 1 root root 8116520 Jan 10 18:20 redis-check-rdb
-rwxr-xr-x 1 root root 4806328 Jan 10 18:20 redis-cli
lrwxrwxrwx 1 root root 12 Jan 10 18:20 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8116520 Jan 10 18:20 redis-server
安装完毕
卸载redis也是非常的简单
首先查看redis服务是否在运行
[root@iz2zehcv8qx0677zmjq8wnz ~]# ps aux | grep redis
root 19345 0.1 0.1 163060 2448 ? Ssl Jan06 7:47 ./redis-server *:6379
root 31599 0.0 0.0 112660 968 pts/2 R+ 17:54 0:00 grep --color=auto redis
关闭redis服务
如果之前没有设置密码
redis-cli -p 6379 shutdown
如果之前设置过密码
修改/etc/init.d/redis启动脚本
添加如下内容
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/redis.conf"
REDISPORT="6379"
PASSWORD=$(cat $CONF|grep '^\s*requirepass'|awk '{print $2}'|sed 's/"//g')
if [ -z $PASSWORD ]
then
$CLIEXEC -p $REDISPORT shutdown
else
$CLIEXEC -a $PASSWORD -p $REDISPORT shutdown
fi
执行关闭命令
redis-cli -a 你的密码 -p 6379 shutdown
删除make的时候生成的几个redisXXX的文件
[root@iz2zehcv8qx0677zmjq8wnz bin]# ll /usr/local/bin/
total 32748
-rw-r--r-- 1 root root 133 Jan 10 18:02 dump.rdb
-rwxr-xr-x 1 root root 4365984 Jan 5 10:29 redis-benchmark
-rwxr-xr-x 1 root root 8116520 Jan 5 10:29 redis-check-aof
-rwxr-xr-x 1 root root 8116520 Jan 5 10:29 redis-check-rdb
-rwxr-xr-x 1 root root 4806328 Jan 5 10:29 redis-cli
lrwxrwxrwx 1 root root 12 Jan 5 10:29 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8116520 Jan 5 10:29 redis-server
[root@iz2zehcv8qx0677zmjq8wnz bin]# rm -rf /usr/local/bin/redis*
[root@iz2zehcv8qx0677zmjq8wnz bin]# rm -rf /usr/local/bin/dump*
[root@iz2zehcv8qx0677zmjq8wnz bin]# ll
total 0
删除解压后的所有文件
[root@iz2zehcv8qx0677zmjq8wnz software]# rm -rf redis-5.0.5/
卸载完毕
进入启动命令目录/usr/local/bin/
[root@iz2zehcv8qx0677zmjq8wnz src]# cd /usr/local/bin/
[root@iz2zehcv8qx0677zmjq8wnz bin]# ll
total 32744
-rwxr-xr-x 1 root root 4365984 Jan 10 18:20 redis-benchmark
-rwxr-xr-x 1 root root 8116520 Jan 10 18:20 redis-check-aof
-rwxr-xr-x 1 root root 8116520 Jan 10 18:20 redis-check-rdb
-rwxr-xr-x 1 root root 4806328 Jan 10 18:20 redis-cli
lrwxrwxrwx 1 root root 12 Jan 10 18:20 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8116520 Jan 10 18:20 redis-server
启动Redis,出现下面表示启动成功
[root@iz2zehcv8qx0677zmjq8wnz bin]# redis-server
3460:C 10 Jan 2020 18:26:25.809 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3460:C 10 Jan 2020 18:26:25.809 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3460, just started
3460:C 10 Jan 2020 18:26:25.809 # 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: 3460
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
3460:M 10 Jan 2020 18:26:25.811 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3460:M 10 Jan 2020 18:26:25.811 # Server initialized
3460:M 10 Jan 2020 18:26:25.811 # 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.
3460:M 10 Jan 2020 18:26:25.811 # 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.
3460:M 10 Jan 2020 18:26:25.811 * Ready to accept connections
相关配置修改
上述启动是前台启动,操作不方便,可以修改redis配置文件使其后台启动
进入redis配置文件(redis安装目录中的redis.conf文件)
[root@iz2zehcv8qx0677zmjq8wnz redis-5.0.5]# vi /opt/install/redis-5.0.5/redis.conf
修改其中的参数daemonize
daemonize no
修改为:
daemonize yes
设置外网可访问
注释 bind 127.0.0.1:#bind 127.0.0.1
修改protected-mode为no:protected-mode no
建议设置密码:
重启redis即可生效
客户端连接
./redis-cli [-h 127.0.0.1 -p 6379 -a 你的密码]
如果未设置密码,去除-a参数即可
./redis-cli [-h 127.0.0.1 -p 6379]
[root@iz2zehcv8qx0677zmjq8wnz bin]# redis-cli shutdown