Redis基础知识详解(非原创)

文章大纲

一、Redis介绍
二、Redis安装并设置开机自动启动
三、Redis文件结构
四、Redis启动方式
五、Redis持久化
六、Redis配置文件详解
七、Redis图形化工具
八、Java之Jedis连接Redis单机
九、项目源码与资料下载
十、参考文章

 

一、Redis介绍

1. 什么是Redis

  Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。建议在linux上运行,它通过提供多种键值数据类型来适应不同场景下的存储需求,数据存储在内存中,也可持久化到磁盘中,目前为止Redis支持的键值数据类型如下:
(1)字符串类型
(2)散列类型
(3)列表类型
(4)集合类型
(5)有序集合类型

2. Redis特征

 

(1)Redis是把数据存在内存中,所以速度才会快。Redis是用C语言写的开源项目。
(2)Redis所有数据保存在内存中,对数据的更新将异步地保存到磁盘上,这样可以做到断电不丢失数据。
(3)Redis主从复制可以实现高可用和分布式

 

3. Redis数据结构

3.1 字符串

 

3.2 Hash

 
 

3.3 list

 
 

3.4 set

 

3.5 zset

 

4. Redis的应用场景

(1)缓存(数据查询、短连接、新闻内容、商品内容等等)(最多使用)

 

(2)分布式集群架构中的session分离
(3)聊天室的在线好友列表
(4)任务队列。(秒杀、抢购、12306等等)

 

(5)应用排行榜

 

(6)网站访问统计

 

(7)数据过期处理(可以精确到毫秒)

温馨提示:在使用场景中,不用考虑数据混乱因素,因为redis的增删查改是单线程执行的。

二、Redis安装并设置开机自动启动

  Redis的使用在Linux中效果会更佳,该文章主要体现教程,因此我以windows作为例子进行安装。

1. 安装

要安装Redis,首先要获取安装包。Windows的Redis安装包需要到以下GitHub链接找到。链接:https://github.com/MSOpenTech/redis。打开网站后,找到Release,点击前往下载页面。

 

在下载网页中,找到最后发行的版本(此处是3.2.100)。找到Redis-x64-3.2.100.msi和Redis-x64-3.2.100.zip,点击下载。这里说明一下,第一个是msi微软格式的安装包,第二个是压缩包

 

双击刚下载好的msi格式的安装包(Redis-x64-3.2.100.msi)开始安装。

 

选择“同意协议”,点击下一步继续。

 

选择“添加Redis目录到环境变量PATH中”,这样方便系统自动识别Redis执行文件在哪里。

 

端口号可保持默认的6379,并选择防火墙例外,从而保证外部可以正常访问Redis服务。

 

设定最大值为100M。作为实验和学习,100M足够了。

 

安装完毕后,需要先做一些设定工作,以便服务启动后能正常运行。使用文本编辑器,这里使用Notepad++,打开Redis服务配置文件。注意:不要找错了,通常为redis.windows-service.conf,而不是redis.windows.conf。后者是以非系统服务方式启动程序使用的配置文件。

 

找到含有requirepass字样的地方,追加一行,输入requirepass 147258qq。这是访问Redis时所需的密码,一般测试情况下可以不用设定密码。不过,即使是作为本地访问,也建议设定一个密码。此处以简单的147258qq来演示。

 

点击“开始”>右击“计算机”>选择“管理”。在左侧栏中依次找到并点击“计算机管理(本地)”>服务和应用程序>服务。再在右侧找到Redis名称的服务,查看启动情况。如未启动,则手动启动之。正常情况下,服务应该正常启动并运行了。

 

最后来测试一下Redis是否正常提供服务。进入Redis的目录,cd C:\Program Files\Redis。输入redis-cli并回车。(redis-cli是客户端程序)如图正常提示进入,并显示正确端口号,则表示服务已经启动。

 

使用服务前需要先通过密码验证。输入“auth 147258qq”并回车(12345是之前设定的密码)。返回提示OK表示验证通过。

 

实际测试一下读写。输入set mykey1 "I love you all!”并回车,用来保存一个键值。再输入get mykey1,获取刚才保存的键值。

 
 

