(一)Redis安装与概述

参考资料 Redis中文

概述

​ Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings)散列(hashes)列表(lists)集合(sets)有序集合(sorted sets) 与范围查询, bitmapshyperloglogs地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication)LUA脚本(Lua scripting)LRU驱动事件(LRU eviction)事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

安装

Redis 的安装可参考其 GitHub 中的 README.md 文档

或可直接从其英文官网 redis.io 下载最新安装包

# 安装 wget [-y: auto say yes]
yum install -y wget
# 拉取 Redis 最新的安装包
wget https://download.redis.io/releases/redis-6.2.0.tar.gz
# 安装 gcc [Redis 需要用C编译]
yum install -y gcc
# 这里可能因为 gcc 版本过低导致 make 失败,需升级 gcc 版本
cd redis-6.2.0
# 编译
make 
# 安装
make install

# 如果 make 失败,需要清理上次执行失败的目录后再次 make
make distclean

配置 Redis 环境变量(这里是将安装后生成的一系列 redis 命令导入到系统中,后续可以在任何位置使用 redis-server & redis-cli 等),后面可以使用 service redis start 来管理 Redis 服务

# vi /etc/profile
增加如下内容 & 保存退出
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
# source /etc/profile

安装 redis-server

# 进入 utils目录,执行 install_server.sh
cd redis-6.2.0/utils
./install_server.sh

安装日志:

Welcome to the redis service installer
This script will help you easily set up a running redis server

# 指定一个端口号,默认 6379
Please select the redis port for this instance: [6379] 
Selecting default: 6379
# 指定了端口号之后,会默认生成的配置文件
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
# 指定了端口号之后,会默认生成的日志文件
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
# 指定了端口号之后,会默认生成的数据文件
# dump.rdb & appendonly.aof 文件会保存在这里
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
# 指定 redis-server 的安装目录
Please select the redis executable path [/usr/local/redis/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

​ 一台机器可以安装多个 redis 实例,他们的安装也很简单,只需要重复执行上述安装命令 ./install_server.sh,并指定不同的端口即可,安装后的多个 redis 实例默认是相互隔离的,可以通过配置使其成为集群或者主从关系。

系统命令启动

# systemctl stop redis_6379
# systemctl status redis_6379
# systemctl start redis_6379

Redis 的数据类型

​ 首先,Redis 是二进制安全的,不仅可以保存文本数据,还可以保存任意格式的二进制数据。Redis 保存中文占用几个字节,取决于客户端「redis-cli」使用的是何种字符编码。

​ Redis 底层是使用 C语言实现的,C语言中,使用 ‘\0’ 来代表字符串的结束。但在 Redis 中,使用**简单动态字符串 (Simple Dynamic String)**来存储字符串。不再以 ‘\0’ 来标识一个字符串的结束。

​ Redis 是一个 Key - Value 型的内存数据库,其支持多种数据类型。(这里的数据类型指的是 Value 的类型)

  • String 可代表以下类型

    • 字符数组
    • 数值类型
    • bitmap 位图
  • Hash 类似 java 中的对象 / JSON

  • List 简单集合

  • Set 去重的集合

  • Sorted Set 提供排序的去重集合

与一个更古老的内存型 Key - Value 数据系统 Memcached 相比(Memcached 的 Value 没有类型的概念,使用通用的 JSON 形式的数据存储),Redis 因为有数据类型的概念,所以可以提供基于不同数据类型的用于计算的方法。称为 计算向数据移动

Redis 配置文件 Gloable

​ 这里只介绍配置文件中的一些全局配置部分

从安装信息我们知道,Redis默认的配置文件位于 /etc/redis/6379.conf,以下配置文件来源于 redis-6.2.0

################################## INCLUDES ###################################

# Include one or more other config files here.  This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
#
# Note that option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf

# 引入其他配置文件
# 通常配置中,我们有一个公共的配置文件,然后又根据不同的 redis 实例存在差异化配置。
# 如果 include 放在第一行,本文件中配置的优先级高于 include 中配置的优先级。
# 如果 include 放在最后一行,本文件中配置的优先级低于 include 中配置的优先级。
include /path/to/local.conf

################################## MODULES #####################################

# Load modules at startup. If the server is not able to load modules
# it will abort. It is possible to use multiple loadmodule directives.
#
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so

# redis 是模块化的,可以通过装载 modules 实现灵活的功能
# 启动时,装载其他 module, 一个常见的 module 是 redisbloom
loadmodule /path/to/my_module.so

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interfece. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 设置 允许访问的来源 ip地址,’-‘ 前缀代表如果后面的地址不可用,将不会导致 redis启动失败。
# ::1 是 IPv6 的回环地址
# redis 默认只允许本机的客户端访问,因为允许所有来源 ip 访问是十分危险的。
bind 127.0.0.1 -::1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.

# Unix domain socket, 用于本机进程之间的 socket通信,直接将应用层数据拷贝给另一个进程。可靠的,消息既不会丢失也不会顺序错乱

#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.

# redis 有一个保护模式, 如果没有配置 bind (未配置等同于允许所有来源ip), 并且没有设置密码, 那么 redis 将只会允许来自回环地址的访问。
protected-mode yes

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
# 端口号
port 6379

TCP 相关

# TCP listen() backlog.
#
# In high requests-per-second environments you need a high backlog in order
# to avoid slow clients connection issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /run/redis.sock
# unixsocketperm 700

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Force network equipment in the middle to consider the connection to be
#    alive.
#
# 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 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis是一款开源的内存键值数据库,它支持多种数据结构,如字符串、哈希、列表、集合和有序集合等。Redis6是Redis的最新版本,它在之前版本的基础上增加了许多新特性和功能。下面是Redis6的概述和详细介绍。 概述: 1. 支持多线程:Redis6引入了多线程支持,可以显著提高Redis的性能,特别是在多核CPU上。 2. 支持异步IO:Redis6支持异步IO,这意味着Redis可以同时处理多个客户端请求,并且不必等待每个请求完成。 3. 支持协程:Redis6支持协程,这意味着Redis可以更好地利用CPU和内存,以提高性能和吞吐量。 4. 改进了集群模式:Redis6改进了集群模式,增加了更多的功能和可扩展性,可以更好地应对大规模应用。 5. 支持TLS:Redis6支持TLS,这意味着Redis可以更好地保护数据的安全性和隐私性。 详细介绍: 1. 多线程支持:Redis6引入了多线程支持,大大提高了Redis的性能,特别是在多核CPU上。Redis6使用了一种叫做“多线程I/O复用”的技术,可以将多个客户端请求并行处理,从而提高Redis的吞吐量。 2. 异步IO支持:Redis6支持异步IO,这意味着Redis可以同时处理多个客户端请求,并且不必等待每个请求完成。这种机制可以提高Redis的性能和吞吐量,特别是在高并发场景下。 3. 协程支持:Redis6支持协程,这意味着Redis可以更好地利用CPU和内存,以提高性能和吞吐量。协程是一种轻量级的线程,可以在不同的任务之间快速切换,从而提高系统的并发能力和效率。 4. 集群模式改进:Redis6改进了集群模式,增加了更多的功能和可扩展性,可以更好地应对大规模应用。Redis6的集群模式支持动态扩容和缩容,可以自动发现和管理节点,从而提高集群的可用性和可靠性。 5. TLS支持:Redis6支持TLS,这意味着Redis可以更好地保护数据的安全性和隐私性。TLS是一种安全传输协议,可以保护数据在传输过程中的机密性和完整性,从而避免数据被篡改或泄露。Redis6的TLS支持可以为企业和用户提供更高的安全保障。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值