Loading

Cache-Redis-01-Redis

前提条件

CentOS7:2009

环境

Redis:3.2.12

安装

在线安装

yum install -y epel-release
# yum search redis
# yum info redis
yum install -y redis

# 验证安装成功
redis-server --version
redis-cli --version

配置

备份原配置文件

cp /etc/redis.conf /etc/redis-origin.conf

密码

# 准备加密后的密码
echo "digital-ocean" | sha256sum
echo "digital-ocean" | sha1sum

# 设置Redis客户端连接密码
sed -i 's/^# requirepass foobared/requirepass <Password>/' /etc/redis.conf

权限

# chown redis:redis /var/lib/redis
# chmod 700 /var/lib/redis
# chown redis:redis /etc/redis.conf
# chmod 600 /etc/redis.conf

优化

TCP最大连接数警告

# WARNING: The TCP backlog setting of 511 cannot be enforced
because /proc/sys/net/core/somaxconn is set to the lower value of 128.

将 backlog 连接的最大值更改为高于 redis.conf 的 tcp-backlog 选项的值,默认为 511

# 设置当前生效,重启后失效
sysctl -w net.core.somaxconn=512
# 开机自动设置该配置
echo net.core.somaxconn=512 >> /etc/sysctl.conf

# 验证当前已生效
cat /proc/sys/net/core/somaxconn
# 验证配置文件已写入,以便开机启动能生效该配置
cat /etc/sysctl.conf | grep net.core.somaxconn

overcommit_memory内存警告

# 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.

Official Redis Administration Guide 官方推荐执行以下命令

# 设置当前生效,重启后失效
sysctl vm.overcommit_memory=1
# 开机自动设置该配置
echo vm.overcommit_memory=1 >> /etc/sysctl.conf

# 验证当前已生效
cat /proc/sys/vm/overcommit_memory
# 验证配置文件已写入,以便开机启动能生效该配置
cat /etc/sysctl.conf | grep vm.overcommit_memory

透明大页警告

# 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.

Official Redis Administration Guide官方推荐禁用透明大页Transparent Huge Pages (THP)

echo never > /sys/kernel/mm/transparent_hugepage/enabled
#echo never > /sys/kernel/mm/transparent_hugepage/defrag

# 添加命令到开机启动脚本
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'>> /etc/rc.local
# 给开机启动脚本/etc/rc.d/rc.local执行权限,否则开机无法执行命令
chmod +x /etc/rc.d/rc.local

监听IP地址

# 注意!172.17.0.1需要安装docker以后才配置 否则redis会因为不存在该IP而启动失败
# 只绑定127.0.0.1回环地址 docker内部访问IP 网卡中本机IP,这样更安全
sed -i 's/^bind 127.0.0.1/bind 127.0.0.1 172.17.0.1 <IPAddress>/' /etc/redis.conf

防火墙

# 防火墙允许外网对6379端口访问
firewall-cmd --permanent --zone=public --add-port=6379/tcp
firewall-cmd --reload

# 防火墙只允许某些私有IP访问6379端口访问
# firewall-cmd --permanent --new-zone=redis
# firewall-cmd --permanent --zone=redis -add-port=6379/tcp
# firewall-cmd --permanent --zone=redis -add-source=192.168.121.0/24
# firewall-cmd --reload

开机启动

# 设置自动启动,安装后默认为自动启动
systemctl enable redis
# 查看是否为自动启动
systemctl is-enabled redis

启动

# 启动服务
systemctl start redis

# 获取服务状态,确认服务已启动
systemctl status redis

验证配置

验证密码

# 验证密码
# 以下命令应该失败
redis-cli
# 127.0.0.1:6379> 中输入如下命令
set key1 10
# 期望输出以下内容
# (error) NOAUTH Authentication required.

# 127.0.0.1:6379> 中输入如下命令
auth <Password>
# 期望输出以下内容
# OK
set key1 10
# 期望输出以下内容
# OK

# 退出redis交互界面
quit

验证监听IP地址

# 验证监听IP
ss -an | grep 6379

安全

重命名危险命令,防止误操作

redis-cli
# 127.0.0.1:6379> 中输入如下命令
rename-command PEXPIRE
rename-command DEL
rename-command BGREWRITEAOF
rename-command BGSAVE
rename-command SAVE
rename-command SPOP
rename-command SREM
rename-command RENAME
# 客户端可修改Redis配置
rename-command CONFIG ""
# 保存当前的rdb文件,并清空当前数据库,重新加载rdb
rename-command DEBUG ""
# 客户端可查询出所有存在的键
rename-command KEYS ""
# 删除当前所选数据库的所有键。此命令永远不会失败
rename-command FLUSHDB ""
# 删除所有现有数据库的所有键,而不仅仅是当前选定的数据库。此命令永远不会失败
rename-command FLUSHALL ""
# 关闭Redis服务
rename-command SHUTDOWN ""

测试

Local Benchmark Test

redis-benchmark -q -n 100000

日志

cat /var/log/redis/redis.log

卸载

yum remove -y redis

调试

journalctl -xe

常用配置

daemonize 如果需要将Redis服务以守护进程在后台运行,则把该项的值改为yes
pidfile 配置多个pid的地址,默认在/var/run/redis/pid
bind 绑定ip,设置后只接受来自该ip的请求
port 监听端口,默认是6379
timeout 客户端连接超时的设定,单位是秒
loglevel 分为4级,debug、verbose、notice、warning
logfile 配置log文件地址
databases 设置数据库的个数,默认使用的数据库为0
save 设置redis进行数据库镜像的频率
rdbcompression 在进行镜像备份时,是否进行压缩
Dbfilename 镜像备份文件的文件名
Dir 数据库镜像备份文件的存放路径
Slaveof 设置数据库为其他数据库的从数据库
Masterauth 主数据库连接需要的密码验证
Requirepass 设置登录时,需要使用的密码
Maxclients 设置同时连接的最大客户端数量
Maxmemory 设置redis能够使用的最大内存
Appendonly 开启append only模式
Appendfsync 设置对appendonly.aof文件同步的频率
vm-enabled 是否开启虚拟内存支持
vm-swap-file 设置虚拟内存的交换文件路径
vm-max-memory 设置redis能够使用的最大虚拟内存
vm-page-size 设置虚拟内存的页大小
vm-pages 设置交换文件的总的page数量
vm-max-threads 设置VMIO同时使用的线程数量
Glueoutputbuf 把小的输出缓存存放在一起
hash-max-zipmap-entries 设置hash的临界值
Activerehashing 重新hash

FAQ

Q1: Redis日志包含以下内容

# You requested maxclients of 10000 requiring at least 10032 max file descriptors.
# Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
# Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.

A1: 执行以下命令

sysctl -w fs.file-max=100000

参考

posted @ 2022-12-11 15:54  知科行技  阅读(29)  评论(0编辑  收藏  举报