2. 设置开机自动启动

设置服务命令:redis-server --service-install redis.windows-service.conf --loglevel verbose

 

输入命令之后没有报错,表示成功了,刷新服务,会看到多了一个redis服务。

 

右键Redis并选择属性

 

设置启动类型为自动

 

常用的redis服务命令。
卸载服务:redis-server --service-uninstall
开启服务:redis-server --service-start
停止服务:redis-server --service-stop

温馨提示
(1)Windows使用的这个Redis是64位版本的,32位操作系统的同学就不要折腾了。
(2)作为服务运行的Redis配置文件,通常为redis.windows-service.conf,而不是redis.windows.conf。小心不要选错了。如果修改了redis.windows.conf(非redis.windows-service.conf)文件上的配置,从服务自启动,配置的信息是不生效的,如密码配置和ip绑定。

三、Redis文件结构

 

四、Redis启动方式

  Redis有三种启动方式,具体如下:
(1)使用redis-server命令,会以默认的redis配置进行启动
(2)使用redis-server –port6379就可以使用动态参数配置进行启动
(3)使用redis-server configPath就可以使用配置文件方式进行启动
(4)当直接运行redis-service.exe时候,是没有使用配置文件的,而且会提示以下内容:

 
 

五、Redis持久化

1. 持久化作用

 

2. 持久化方式

 

3. RDB

3.1 什么是RDB

 

3.2 RDB文件生成方式

 

save方式

 

bgsave方式

 

Save与bgsave比较

 

自动生成RDB

 

3.3 RDB总结

 

4. AOF

4.1 RDB问题

 

因为RDB需要将全部数据生成RDB文件,所以这个过程比较耗时,如果用fork(bgsave)过程,则太消耗内容。如果RDB文件非常大,还会影响IO性能。
在T3-T4之间就会出现数据丢失。

 

4.2 AOF文件创建和恢复

创建时:

 

恢复时:

 

4.3 AOF三种策略

 

Always策略

 

Everysec策略
每秒写入一次数据,如果机器突然有问题,可能丢失一秒数据

 

No策略
根据操作系统策略自行选择

 

三种策略比较

 

4.4 AOF重写
把过期的,重复的,可优化命令进行化解。

 

重写作用

 

重写方式

 

Bgrewriteaof命令

 

AOF重写配置

 

AOF重写流程

 

5. RDB与AOF选择

 

6. Redis默认的持久化

  Redis默认的持久化方式是RDB,具体可看下图:

 

六、Redis配置文件详解

  Redis常用的配置文件在redis.windows-service.conf,具体配置包括设置登录密码、设置持久化方式、持久化路径、最大的内存空间、数据库数量、日志的等级、日志的路径、设置允许客户端连接的IP等,主从复制、高可用、集群、缓存等相关的功能将在下一篇进行讲解。

 
# redis 配置文件示例
 
# 当你需要为某个配置项指定内存大小的时候,必须要带上单位,
# 通常的格式就是 1k 5gb 4m 等酱紫:
#
# 1k  => 1000 bytes
# 1kb => 1024 bytes
# 1m  => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g  => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# 单位是不区分大小写的,你写 1K 5GB 4M 也行
 
################################## INCLUDES ###################################
 
# 假如说你有一个可用于所有的 redis server 的标准配置模板,
# 但针对某些 server 又需要一些个性化的设置,
# 你可以使用 include 来包含一些其他的配置文件,这对你来说是非常有用的。
#
# 但是要注意哦,include 是不能被 config rewrite 命令改写的
# 由于 redis 总是以最后的加工线作为一个配置指令值,所以你最好是把 include 放在这个文件的最前面,
# 以避免在运行时覆盖配置的改变,相反,你就把它放在后面(外国人真啰嗦)。
#
# include /path/to/local.conf
# include /path/to/other.conf
 
################################ 常用 #####################################
 
# 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把它改成 yes。
# 当redis作为守护进程运行的时候,它会写一个 pid 到 /var/run/redis.pid 文件里面。
daemonize no
 
