redis的编译安装非常简单,
下载redis-2.6.13.tar.gz后,
tar xvf redis-2.6.13.tar.gz
cd redis-2.6.13
make && make install
将会把redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump 五个文件复制到 /usr/local/bin/下
[root@localhost ~]# ll /usr/local/bin
总用量 12464
-rwxr-xr-x. 1 root root 3853805 6月 18 18:54 redis-benchmark
-rwxr-xr-x. 1 root root 16459 6月 18 18:54 redis-check-aof
-rwxr-xr-x. 1 root root 37707 6月 18 18:54 redis-check-dump
-rwxr-xr-x. 1 root root 3909826 6月 18 18:54 redis-cli
-rwxr-xr-x. 1 root root 4931736 6月 18 18:54 redis-server
[root@localhost ~]#
然后将源码中的 redis.conf 复制到 /etc/redis.conf
修改 /etc/redis.conf里面可以把 daemonize no 修改为
daemonize yes
就可以默认在后台执行redis-server了。
再制作一个 init.d 的启动脚本:
#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis \
# chkconfig --add redis or chkconfig --list redis \
# service redis start or service redis stop
# processname: redis-server
# config: /etc/redis.conf
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
#make sure some dir exist
if [ ! -d /var/lib/redis ] ;then
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
fi
case "$1" in
status)
ps -A|grep redis
;;
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
将上面内容复制到 /etc/init.d/redis
chmod o+x /etc/init.d/redis
chkconfig --add redis
chkconfig --list redis
service redis start
这就是主服务器,从服务器,配置一样,只不过 修改/etc/redis.conf 中
slaveof
slaveof 192.168.0.1 6379
然后开启从服务器的redis服务。
测试
#主服务器
redis-cli -p 6379 set hello world
#从服务器
redis-cli -p 6379 get hello
"world"
#主服务器
redis-cli -p 6379 set hello world2
#从服务器
redis-cli -p 6379 get hello
"world2"
redis-cli -p 6379 set hello world
(error) READONLY You can't write against a read only slave.
#成功 配置主从redis 服务器。好简单啊。
由于配置中有一条 从服务器 是只读的,所以从服务器 没法设置数据,只可以读取数据。