部署Redis

1、Redis 简介

Redis (Remote Dictionary Server远程字典服务)是一个遵循BSD MIT开源协议的高性能的NoSQL。Redis基于ANSI C语言语言)编写的key-value数据库,是意大利的Salvatore Sanfilippo在2009年发布,从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal公司赞助。目前国内外使用的公司众多,比如:阿里,腾讯,百度,京东,新浪微博,GitHub,Twitter 等。

Redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,Go, C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端。

DB-Engine月度排行榜Redis在键值型存储类的数据库长期居于首位,远远高于第二位的memcached
https://db-engines.com/en/ranking

Redis 官网地址
https://redis.io/
图片[1]-部署Redis-李佳程的个人主页

1.1、Redis 特性

  • 速度快: 10W QPS,基于内存,C语言实现
  • 单线程
  • 持久化
  • 支持多种数据结构
  • 支持多种编程语言
  • 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
  • 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
  • 主从复制
  • 支持高可用和分布式

1.2、单线程

Redis 6.0版本前一直是单线程方式处理用户的请求

图片[2]-部署Redis-李佳程的个人主页

单线程为何如此快?

  • 纯内存
  • 非阻塞
  • 避免线程切换和竞态消耗
图片[3]-部署Redis-李佳程的个人主页
  • 一次只运行一条命令
  • 避免执行长(慢)命令:keys *, flushall, flushdb, slow lua script, mutil/exec, operate big value(collection)
  • 其实不是单线程:早期版本是单进程单线程,3.0 版本后实际还有其它的线程,实现特定功能,如:fysncfile descriptor,close file descriptor

1.3、Redis 对比 Memcached

图片[4]-部署Redis-李佳程的个人主页

1.4、Redis 常见应用场景

  • 缓存:缓存RDBMS中数据,比如网站的查询结果、商品信息、微博、新闻、消息
  • Session 共享:实现Web集群中的多服务器间的session共享
  • 计数器:商品访问排行榜、浏览数、粉丝数、关注、点赞、评论等和次数相关的数值统计场景
  • 社交:朋友圈、共同好友、可能认识他们等
  • 地理位置:基于地理信息系统GIS(Geographic Information System)实现摇一摇、附近的人、外卖等功能
  • 消息队列:ELK等日志系统缓存、业务的订阅/发布系统
图片[5]-部署Redis-李佳程的个人主页

1.5、缓存的实现流程

数据更新操作流程:

图片[6]-部署Redis-李佳程的个人主页

数据读操作流程:

图片[7]-部署Redis-李佳程的个人主页

2、Redis 安装及连接

官方下载地址:
http://download.redis.io/releases/

2.1、yum安装 redis

配置yum源,直接安装,这里不过多介绍,有问题根据网站下方联系方式反馈

2.2、编译安装 Redis

# 安装依赖包
[root@redis01 ~]# yum -y install gcc make jemalloc-devel

# 如果支持systemd需要安装下面包
[root@redis01 ~]# yum -y install gcc jemalloc-devel systemd-devel

# 下载源码并解压
[root@redis01 ~]# wget http://download.redis.io/releases/redis-6.2.4.tar.gz
[root@redis01 ~]# tar xvf redis-6.2.4.tar.gz

# 编译安装
[root@redis01 ~]# cd redis-6.2.4/
[root@redis01 redis-6.2.4]# make -j 2 PREFIX=/apps/redis install

# 配置环境变量
[root@redis01 ~]# echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
[root@redis01 ~]# . /etc/profile.d/redis.sh

# 目录结构
[root@redis01 ~]# tree /apps/redis/
/apps/redis/
└── bin
    ├── redis-benchmark
    ├── redis-check-aof -> redis-server
    ├── redis-check-rdb -> redis-server
    ├── redis-cli
    ├── redis-sentinel -> redis-server
    └── redis-server
1 directory, 6 files

# 准备相关目录和配置文件
[root@redis01 ~]# mkdir /apps/redis/{etc,log,data,run}
[root@redis01 ~]# cp redis-6.2.4/redis.conf /apps/redis/etc/
# 前台启动 Redis
# redis-server 是 redis 服务器端的主程序
[root@redis01 ~]# redis-server --help
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-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

