二进制部署Redis-5.07

Redis简介

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。
它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。
内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

Redis的特点

Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
Redis不仅仅支持简单的k-v类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
Redis支持数据的备份,即master-slave主从模式的数据备份。

Redis的优势

性能极高——Redis读的速度为11w/s,写的速度为8.1w/s。
丰富的数据类型——Redis支持二进制案例的Strings,Lists,Hashes,Sets即Ordered Sets数据类型操作。
原子性——Redis的所有操作都是原子性的,同时Redis还支持对几个操作合并后的原子性执行。
丰富的特性——Redis还支持public/subscribe,通知,key过期等特性。

Redis应用

应用在高并发和实时请求的场景,eg新浪微博
hash:关注列表,粉丝列表
string:微博数,粉丝数
(避免使用select count(*) from...)
sorted set:
TopN,热门微博,还有github,stackoverflow

部署

mkdir -p /application/tools
cd /application/tools
wget -q http://download.redis.io/releases/redis-5.0.7.tar.gz
tar xf redis-5.0.7.tar.gz -C /application/ 
make /application/redis-5.0.7 && make install 

复制新文件实例

mkdir -p /application/redis6380
cp /application/redis-5.0.7/redis.conf  /application/redis6380/
cp /application/redis-5.0.7/src/redis-server /application/redis6380/

创建实例日志存放路径以启动名称到环境启动加载位置并授权

mkdir -p /application/redis6380/logs/
cp /application/redis-5.0.7/src/redis-cli /usr/bin/
cp /application/redis-5.0.7/src/redis-server /usr/bin/
chmod +x /usr/bin/redis-server
chmod +x /usr/bin/redis-cli

修改配置文件

egrep -v "^#|^$" /application/redis6380/redis.conf 

bind 127.0.0.1
protected-mode yes
requirepass root
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /application/redis6380/redis-6380.pid
loglevel notice
logfile "/application/redis6380/logs/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

启动脚本

cat /etc/init.d/redis

#!/bin/bash
# chkconfig: 2345 80 90
# description: Start and Stop redis
REDISPORT=6380
EXEC=/application/redis6380/redis-server
REDIS_CLI=/usr/bin/redis-cli
PIDFILE=/application/redis6380/redis-6380.pid
CONF="/application/redis6380/redis.conf"
AUTH="root" #如果启用了密码,此处需要添加该项
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac

chmod +x /etc/init.d/redis

chkconfig --add redis

service redis satrt

配置成systemctl启动方式

vim /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=/data/redis6380/redis-server /data/redis6380/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

提示:如果多个实例可以复制出多个文件,然后改配置路径,复制出来的文件名不能一样,可以以端口号来命名

systemctl daemon-reload
systemctl enable redis.service
systemctl start redis.service

 

posted @ 2020-10-10 00:09  缺个好听的昵称  阅读(333)  评论(0编辑  收藏  举报