Redis-初探

官网 http://redis.io
中文网 http://redis.cn
命令参考 http://redisdoc.cn

Redis(Remote Dictionary Server)是一个开源的key-value内存存储的NoSQL数据库,具有非常强悍的读写性能,现阶段正越来越多地被用于互联网高并发场景。

1 安装

1.1 Linux(CentOS 7.4)下yum方式安装
yum install redis
1.2 手动编译安装

下载安装redis 4.0.9至/usr/local/src目录

wget -P /usr/local/src http://download.redis.io/releases/redis-4.0.9.tar.gz

解压至/opt/redis目录

mkdir /opt/reids && tar xzvf /usr/local/src/redis-4.0.9.tar.gz -C /opt/redis

编译安装(需要依赖gcc)

cd /opt/redis/redis-4.0.9 && make MALLOC=libc

复制相关运行文件到/usr/local/bin目录

make install

[可选]安装后的测试(需要依赖tcl 8.5+)

make test

2 启动

2.1 交互模式启动
[root@VM_0_171_centos ~]# redis-server
23173:C 26 Mar 16:13:02.365 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 23173
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

23173:M 26 Mar 16:13:02.366 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
23173:M 26 Mar 16:13:02.366 # Server started, Redis version 3.2.3
23173:M 26 Mar 16:13:02.366 # 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.
23173:M 26 Mar 16:13:02.366 # 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.
23173:M 26 Mar 16:13:02.366 * DB loaded from disk: 0.000 seconds
23173:M 26 Mar 16:13:02.366 * The server is now ready to accept connections on port 6379

2.2 后台启动

step 1:需要修改配置文件/etc/redis.conf

# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes

step 2:使用配置文件启动

[root@VM_0_171_centos ~]# redis-server /etc/redis.conf
[root@VM_0_171_centos ~]#

3 客户端连接

[root@VM_0_171_centos ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

4 停止服务

[root@VM_0_171_centos ~]# redis-shutdown
[root@VM_0_171_centos ~]#

5 其它程序

redis通过yum方式安装后,所有程序均位于/usr/bin目录

[root@VM_0_171_centos ~]# ls -l /usr/bin | grep redis
-rwxr-xr-x  1 root root      86832 8月   5 2016 redis-benchmark
-rwxr-xr-x  1 root root      15408 8月   5 2016 redis-check-aof
-rwxr-xr-x  1 root root     975248 8月   5 2016 redis-check-rdb
-rwxr-xr-x  1 root root     173344 8月   5 2016 redis-cli
lrwxrwxrwx  1 root root         12 3月  25 23:41 redis-sentinel -> redis-server
-rwxr-xr-x  1 root root     975248 8月   5 2016 redis-server
-rwxr-xr-x  1 root root        918 8月   5 2016 redis-shutdown
[root@VM_0_171_centos ~]#

其中redis-benchmark可以用来测试redis的性能,官网给出的信息是redis的读速度:110000次/s,写速度:81000次/s

[root@VM_0_171_centos ~]# redis-benchmark
====== PING_INLINE ======
  100000 requests completed in 0.69 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
145137.88 requests per second

====== PING_BULK ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153846.16 requests per second

====== SET ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.97% <= 1 milliseconds
100.00% <= 1 milliseconds
153609.83 requests per second

====== GET ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
156250.00 requests per second

====== INCR ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
156006.25 requests per second

====== LPUSH ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153846.16 requests per second

====== RPUSH ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
156985.86 requests per second

====== LPOP ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153374.23 requests per second

====== RPOP ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153374.23 requests per second

====== SADD ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
152671.77 requests per second

====== SPOP ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
154083.20 requests per second

====== LPUSH (needed to benchmark LRANGE) ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153846.16 requests per second

====== LRANGE_100 (first 100 elements) ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.97% <= 1 milliseconds
100.00% <= 1 milliseconds
155279.50 requests per second

====== LRANGE_300 (first 300 elements) ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.99% <= 1 milliseconds
100.00% <= 1 milliseconds
155763.23 requests per second

====== LRANGE_500 (first 450 elements) ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
154320.98 requests per second

====== LRANGE_600 (first 600 elements) ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153374.23 requests per second

====== MSET (10 keys) ======
  100000 requests completed in 0.63 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
159744.41 requests per second


[root@VM_0_171_centos ~]#

6 配置文件

通过yum方式安装后,redis的配置文件的位置为:/etc/redis.conf,包括前面提到的后台启动也是需要通过修改配置文件实现,配置项比较多,需要对redis有进一步了解

6.1 设置密码

如果要限制连接redis必须使用密码,需要配置requirepass选项:requriepass <password>

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass 123456

设置密码后需要重启服务,再连接redis-cli时会提示需要验证授权,通过auth <password>命令验证

[root@VM_0_171_centos ~]# vim /etc/redis.conf
[root@VM_0_171_centos ~]# redis-server /etc/redis.conf
[root@VM_0_171_centos ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

7 帮助

客户端连接redis后,可以通过help获取帮助,帮助信息里面给出了3种不同方式获取帮助,具体用法其它章节会进一步描述

127.0.0.1:6379> help
redis-cli 3.2.3
To get help about Redis commands type:
      "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit

To set redis-cli perferences:
      ":set hints" enable online hints
      ":set nohints" disable online hints
Set your preferences in ~/.redisclirc

8.常用命令

8.1查找匹配的key(支持通配符?*)

keys pattern

8.2键的总数

dbsize

8.3检查键是否存在

exists key

8.4删除键

del key

8.5设置键过期时间

expire key seconds 

8.6查看键剩余过期时间

# 大于等于0的整数:键剩余的过期时间
# -1:键未设置过期时间
# -2:键不存在
ttl key

8.7键的数据类型

type key
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,828评论 6 526
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,669评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,467评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,238评论 1 306
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,039评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,561评论 1 319
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,658评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,806评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,316评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,293评论 3 353
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,443评论 1 364
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,998评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,698评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,097评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,333评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,012评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,434评论 2 370