Redis单实例安装
Redis(Remote Dictionary Server)是一个开源的内存数据库,遵守 BSD 协议,它提供了一个高性能的键值(key-value)存储系统,常用于缓存、消息队列、会话存储等应用场景。
- 性能极高:Redis 以其极高的性能而著称,能够支持每秒数十万次的读写操作24。这使得Redis成为处理高并发请求的理想选择,尤其是在需要快速响应的场景中,如缓存、会话管理、排行榜等。
- 丰富的数据类型:Redis 不仅支持基本的键值存储,还提供了丰富的数据类型,包括字符串、列表、集合、哈希表、有序集合等。这些数据类型为开发者提供了灵活的数据操作能力,使得Redis可以适应各种不同的应用场景。
- 原子性操作:Redis 的所有操作都是原子性的,这意味着操作要么完全执行,要么完全不执行。这种特性对于确保数据的一致性和完整性至关重要,尤其是在高并发环境下处理事务时。
- 持久化:Redis 支持数据的持久化,可以将内存中的数据保存到磁盘中,以便在系统重启后恢复数据。这为 Redis 提供了数据安全性,确保数据不会因为系统故障而丢失。
- 支持发布/订阅模式:Redis 内置了发布/订阅模式(Pub/Sub),允许客户端之间通过消息传递进行通信。这使得 Redis 可以作为消息队列和实时数据传输的平台。
- 单线程模型:尽管 Redis 是单线程的,但它通过高效的事件驱动模型来处理并发请求,确保了高性能和低延迟。单线程模型也简化了并发控制的复杂性。
- 主从复制:Redis 支持主从复制,可以通过从节点来备份数据或分担读请求,提高数据的可用性和系统的伸缩性。
- 应用场景广泛:Redis 被广泛应用于各种场景,包括但不限于缓存系统、会话存储、排行榜、实时分析、地理空间数据索引等。
- 社区支持:Redis 拥有一个活跃的开发者社区,提供了大量的文档、教程和第三方库,这为开发者提供了强大的支持和丰富的资源。
- 跨平台兼容性:Redis 可以在多种操作系统上运行,包括 Linux、macOS 和 Windows,这使得它能够在不同的技术栈中灵活部署。
安装编译环境
代码语言:javascript复制# ubuntu
apt install make gcc
# centos
yum install make gcc
安装 Redis
代码语言:javascript复制# 查看 Redis 版本
http://download.redis.io/releases/
# 下载 Redis
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解压
tar xvf redis-7.2.5.tar.gz
cd redis-7.2.5/
# 进行编译
make && make install
配置服务
代码语言:javascript复制cat << EOF > /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
配置停止脚本
代码语言:javascript复制mkdir /usr/local/redis
vim /usr/local/redis/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsockets/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
授权启动服务
代码语言:javascript复制chmod x /usr/local/redis/redis-shutdown
useradd -s /sbin/nologin redis
cp /root/redis-7.2.5/redis.conf /usr/local/redis/ && chown -R redis:redis /usr/local/redis
mkdir -p /usr/local/redis/data && chown -R redis:redis /usr/local/redis/data
修改配置
代码语言:javascript复制bind 0.0.0.0 -::1 # 监听ip,多个ip用空格分隔
daemonize yes # 允许后台启动
logfile "/usr/local/redis/redis.log" # 日志路径
dir /usr/local/redis/data # 数据库备份文件存放目录
requirepass 123123 # 设置连接密码
appendonly yes # 在/usr/local/redis/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中
修改linux内核参数
代码语言:javascript复制# 临时生效
sysctl -w vm.overcommit_memory=1
# 永久生效
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf && sysctl -p
### 可选值:0,1,2。
# 0,:表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
# 1:表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
# 2: 表示内核允许分配超过所有物理内存和交换空间总和的内存。
启动 Redis
代码语言:javascript复制systemctl daemon-reload
systemctl enable redis
systemctl start redis
systemctl status redis
查看集群
代码语言:javascript复制# 交互式
redis-cli -h 192.168.1.21 -a 123123
192.168.1.21:6379> info replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:9d6563f8b2cf7300bc82890838b877eceae2d8bf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.1.21:6379>
# 交互式
redis-cli -h 192.168.1.21
192.168.1.21:6379>
192.168.1.21:6379> info replication
NOAUTH Authentication required.
192.168.1.21:6379>
192.168.1.21:6379>
192.168.1.21:6379> auth 123123
OK
192.168.1.21:6379>
192.168.1.21:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:9d6563f8b2cf7300bc82890838b877eceae2d8bf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.1.21:6379>
192.168.1.21:6379>
# 非交互式
redis-cli -h 192.168.1.21 -a 123123 info replication
压测
代码语言:javascript复制root@cby:~# redis-benchmark -t set,get -n 100000 -a 123123 -h 192.168.1.21
====== SET ======
100000 requests completed in 0.85 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": yes
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.095 milliseconds (cumulative count 13)
50.000% <= 0.287 milliseconds (cumulative count 52749)
75.000% <= 0.343 milliseconds (cumulative count 77482)
87.500% <= 0.367 milliseconds (cumulative count 88051)
93.750% <= 0.383 milliseconds (cumulative count 94598)
96.875% <= 0.399 milliseconds (cumulative count 97691)
98.438% <= 0.407 milliseconds (cumulative count 98450)
99.219% <= 0.423 milliseconds (cumulative count 99272)
99.609% <= 0.455 milliseconds (cumulative count 99612)
99.805% <= 0.599 milliseconds (cumulative count 99816)
99.902% <= 0.911 milliseconds (cumulative count 99903)
99.951% <= 1.039 milliseconds (cumulative count 99952)
99.976% <= 1.303 milliseconds (cumulative count 99977)
99.988% <= 1.343 milliseconds (cumulative count 99988)
99.994% <= 1.367 milliseconds (cumulative count 99995)
99.997% <= 1.375 milliseconds (cumulative count 99997)
99.998% <= 1.383 milliseconds (cumulative count 99999)
99.999% <= 1.391 milliseconds (cumulative count 100000)
100.000% <= 1.391 milliseconds (cumulative count 100000)
Cumulative distribution of latencies:
0.016% <= 0.103 milliseconds (cumulative count 16)
13.574% <= 0.207 milliseconds (cumulative count 13574)
59.956% <= 0.303 milliseconds (cumulative count 59956)
98.450% <= 0.407 milliseconds (cumulative count 98450)
99.708% <= 0.503 milliseconds (cumulative count 99708)
99.825% <= 0.607 milliseconds (cumulative count 99825)
99.868% <= 0.703 milliseconds (cumulative count 99868)
99.877% <= 0.807 milliseconds (cumulative count 99877)
99.899% <= 0.903 milliseconds (cumulative count 99899)
99.938% <= 1.007 milliseconds (cumulative count 99938)
99.966% <= 1.103 milliseconds (cumulative count 99966)
99.967% <= 1.207 milliseconds (cumulative count 99967)
99.977% <= 1.303 milliseconds (cumulative count 99977)
100.000% <= 1.407 milliseconds (cumulative count 100000)
Summary:
throughput summary: 117508.81 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.285 0.088 0.287 0.391 0.423 1.391
====== GET ======
100000 requests completed in 0.80 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": yes
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.039 milliseconds (cumulative count 1)
50.000% <= 0.255 milliseconds (cumulative count 51084)
75.000% <= 0.311 milliseconds (cumulative count 76787)
87.500% <= 0.343 milliseconds (cumulative count 90043)
93.750% <= 0.359 milliseconds (cumulative count 95251)
96.875% <= 0.375 milliseconds (cumulative count 97337)
98.438% <= 0.391 milliseconds (cumulative count 98520)
99.219% <= 0.415 milliseconds (cumulative count 99259)
99.609% <= 0.519 milliseconds (cumulative count 99611)
99.805% <= 0.639 milliseconds (cumulative count 99808)
99.902% <= 0.911 milliseconds (cumulative count 99903)
99.951% <= 1.895 milliseconds (cumulative count 99952)
99.976% <= 1.991 milliseconds (cumulative count 99977)
99.988% <= 2.031 milliseconds (cumulative count 99988)
99.994% <= 2.055 milliseconds (cumulative count 99994)
99.997% <= 2.071 milliseconds (cumulative count 99998)
99.998% <= 2.079 milliseconds (cumulative count 100000)
100.000% <= 2.079 milliseconds (cumulative count 100000)
Cumulative distribution of latencies:
0.052% <= 0.103 milliseconds (cumulative count 52)
27.094% <= 0.207 milliseconds (cumulative count 27094)
73.309% <= 0.303 milliseconds (cumulative count 73309)
99.140% <= 0.407 milliseconds (cumulative count 99140)
99.577% <= 0.503 milliseconds (cumulative count 99577)
99.780% <= 0.607 milliseconds (cumulative count 99780)
99.832% <= 0.703 milliseconds (cumulative count 99832)
99.855% <= 0.807 milliseconds (cumulative count 99855)
99.899% <= 0.903 milliseconds (cumulative count 99899)
99.933% <= 1.007 milliseconds (cumulative count 99933)
99.938% <= 1.207 milliseconds (cumulative count 99938)
99.947% <= 1.407 milliseconds (cumulative count 99947)
99.950% <= 1.503 milliseconds (cumulative count 99950)
99.954% <= 1.903 milliseconds (cumulative count 99954)
99.981% <= 2.007 milliseconds (cumulative count 99981)
100.000% <= 2.103 milliseconds (cumulative count 100000)
Summary:
throughput summary: 125628.14 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.259 0.032 0.255 0.359 0.407 2.079
root@cby:~#
关于 https://www.oiox.cn/ https://www.oiox.cn/index.php/start-page.html CSDN、GitHub、51CTO、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客