# 当redis作为守护进程运行的时候,它会把 pid 默认写到 /var/run/redis.pid 文件里面,
# 但是你可以在这里自己制定它的文件位置。
pidfile /var/run/redis.pid
 
# 监听端口号,默认为 6379,如果你设为 0 ,redis 将不在 socket 上监听任何客户端连接。
port 6379
 
# TCP 监听的最大容纳数量
#
# 在高并发的环境下,你需要把这个值调高以避免客户端连接缓慢的问题。
# Linux 内核会一声不响的把这个值缩小成 /proc/sys/net/core/somaxconn 对应的值,
# 所以你要修改这两个值才能达到你的预期。
tcp-backlog 511
 
# 默认情况下,redis 在 server 上所有有效的网络接口上监听客户端连接。
# 你如果只想让它在一个网络接口上监听,那你就绑定一个IP或者多个IP。
#
# 示例,多个IP用空格隔开:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
 
# 指定 unix socket 的路径。
#
# unixsocket /tmp/redis.sock
# unixsocketperm 755
 
# 指定在一个 client 空闲多少秒之后关闭连接(0 就是不管它)
timeout 0
 
# tcp 心跳包。
#
# 如果设置为非零,则在与客户端缺乏通讯的时候使用 SO_KEEPALIVE 发送 tcp acks 给客户端。
# 这个之所有有用,主要由两个原因:
#
# 1) 防止死的 peers
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 60 seconds.
# 推荐一个合理的值就是60秒
tcp-keepalive 0
 
# 定义日志级别。
# 可以是下面的这些值:
# debug (适用于开发或测试阶段)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (适用于生产环境)
# warning (仅仅一些重要的消息被记录)
loglevel notice
 
# 指定日志文件的位置
logfile ""
 
# 要想把日志记录到系统日志,就把它改成 yes,
# 也可以可选择性的更新其他的syslog 参数以达到你的要求
# syslog-enabled no
 
# 设置 syslog 的 identity。
# syslog-ident redis
 
# 设置 syslog 的 facility,必须是 USER 或者是 LOCAL0-LOCAL7 之间的值。
# syslog-facility local0
 
# 设置数据库的数目。
# 默认数据库是 DB 0,你可以在每个连接上使用 select <dbid> 命令选择一个不同的数据库,
# 但是 dbid 必须是一个介于 0 到 databasees - 1 之间的值
databases 16
 
################################ 快照 ################################
#
# 存 DB 到磁盘:
#
#   格式:save <间隔时间(秒)> <写入次数>
#
#   根据给定的时间间隔和写入次数将数据保存到磁盘
#
#   下面的例子的意思是:
#   900 秒内如果至少有 1 个 key 的值变化,则保存
#   300 秒内如果至少有 10 个 key 的值变化,则保存
#   60 秒内如果至少有 10000 个 key 的值变化,则保存
#  
#   注意:你可以注释掉所有的 save 行来停用保存功能。
#   也可以直接一个空字符串来实现停用:
#   save ""
 
save 900 1
save 300 10
save 60 10000
 
# 默认情况下,如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,
# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,
# 否则就会没人注意到灾难的发生。
#
# 如果后台保存进程重新启动工作了,redis 也将自动的允许写操作。
#
# 然而你要是安装了靠谱的监控,你可能不希望 redis 这样做,那你就改成 no 好了。
stop-writes-on-bgsave-error yes
 
# 是否在 dump .rdb 数据库的时候使用 LZF 压缩字符串
# 默认都设为 yes
# 如果你希望保存子进程节省点 cpu ,你就设置它为 no ,
# 不过这个数据集可能就会比较大
rdbcompression yes
 
# 是否校验rdb文件
rdbchecksum yes
 
# 设置 dump 的文件位置
dbfilename dump.rdb
 
# 工作目录
# 例如上面的 dbfilename 只指定了文件名,
# 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。
dir ./
 
################################# 主从复制 #################################
 
# 主从复制。使用 slaveof 来让一个 redis 实例成为另一个reids 实例的副本。
# 注意这个只需要在 slave 上配置。
#
# slaveof <masterip> <masterport>
 
# 如果 master 需要密码认证,就在这里设置
# masterauth <master-password>
 
