1、操作系统:CentOS 7.3
2、Redis:5.0.0
官网下载地址:http://www.redis.cn/download.html
百度云地址:https://pan.baidu.com/s/1f2XTXWHMs3nro6glidexrg 提取码:sf6h
tar -xvf redis-5.0.0.tar.gz
cd redis-5.0.0
make && make install
mkdir -p /opt/redis-5.0.0/bin
mkdir -p /opt/redis-5.0.0/log
mkdir -p /opt/redis-5.0.0/pid
mkdir -p /opt/redis-5.0.0/db
ln -s /usr/local/bin/redis-* /opt/redis-5.0.0/bin
(1)复制安装包下的redis配置文件到安装目录
cp /root/redis-5.0.0/redis.conf /opt/redis-5.0.0/
(2)编辑配置文件(所有配置文件均为在原来的基础上修改,不需添加)
vi /opt/redis-5.0.0/redis.conf
redis默认只允许本机连接,所以需要找到“bind 127.0.0.1”并将这行注释掉:
redis在3.0版本以后增加了保护模式,需要设置密码,如不需要设置密码,可将“protected-mode yes”改为no,即关闭保护模式:
将默认的“daemonize no”改为yes,设置redis以守护线程方式启动:
分别配置pid,log,db文件的保存地址,目录为我们事先创建好的相关目录
pidfile /opt/redis-5.0.0/pid/redis.pid
logfile /opt/redis-5.0.0/log/redis.log
dir /opt/redis-5.0.0/db
vi /etc/init.d/redis
(1)添加以下配置
#!/bin/sh
#
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
PATH=/opt/redis-5.0.0/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/opt/redis-5.0.0/bin/redis-server
CLIEXEC=/opt/redis-5.0.0/bin/redis-cli
PIDFILE=/opt/redis-5.0.0/pid/redis.pid
CONF="/opt/redis-5.0.0/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
(2)设置服务权限并配置为可执行文件
chmod 777 /etc/init.d/redis
chown root:root /etc/init.d/redis
(1)启动redis
service redis start
(2)登录redis查看
redis-cli
chkconfig redis on
(1)修改redis配置文件,添加一行配置“slaveof 192.168.0.101 6379”映射到主节点
vi /opt/redis-5.0.0/redis.conf
slaveof 192.168.0.101 6379
(2)重启从节点的redis
service redis stop
service redis start
(1)主节点与从节点均登录redis并执行info命令查看主从配置结果
redis-cli
info
找到“# Replication”模块,可以看到主节点提示存在一个从节点,并且会列出从节点的相关信息,同样,可以在从节点看到自己的主节点是哪个,列出主节点的相关信息
(2)验证主从
登录主节点redis,set age 24,到从节点直接get age,看到可以get到我们在主节点设置的值24,说明主从配置成功