redis官网下载地址,
1 redis单机版本安装
上传redis-3.0.7.tar.gz到application目录下
mkdir -p /application
cd /application
tar xzvf redis-3.0.7.tar.gz
cd redis-3.0.7
make MALLOC=jemalloc
make PREFIX=/application/redis-3.0.7 install
ln -s /application/redis-3.0.7/ /application/redis
cd ../redis
mkdir logs 创建日志目录
mkdir redisData 创建持久化数据目录
cd ..
rm -f redis-3.0.7.tar.gz
vi /etc/profile,添加以下redis的环境变量配置,并通过source /etc/profile生效。
export REDIS_HOME=/application/redis
export PATH=$PATH:$REDIS_HOME/bin
执行以下命令,对redis服务器进行启动、关闭操作
# redis启动
redis-server /application/redis/redis.conf &
# redis关闭
redis-cli shutdown
redis-cli -p 6379 shutdown 也可以执行此命令
# 查看redis进程
ps -ef | grep redis
2 redis主从集群Sentinel模式
port 26379
daemonize yes
sentinel monitor mymaster 192.168.5.174 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
运行redis sentinel
redis-server /application/redis/sentinel.conf --sentinel 运行sentinel
redis-cli -p 26379 sentinel masters 查看当前的master节点情况
redis-cli -p 26379 shutdown 停掉sentinel
3 redis.conf
3.2以后的版本会有变化
notify-keyspace-events Ex
logfile "/application/redis/logs/redis.log"
dir /application/redis/redisData
slaveof 192.168.5.174 6379 仅在slave节点增加此配置。
4 redis开机启动
更改redis.conf
将redis改成守护进程,将daemonize调整为yes
daemonize yes
定义/etc/init.d/redis脚本,注意redis的目录
# chkconfig: 2345 90 10
# description: redis is a persistent key-value database
###########################
###########################
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/application/redis/bin/redis-server
REDIS_CLI=/application/redis/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/application/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
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 755 redis
执行启动测试,
service redis start #或者 /etc/init.d/redis start
service redis stop #或者 /etc/init.d/redis stop
设置开机启动
5 过程遇到的问题
5.1 syntax error near unexpected token newline
如果出现syntax error near unexpected token
newline’的异常,则执行命令
dos2unix /etc/init.d/redis,将回车换行改成linux的$,可以通过
cat -A /etc/init.d/redis核实是否修正完毕,详细解决办法参照[关于报错“syntax error near unexpected token
”和回车换行](http://blog.csdn.net/windless0530/article/details/6077654)
5.2 /var/run/redis.pid exists, process is already running or crashed
执行命令rm -rf /var/run/redis.pid
,将之前的删掉,原因是之前的redis通过其他的方式正在运行,采用新的方式redis就没法启动了。
5.3 service redis does not support chkconfig
必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中:
# chkconfig: 2345 90 10
# description: redis is a persistent key-value database
详细可以参考 service redis does not support chkconfig的解决办法
6 redis配置密钥
redis不需要访问密码,则任何应用都可以接入
user default on nopass sanitize-payload ~* &* +@all
但是设置了requirepass也有问题,因为
# 产生hash值
[root@es2 ~]# echo -n "12345678" | sha256sum
ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f -
# 在redis.conf中配置
user default on sanitize-payload #ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f ~* &* +@all