# 前台启动redis
[root@redis01 ~]# redis-server /apps/redis/etc/redis.conf
5978:C 23 Nov 2022 08:39:22.297 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5978:C 23 Nov 2022 08:39:22.297 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=5978, just started
5978:C 23 Nov 2022 08:39:22.297 # Configuration loaded
5978:M 23 Nov 2022 08:39:22.297 * Increased maximum number of open files to 10032 (it was originally set to 1024).
5978:M 23 Nov 2022 08:39:22.297 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5978
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'
5978:M 23 Nov 2022 08:39:22.298 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5978:M 23 Nov 2022 08:39:22.298 # Server initialized
5978:M 23 Nov 2022 08:39:22.298 # 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.
5978:M 23 Nov 2022 08:39:22.298 * Ready to accept connections

[root@redis01 ~]# ss -ntl
State       Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port
LISTEN      0      128                           127.0.0.1:6379                                              *:*
LISTEN      0      128                                   *:111                                               *:*
LISTEN      0      128                                   *:22                                                *:*
LISTEN      0      100                           127.0.0.1:25                                                *:*
LISTEN      0      128                               [::1]:6379                                           [::]:*
LISTEN      0      128                                [::]:111                                            [::]:*
LISTEN      0      128                                [::]:22                                             [::]:*
LISTEN      0      100                               [::1]:25                                             [::]:* 
# 开启 Redis 多实例
[root@redis01 ~]# redis-server /apps/redis/etc/redis.conf --port 6380
[root@redis01 ~]# redis-server /apps/redis/etc/redis.conf --port 6381

[root@redis01 ~]# ss -ntl
State       Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port
LISTEN      0      128                           127.0.0.1:6379                                              *:*
LISTEN      0      128                           127.0.0.1:6380                                              *:*
LISTEN      0      128                           127.0.0.1:6381                                              *:*
LISTEN      0      128                                   *:111                                               *:*
LISTEN      0      128                                   *:22                                                *:*
LISTEN      0      100                           127.0.0.1:25                                                *:*
LISTEN      0      128                               [::1]:6379                                           [::]:*
LISTEN      0      128                               [::1]:6380                                           [::]:*
LISTEN      0      128                               [::1]:6381                                           [::]:*
LISTEN      0      128                                [::]:111                                            [::]:*
LISTEN      0      128                                [::]:22                                             [::]:*
LISTEN      0      100                               [::1]:25                                             [::]:*

[root@redis01 ~]# ps -ef | grep redis
root       6060   1443  0 08:40 pts/0    00:00:00 redis-server 127.0.0.1:6379
root       6136   1443  0 08:42 pts/0    00:00:00 redis-server 127.0.0.1:6380
root       6157   1443  0 08:42 pts/0    00:00:00 redis-server 127.0.0.1:6381
root       6180   1477  0 08:42 pts/1    00:00:00 grep --color=auto redis

[root@redis01 ~]# redis-cli -p 6380
127.0.0.1:6380> exit
[root@redis01 ~]# redis-cli -p 6381
127.0.0.1:6381> exit
# 消除启动时的两个Warning提示信息(可选)
# 前面直接启动Redis时有两个Waring信息,可以用下面方法消除告警,但非强制消除

# WARNING: The TCP backlog setting of 511 cannot be enforced because
/proc/sys/net/core/somaxconn is set to the lower value of 128.
# Tcp backlog 是指TCP的第三次握手服务器端收到客户端 ack确认号之后到服务器用Accept函数处理请求前的队列长度,即全连接队列
# 处理方法:
[root@redis01 ~]# vim /etc/sysctl.conf
net.core.somaxconn = 1024
[root@redis01 ~]# sysctl -p


# 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.
# 内核参数overcommit_memory 实现内存分配策略,可选值有三个:0、1、2
# 0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则内存
申请失败,并把错误返回给应用进程
#1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何
#2 表示内核允许分配超过所有物理内存和交换空间总和的内存
# 处理方法:
[root@redis01 ~]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@redis01 ~]# sysctl -p


