本文参照官方文档。最初安装redis时,编译后只能在当前目录使用,并且 init 脚本也是参照网络而写。最近在官方看了部分文档,对其编译与使用过程有了进一步了解,遂整理此文,以备查阅。
准备和说明:
系统:centos6.5
软件源码目录:/usr/local/src
软件安装目录:/usr/local/bin
软件官网:redis.io
软件下载地址及版本(截至本文发布最新稳定版):http://download.redis.io/releases/redis-3.0.6.tar.gz
软件安装:
$wget -c -P /usr/local/src $cd /usr/local/src $tar zxvf redis-3.0.6.tar.gz $cd /usr/local/src/redis-3.0.6 #查看REDME得知 #编译安装到当前路径src目录 $sudo make 或 #编译redis并安装到目录/usr/local/bin #此文采用此种方式 $sudo make install 或 #编译redis并安装到自定义目录/usr/local/redis $sudo make prefix=/usr/local/redis
经过上面的编译,生成以下二进制文件
- redis-server is the Redis Server itself.
- redis-sentinel is the Redis Sentinel executable (monitoring and failover).
- redis-cli is the command line interface utility to talk with Redis.
- redis-benchmark is used to check Redis performances.
- redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.
完善安装:
在完成以上编译,就已完成redis软件安装,可使用 ./redis-server 运行 redis 服务。但在生产环境这并不可取,所以需要后续工作进行完善。
如只使用 make 编译,请将 server 和 cli 拷贝到 /usr/local/bin 下:
$sudo cp src/redis-server /usr/local/bin $sudo cp src/redis-cli /usr/local/bin
创建配置文件和数据存储目录:
$sudo mkdir /etc/redis $sudo mkdir /var/redis
复制 init 脚本到 /etc/init.d 并使用 redis 加实例端口命名
$sudo utils/redis_init_script /etc/init.d/redis_6379
编辑 init 脚本,修改确认 REDISPORT 端口和 PIDFILE 、CONF 文件路径及名称,增加 chkconfig 级别
$sudo vim /etc/init.d/redis_6379
/etc/init.d/redis_6379
#!/bin/sh # # redis init file for starting up the redis daemon # # chkconfig: - 20 80 # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. # Source function library. . /etc/rc.d/init.d/functions REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" 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 ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
复制配置模板到 /etc/redis 目录,并以运行实例端口命名
$sudo cp redis.conf /etc/redis/6379.conf
创建运行实例工作目录
$sudo mkdir /var/redis/6379
编辑配置文件,确保以下更改
- Set daemonize to yes (by default it is set to no).
- Set the pidfile to
/var/run/redis_6379.pid
(modify the port if needed). - Change the port accordingly. In our example it is not needed as the default port is already 6379.
- Set your preferred loglevel.
- Set the logfile to
/var/log/redis_6379.log
- Set the dir to /var/redis/6379 (very important step!)
redis.conf
daemonize yes pidfile /var/run/redis_6379.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/var/log/redis_6379.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/redis/6379 slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 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 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-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
将 redis 加入系统服务,并设置默认运行级别
chkconfig --add redis_6379 chkconfig --level 2345 redis_6379 on
启动 redis 服务
$sudo service redis_6379 start
至此完成安装及配置,可使用 redis-cli 链接 redis 服务测试
时间: 2024-10-10 23:13:19