Linux下安装Redis
安装chkconfig:
rpm -aq |grep chkconfig
chkconfig-1.3.30.1-2
# export PATH=/sbin:$PATH
# chkconfig
或是 vi /etc/profile
加入 export PATH=/sbin:$PATH
执行 source /etc/profile
执行 chkconfig
安装redis:
tar -xvf redis-3.0.6.tar.gz
cd redis-3.0.6
make PREFIX=/usr/local/redis install
测试:
cd src
make test
配置redis服务:
cp utils/redis_init_script /etc/rc.d/init.d/redis
添加注册服务:
vi /etc/rc.d/init.d/redis
第2行添加
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
修改EXEC和CLIEXEC路径(安装文件目录):
EXEC=/app/re/redis-3.0.6/src/redis-server
CLIEXEC=/app/re/redis-3.0.6/src/redis-cli
redis开启的命令,以后台运行的方式执行。
$EXEC $CONF &
改完了保存!
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
/etc/profile 加入
export REDIS_HOME=/usr/local/redis
export PATH=${PATH}:${REDIS_HOME}/bin
执行 source /etc/profile
注册redis服务:
chkconfig --add redis
#设置为开机自启动服务器
chkconfig redis on
#打开服务
service redis start
#关闭服务
service redis stop
关闭服务时出现问题:/var/run/redis_6379.pid文件不存在
需要做如下修改:
vi /etc/redis/6379.conf
查找:daemonize
################################ GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis_6379.pid
访问策略,需要时进行修改
#bind 127.0.0.1 可考虑注释掉这一行
redis.conf配置文件中指定的pid路径地址,这里说明一下,在 redis.conf配置文件中需要将 daemonize这个参数项设置为 yes才会在redis启动时生成pid文件,很多新人不知道,没有生成pid文件,所以脚本里根据pid文件关闭redis就失败。
启动时,有如下提示,按要求处理即可
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
sysctl vm.overcommit_memory=1
$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set aa bb
OK
127.0.0.1:6379> get aa
"bb"
127.0.0.1:6379>
vi /etc/redis/6379.conf
修改数据文件路径:
# The filename where to dump the DB
dbfilename dump.rdb
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /app/redis_data/
配置主从:
################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 192.168.1.2 6379