# 验证
[root@redis01 ~]# redis-server /apps/redis/etc/redis.conf
6603:C 23 Nov 2022 08:51:01.234 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6603:C 23 Nov 2022 08:51:01.234 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=6603, just started
6603:C 23 Nov 2022 08:51:01.234 # Configuration loaded
6603:M 23 Nov 2022 08:51:01.234 * Increased maximum number of open files to 10032 (it was originally set to 1024).
6603:M 23 Nov 2022 08:51:01.234 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6603
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

6603:M 23 Nov 2022 08:51:01.235 # Server initialized
6603:M 23 Nov 2022 08:51:01.235 * Loading RDB produced by version 6.2.4
6603:M 23 Nov 2022 08:51:01.235 * RDB age 619 seconds
6603:M 23 Nov 2022 08:51:01.235 * RDB memory usage when created 0.77 Mb
6603:M 23 Nov 2022 08:51:01.235 * DB loaded from disk: 0.000 seconds
6603:M 23 Nov 2022 08:51:01.235 * Ready to accept connections
# 创建 Redis 用户和设置数据目录权限
[root@redis01 ~]# useradd -r -s /sbin/nologin redis
[root@redis01 ~]# chown -R redis.redis /apps/redis
# 创建 Redis 服务 Service 文件
[root@redis01 ~]# vim /lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target
# Redis 通过Service方式启动
[root@redis01 ~]# systemctl daemon-reload
[root@redis01 ~]# systemctl start redis.service
[root@redis01 ~]# systemctl status  redis.service
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-11-23 08:59:23 CST; 8s ago
 Main PID: 7064 (redis-server)
   Status: "Ready to accept connections"
   CGroup: /system.slice/redis.service
           └─7064 /apps/redis/bin/redis-server 127.0.0.1:6379

Nov 23 08:59:23 redis01 systemd[1]: Starting Redis persistent key-value database...
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:C 23 Nov 2022 08:59:23.224 * Supervised by systemd. Please make sure you ... unit.
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:C 23 Nov 2022 08:59:23.224 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:C 23 Nov 2022 08:59:23.224 # Redis version=6.2.4, bits=64, commit=0000000...tarted
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:C 23 Nov 2022 08:59:23.224 # Configuration loaded
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:M 23 Nov 2022 08:59:23.224 * monotonic clock: POSIX clock_gettime
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:M 23 Nov 2022 08:59:23.225 * Running mode=standalone, port=6379.
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:M 23 Nov 2022 08:59:23.225 # Server initialized
Nov 23 08:59:23 redis01 redis-server[7064]: 7064:M 23 Nov 2022 08:59:23.225 * Ready to accept connections
Nov 23 08:59:23 redis01 systemd[1]: Started Redis persistent key-value database.
Hint: Some lines were ellipsized, use -l to show in full.

[root@redis01 ~]# ss -ntl
State       Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port
LISTEN      0      511                           127.0.0.1:6379                                              *:*
LISTEN      0      128                                   *:111                                               *:*
LISTEN      0      128                                   *:22                                                *:*
LISTEN      0      100                           127.0.0.1:25                                                *:*
LISTEN      0      511                               [::1]:6379                                           [::]:*
LISTEN      0      128                                [::]:111                                            [::]:*
LISTEN      0      128                                [::]:22                                             [::]:*
LISTEN      0      100                               [::1]:25                                             [::]:*
 # 验证客户端连接 Redis
redis-cli -h IP/HOSTNAME -p PORT -a PASSWORD

