1、下载Redis3.2.5安装包
wget http://download.redis.io/releases/redis-3.2.5.tar.gz
2、解压、编译、安装redis-3.2.5:
tar -zxvf redis-3.2.5.tar.gz -C /usr/src/
cd /usr/src/redis-3.2.5/
make PREFIX=/home/redis install
3、创建redis相关目录:
mkdir -p /home/redis/log
mkdir -p /home/redis/pid
mkdir -p /home/redis/db
4、将可执行文件复制到自己的安装目录:/home/redis/
ln -s /usr/local/bin/redis-* /home/redis/bin/
(第4步可以不要)
5、复制配置文件到自己的安装目录:/home/redis/
cp redis.conf /home/redis/
6、进入自己的安装目录,编辑redis.conf配置文件:
cd /home/redis/
vim /home/redis/redis.conf
根据实际需要修改配置文件,以下仅供参考
daemonize yes
pidfile /home/redis/pid/redis.pid
logfile /home/redis/log/redis.log
dir /home/redis/db
port 6379
tcp-backlog 511
timeout 600
tcp-keepalive 0
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
slave-serve-stale-data yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
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
#vm-enabled no
#max-memory 4G
7.修改环境变量
vim /etc/profile
export PATH=/home/redis/bin:$PATH
8.修改系统内核参数
a. sysctl vm.overcommit_memory=1
b. echo never > /sys/kernel/mm/transparent_hugepage/enabled
并且将它写进/etc/rc.local文件里,防止重启后失效。
c.echo 511 > /proc/sys/net/core/somaxconn
vim /etc/sysctl.conf(永久修改)
systemctl -p
9、创建redis服务脚本,并赋予权限:
vim /etc/init.d/redis
#!/bin/sh
#Simple Redis init.d script conceived to work on Linux systems as it does use of the /proc filesystem.
PATH=/home/redis/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/home/redis/bin/redis-server
CLIEXEC=/home/redis/bin/redis-cli
PIDFILE=/home/redis/pid/redis.pid
CONF="/home/redis/redis.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
8、添加redis服务开机启动:
chmod a+x /etc/init.d/redis
9、启动redis服务:
service redis start
ps -ef | grep redis
netstat -anptu | grep 6379
10、测试OK
redis-cli
set key1 hello
get key1
quit
(防火墙启用6379端口:iptables -A INPUT -p tcp --dport 6379 -j ACCEPT)
注意:运行过程中,若出现报错, MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.则说明服务器内存不足,导致redis在使用rdb快照方式持久化数据,fork另一个进程时,内存不够用。此时,可以用命令打开内核参数echo 1 > /proc/sys/vm/overcommit_memory。
redis两种持久化数据方式,一是rdb快照方式,二是aof方式。
rdb方式是在定时时间到了后,fork一个进程将数据刷写到磁盘。如果定时刷写之前redis发生故障,会发生数据丢失。
aof方式有三种策略,1.每一条aof记录都立即同步到文件,但IO开销较大。2.每秒同步一次。3.redis并不直接调用文件同步,而是交给操作系统来处理,操作系统可以根据buffer填充情况/通道空闲时间等择机触发同步。
如果你的redis持久化手段中有aof,那么在server故障失效后再次启动前,需要检测aof文件的完整性。aof文件默认位于src下appendonly.aof。命令为./redis-check-aof --fix appendonly.aof