使用说明
- 适应于centos7操作系统,安装单点redis部署;
- firewalld和iptables需开放PORT监听端口;
- redis密码(PAS及SSWD)和监听PORT等,可根据需要修改;
- 该脚本应用场景为开发测试,对redis性能和稳定性的要求不高;
- 若生产使用redis,则需找DBA选择合适的方案,如集群模式和哨兵模式等,避免redis雪崩而影响业务。
redis_install.sh文件
#安装redis shell脚本
##0.离线环境下proxy_ip可用nginx反向代理, 或直接使用tar.gz压缩包
#!/bin/sh
set -eu
proxy_ip=109.74.203.151
PORT=6379
MAMMEMORY=4g
PASSSWD=sysu-edu-cn
##1.创建存放代码和日志的目录
mkdir -p /opt/soft/
mkdir -p /data/redis/
useradd redis
##2.配置环境
yum install -y gcc-c++ gcc tcl-devel
curl download.redis.io
if [ $? != 0 ];then
echo "$proxy_ip download.redis.io" >>/etc/hosts
fi
##3.下载安装redis
wget -O /opt/soft/redis-5.0.6.tar.gz http://download.redis.io/releases/redis-5.0.6.tar.gz
cd /opt/soft/
tar -xf redis-5.0.6.tar.gz
cd redis-5.0.6
make && make test
if [ $? != 0 ];then
echo "redis 安装异常,请及时查看错误 "
exit 10
fi
##4.更改redis权限
chown -R redis.redis /opt/soft/redis-5.0.6 /data/redis
ln -s /opt/soft/redis-5.0.6 /opt/redis
cp src/redis-cli /usr/bin/
##5.更改redis配置
cat << EOF > /opt/soft/redis-5.0.6/redis.conf
daemonize yes
pidfile /data/redis/redis_${PORT}.pid
port ${PORT}
tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel verbose
logfile "/data/redis/redis_${PORT}.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_${PORT}.rdb
dir /data/redis/
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
requirepass $PASSSWD
maxmemory $MAMMEMORY
maxmemory-policy allkeys-lru
appendonly yes
appendfilename "appendonly_${PORT}.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 1024mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 20000000
hash-max-ziplist-value 2000
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF
##6.启动redis
su - redis -c '/opt/redis/src/redis-server /opt/redis/redis.conf'
##7.检测redis
redis-cli -p $PORT -a $PASSSWD info
##8.添加开机启动项
if [ $? != 0 ];then
echo "redis 安装异常,请及时查看错误 "
exit 10
else
echo "su - redis -c '/opt/redis/src/redis-server /opt/redis/redis.conf' " >>/etc/rc.local
fi