暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Redis安装及使用

叶同学专栏 2021-04-16
467

Redis介绍

Redis是一个NoSQL的内存数据库,数据都是存放在内存中,所以它的性能极高, 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。有着丰富的数据类型, Strings, Lists, Hashes, Sets 等。并且具有原子性, 单个操作是原子性的,多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。

Redis搭建的方式

一、单节点实例

二、主从模式(master/slaver)

在配置文件中加上一行配置:slaveof 192.168.1.1 6379指明master的ip和端口号。

三、sentinel模式

sentinel的中文含义是哨兵、守卫。在主从模式中,当master节点挂了以后,slave节点不能主动选举一个master节点出来,那么我就安排一个或多个sentinel来做这件事,当sentinel发现master节点挂了以后,sentinel就会从slave中重新选举一个master, 修改它们的配置文件,其他slave的配置文件也会被修改,比如slaveof属性会指向新的master 。

四、cluster模式

解决单机Redis容量有限的问题,将Redis的数据根据一定的规则分配到多台机器 。

Redis单机安装及启动

解压编译

使用6.0.10版本编译时会报错,因为gcc版本低于5,换回低版本的安装

[root@yejf]/opt# tar -zxvf redis-5.0.10.tar.gz --directory=/usr/local/redis
[root@yejf]/opt# cd /usr/local/redis/redis-5.0.10
[root@yejf]/usr/local/redis/redis-5.0.10# make -j 16
...
   CC redis-cli.o
    CC redis-benchmark.o
    LINK redis-benchmark
    LINK redis-server
    INSTALL redis-sentinel
    INSTALL redis-check-rdb
    INSTALL redis-check-aof
    LINK redis-cli

Hint: It's a good idea to run 'make test' ;)

make[1]: 离开目录“/usr/local/redis/redis-5.0.10/src”

编译成功后,目录下会生成src目录

启动redis服务命令例子

Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --replicaof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

设置登陆密码以及监听地址

#修改配置文件redis.conf
masterauth 1111
bind 192.168.43.100

根据配置文件启动redis服务

[root@yejf]/usr/local/redis/redis-5.0.10# cd src
[root@yejf]/usr/local/redis/redis-5.0.10/src# ./redis-server ../redis.conf
9741:C 13 Jan 2021 15:15:58.019 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9741:C 13 Jan 2021 15:15:58.019 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=9741, just started
9741:C 13 Jan 2021 15:15:58.019 # Configuration loaded
9741:M 13 Jan 2021 15:15:58.019 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.10 (00000000/064 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'
` _.-'|     Port: 6379
 |    `
-._   `._    /     _.-'    |     PID: 9741
  `
-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `
-._`-._        _.-'_.-'    |           http://redis.io
  `
-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'
    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `
-._    `-.__.-'    _.-'
          `
-._        _.-'
              `-.__.-'


9741:M 13 Jan 2021 15:15:58.020 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9741:M 13 Jan 2021 15:15:58.020 # Server initialized
9741:M 13 Jan 2021 15:15:58.020 # 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.
9741:M 13 Jan 2021 15:15:58.020 * DB loaded from disk: 0.000 seconds
9741:M 13 Jan 2021 15:15:58.020 * Ready to accept connections

使用测试客户端程序 redis-cli 和 redis 服务交互

192.168.43.100:6379ping
PONG

常用命令

获取配置文件中的参数配置

192.168.43.100:6379config get masterauth
1) "masterauth"
2) "1111"
192.168.43.100:6379config get port
1) "port"
2) "6379"

切换数据库

192.168.43.100:6379set str aaa
OK
192.168.43.100:6379get str
"aaa"
192.168.43.100:6379select 2
OK
192.168.43.100:6379[2]get str
(nil)
192.168.43.100:6379[2]select 0
OK
192.168.43.100:6379get str
"aaa"

Redis支持多个数据库,并且每个数据库的数据是隔离的不能共享,并且基于单机才有,如果是集群就没有数据库的概念。 
每个数据库对外都是一个从0开始的递增数字命名,Redis默认支持16个数据库(可以通过配置文件支持更多,无上限),可以通过配置databases来修改这一数字。

查找所有的key

192.168.43.100:6379> keys *
1"y"
2"str"
3"a"

常用数据类型的使用

String(字符串)

192.168.43.100:6379set y a1
OK
192.168.43.100:6379get y
"a1"

Hash(哈希)

192.168.43.100:6379> hmset y y1 aaa y2 bbb
(error) WRONGTYPE Operation against a key holding the wrong kind of value
192.168.43.100:6379> del y
(integer) 1
192.168.43.100:6379> hmset y y1 aaa y2 bbb
OK
192.168.43.100:6379> hget y y1
"aaa"
192.168.43.100:6379> hget y y2
"bbb"
192.168.43.100:6379> hget y
(error) ERR wrong number of arguments for 'hget' command

List(列表)

192.168.43.100:6379del y
(integer) 1
192.168.43.100:6379lpush y aaa
(integer) 1
192.168.43.100:6379lpush y bbb
(integer) 2
192.168.43.100:6379lpush y ccc
(integer) 3
192.168.43.100:6379lrange y 0 5
1) "ccc"
2) "bbb"
3) "aaa"
192.168.43.100:6379lrange y 2 2
1) "aaa"

Set(集合)

192.168.43.100:6379del y
(integer) 1
192.168.43.100:6379sadd y aaa
(integer) 1
192.168.43.100:6379sadd y bbb
(integer) 1
192.168.43.100:6379sadd y ccc
(integer) 1
192.168.43.100:6379sadd y ddd
(integer) 1
192.168.43.100:6379sadd y ddd
(integer) 1
192.168.43.100:6379smembers y
1) "ddd" #集群元素唯一性,第二次插入的元素将被忽略
2) "bbb"
3) "aaa"
4) "ccc"

zset(sorted set:有序集合)

192.168.43.100:6379> del y
(integer) 1
192.168.43.100:6379> zadd y aaa
(error) ERR wrong number of arguments for 'zadd' command
192.168.43.100:6379> zadd y 0 aaa
(integer) 1
192.168.43.100:6379> zadd y 2 bbb
(integer) 1
192.168.43.100:6379> zadd y 1 ccc
(integer) 1
192.168.43.100:6379> ZRANGEBYSCORE y 0 5
1"aaa"
2"ccc"
3"bbb"
192.168.43.100:6379>

各个数据类型应用场景

类型简介特性场景
String(字符串)二进制安全可以包含任何数据,比如jpg图片或者序列化的对象,一个键最大能存储512M---
Hash(字典)键值对集合,即编程语言中的Map类型适合存储对象,并且可以像数据库中update一个属性一样只修改某一项属性值(Memcached中需要取出整个字符串反序列化成对象修改完再序列化存回去)存储、读取、修改用户属性
List(列表)链表(双向链表)增删快,提供了操作某一段元素的API1,最新消息排行等功能(比如朋友圈的时间线) 2,消息队列
Set(集合)哈希表实现,元素不重复1、添加、删除,查找的复杂度都是O(1) 2、为集合提供了求交集、并集、差集等操作1、共同好友 2、利用唯一性,统计访问网站的所有独立ip 3、好友推荐时,根据tag求交集,大于某个阈值就可以推荐
Sorted Set(有序集合)将Set中的元素增加一个权重参数score,元素按score有序排列数据插入集合时,已经进行天然排序1、排行榜 2、带权重的消息队列


文章转载自叶同学专栏,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论