# 当一个 slave 与 master 失去联系,或者复制正在进行的时候,
# slave 可能会有两种表现:
#
# 1) 如果为 yes ,slave 仍然会应答客户端请求,但返回的数据可能是过时,
#    或者数据可能是空的在第一次同步的时候
#
# 2) 如果为 no ,在你执行除了 info he salveof 之外的其他命令时,
#    slave 都将返回一个 "SYNC with master in progress" 的错误,
#
slave-serve-stale-data yes
 
# 你可以配置一个 slave 实体是否接受写入操作。
# 通过写入操作来存储一些短暂的数据对于一个 slave 实例来说可能是有用的,
# 因为相对从 master 重新同步数而言,据数据写入到 slave 会更容易被删除。
# 但是如果客户端因为一个错误的配置写入,也可能会导致一些问题。
#
# 从 redis 2.6 版起,默认 slaves 都是只读的。
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
# 注意:只读的 slaves 没有被设计成在 internet 上暴露给不受信任的客户端。
# 它仅仅是一个针对误用实例的一个保护层。
slave-read-only yes
 
# Slaves 在一个预定义的时间间隔内发送 ping 命令到 server 。
# 你可以改变这个时间间隔。默认为 10 秒。
#
# repl-ping-slave-period 10
 
# The following option sets the replication timeout for:
# 设置主从复制过期时间
#
# 1) Bulk transfer I/O during SYNC, from the point of view of slave.
# 2) Master timeout from the point of view of slaves (data, pings).
# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).
#
# It is important to make sure that this value is greater than the value
# specified for repl-ping-slave-period otherwise a timeout will be detected
# every time there is low traffic between the master and the slave.
# 这个值一定要比 repl-ping-slave-period 大
#
# repl-timeout 60
 
# Disable TCP_NODELAY on the slave socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to slaves. But this can add a delay for
# the data to appear on the slave side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the slave side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and slaves are many hops away, turning this to "yes" may
# be a good idea.
repl-disable-tcp-nodelay no
 
# 设置主从复制容量大小。这个 backlog 是一个用来在 slaves 被断开连接时
# 存放 slave 数据的 buffer,所以当一个 slave 想要重新连接,通常不希望全部重新同步,
# 只是部分同步就够了,仅仅传递 slave 在断开连接时丢失的这部分数据。
#
# The biggest the replication backlog, the longer the time the slave can be
# disconnected and later be able to perform a partial resynchronization.
# 这个值越大,salve 可以断开连接的时间就越长。
#
# The backlog is only allocated once there is at least a slave connected.
#
# repl-backlog-size 1mb
 
# After a master has no longer connected slaves for some time, the backlog
# will be freed. The following option configures the amount of seconds that
# need to elapse, starting from the time the last slave disconnected, for
# the backlog buffer to be freed.
# 在某些时候,master 不再连接 slaves,backlog 将被释放。
#
# A value of 0 means to never release the backlog.
# 如果设置为 0 ,意味着绝不释放 backlog 。
#
# repl-backlog-ttl 3600
 
# 当 master 不能正常工作的时候,Redis Sentinel 会从 slaves 中选出一个新的 master,
# 这个值越小,就越会被优先选中,但是如果是 0 , 那是意味着这个 slave 不可能被选中。
#
# 默认优先级为 100。
slave-priority 100
 
# It is possible for a master to stop accepting writes if there are less than
# N slaves connected, having a lag less or equal than M seconds.
#
# The N slaves need to be in "online" state.
#
# The lag in seconds, that must be <= the specified value, is calculated from
# the last ping received from the slave, that is usually sent every second.
#
# This option does not GUARANTEES that N replicas will accept the write, but
# will limit the window of exposure for lost writes in case not enough slaves
# are available, to the specified number of seconds.
#
# For example to require at least 3 slaves with a lag <= 10 seconds use:
#
# min-slaves-to-write 3
# min-slaves-max-lag 10
#
# Setting one or the other to 0 disables the feature.
#
# By default min-slaves-to-write is set to 0 (feature disabled) and
# min-slaves-max-lag is set to 10.
 
################################## 安全 ###################################
 
# 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章