[root@redis01 ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
# Server
redis_version:6.2.4
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:bd81f2a8f98c502c
redis_mode:standalone
os:Linux 3.10.0-1160.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:7064
process_supervised:systemd
run_id:ad7e099954a7b2dc094efbb436a991714a0e5bfc
tcp_port:6379
server_time_usec:1669165358152728
uptime_in_seconds:195
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:8220974
executable:/apps/redis/bin/redis-server
config_file:/apps/redis/etc/redis.conf
io_threads_active:0
# 一键编译安装Redis脚本
[root@redis02 ~]# cat install_redis.sh
#!/bin/bash

REDIS_VERSION=redis-6.2.5
PASSWORD=123456
INSTALL_DIR=/apps/redis
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

. /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $" OK "
    elif [ $2 = "failure" -o $2 = "1" ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

prepare(){
    if [ $ID = "centos" ];then
       yum  -y install gcc make jemalloc-devel systemd-devel
    else
       apt update
       apt -y install  gcc make libjemalloc-dev libsystemd-dev
    fi
    if [ $? -eq 0 ];then
       color "安装软件包成功"  0
    else
       color "安装软件包失败,请检查网络配置" 1
       exit
    fi
}
install() {
    if [ ! -f ${REDIS_VERSION}.tar.gz ];then
        wget http://download.redis.io/releases/${REDIS_VERSION}.tar.gz || { color "Redis 源码下载失败" 1 ; exit; }
    fi
    tar xf ${REDIS_VERSION}.tar.gz
    cd ${REDIS_VERSION}
    make -j $CUPS USE_SYSTEMD=yes PREFIX=${INSTALL_DIR} install && color "Redis 编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }

    ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/

    mkdir -p ${INSTALL_DIR}/{etc,log,data,run}

    cp redis.conf  ${INSTALL_DIR}/etc/
    sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/'  -e "/# requirepass/a requirepass $PASSWORD"  -e "/^dir .*/c dir ${INSTALL_DIR}/data/"  -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log"  -e  "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf

    if id redis &> /dev/null ;then
         color "Redis 用户已存在" 1
    else
         useradd -r -s /sbin/nologin redis
         color "Redis 用户创建成功" 0
    fi

    chown -R redis.redis ${INSTALL_DIR}

    cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
   sysctl -p
    if [ $ID = "centos" ];then
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
        chmod +x /etc/rc.d/rc.local
        /etc/rc.d/rc.local
    else
        echo -e '#!/bin/bash\necho never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
        chmod +x /etc/rc.local
        /etc/rc.local
    fi


cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target

EOF

     systemctl daemon-reload
     systemctl enable --now redis &> /dev/null
     if [ $? -eq 0 ];then
         color "Redis 服务启动成功,Redis信息如下:"  0
     else
         color "Redis 启动失败" 1
         exit
     fi
     sleep 2
     redis-cli -a $PASSWORD INFO Server 2> /dev/null
}

prepare
install

2.3、Redis 相关工具和客户端连接

https://redis.io/clients

图形工具

有一些第三方开发的图形工具也可以连接redis,比如: RedisDesktopManager

图片[8]-部署Redis-李佳程的个人主页
图片[9]-部署Redis-李佳程的个人主页
图片[10]-部署Redis-李佳程的个人主页

# 客户端程序 redis-cli

#默认为本机无密码连接
redis-cli
# 远程客户端连接,注意:Redis没有用户的概念
redis-cli -h <Redis服务器IP> -p <PORT> -a <PASSWORD>
# shell 脚本访问 Redis
[root@redis02 ~]# cat redis_test.sh
#!/bin/bash
NUM=100
PASS=123456
for i in `seq $NUM`;do
    redis-cli -h 127.0.0.1 -a "$PASS"  --no-auth-warning  set key${i} value${i}
    echo "key${i} value${i} 写入完成"
done
echo "$NUM个key写入完成"
# python 程序连接
# 使用redis-py 库连接 Redis
[root@redis02 ~]# yum -y install python3 python3-redis
[root@redis02 ~]# cat redis_test.py
#!/usr/bin/python3
import redis
pool = redis.ConnectionPool(host="127.0.0.1",port=6379,password="123456",decode_responses=True)
c = redis.Redis(connection_pool=pool)
for i in range(100):
 c.set("k%d" % i,"v%d" % i)
 data=c.get("k%d" % i)
 print(data)

[root@redis02 ~]# python3 redis_test.py
v0
v1
v2
v3
v4
v5
v6
v7
......

[root@redis02 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get k10
"v10"
127.0.0.1:6379>

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享