Redis replication
1. Redis 安装
1.1 下载安装
sudo mkdir -p /home/q/redis/data
echo '下载redis 5.0'
sudo wget -P /home/q/redis http://download.redis.io/releases/redis-5.0.0.tar.gz
echo '解压redis-5.0.0.tar.gz'
sudo tar -zxvf /home/q/redis/redis-5.0.0.tar.gz -C /home/q/redis
echo 'redis 编译'
cd /home/q/redis/redis-5.0.0/deps
sudo make hiredis lua jemalloc linenoise
cd /home/q/redis/redis-5.0.0
sudo make
echo 'redis 安装'
cd /home/q/redis/redis-5.0.0
sudo make install
部分日志输出
===============================================================================
jemalloc version : 5.1.0-0-g0
library revision : 2
CONFIG : --with-version=5.1.0-0-g0 --with-lg-quantum=3 --with-jemalloc-prefix=je_ --enable-cc-silence 'CFLAGS=-std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops ' LDFLAGS=
CC : gcc
CONFIGURE_CFLAGS : -std=gnu99 -Wall -Wsign-compare -Wundef -Wno-format-zero-length -pipe -g3 -fvisibility=hidden -O3 -funroll-loops
SPECIFIED_CFLAGS : -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops
EXTRA_CFLAGS :
CPPFLAGS : -D_GNU_SOURCE -D_REENTRANT
CXX : g++
CONFIGURE_CXXFLAGS :
SPECIFIED_CXXFLAGS :
EXTRA_CXXFLAGS :
LDFLAGS :
EXTRA_LDFLAGS :
DSO_LDFLAGS : -shared -Wl,-soname,$(@F)
LIBS : -lrt -lm -lpthread -ldl
RPATH_EXTRA :
XSLTPROC : false
XSLROOT :
PREFIX : /usr/local
BINDIR : /usr/local/bin
DATADIR : /usr/local/share
INCLUDEDIR : /usr/local/include
LIBDIR : /usr/local/lib
MANDIR : /usr/local/share/man
srcroot :
abs_srcroot : /home/q/redis/redis-5.0.0/deps/jemalloc/
objroot :
abs_objroot : /home/q/redis/redis-5.0.0/deps/jemalloc/
JEMALLOC_PREFIX : je_
JEMALLOC_PRIVATE_NAMESPACE
: je_
install_suffix :
malloc_conf :
autogen : 0
debug : 0
stats : 1
prof : 0
prof-libunwind : 0
prof-libgcc : 0
prof-gcc : 0
fill : 1
utrace : 0
xmalloc : 0
log : 0
lazy_lock : 0
cache-oblivious : 1
cxx : 0
===============================================================================
cd src && make install
make[1]: Entering directory `/home/q/redis/redis-5.0.0/src'
CC Makefile.dep
make[1]: Leaving directory `/home/q/redis/redis-5.0.0/src'
make[1]: Entering directory `/home/q/redis/redis-5.0.0/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/home/q/redis/redis-5.0.0/src'
1.2 配置redis.conf
echo '复制配置文件'
sudo cp /home/q/redis/redis-5.0.0/redis.conf /etc/
echo '更新redis.conf'
# 设置daemonize
sudo sed -i 's/daemonize no/daemonize yes/' /etc/redis.conf
# 设置redis.pid
sudo sed -i 's/\/var\/run\/redis_6379.pid/\/home\/q\/redis\/data\/redis.pid/' /etc/redis.conf
# 设置logfile
sudo sed -i '/logfile/s/.*/logfile \/home\/q\/redis\/data\/redis.log/g' /etc/redis.conf
# 设置dir路径
sudo sed -i '/^dir/s/.*/dir \/home\/q\/redis\/data/g' /etc/redis.conf
1.3 设置Redis相关的用户
# 查看是否有redis用户
cat /etc/passwd | grep redis
# 创建redis用户和组
sudo groupadd redis >/dev/null 2>&1 || true
sudo useradd -M -g redis redis -s /sbin/nologin >/dev/null 2>&1 || true
sudo chown -R redis:redis /home/q/redis/redis-5.0.0
1.4 启动Redis服务
# 查看redis相关服务
sudo netstat -anp | grep 6379
sudo ps -ef | grep redis | grep -v grep
# 启动redis服务
sudo -u redis /home/q/redis/redis-5.0.0/src/redis-server /etc/redis.conf
# 关闭redis服务
sudo redis-cli -a <password> shutdown
1.5 Redis脚本
#!/bin/sh
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/home/q/redis/data/redis.pid
CONF="/etc/redis.conf"
REDISPORT="6379"
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
;;
status)
if [ ! -f $PIDFILE ]
then
echo 'Redis is not running'
else
echo "Redis is running ($(<$PIDFILE))"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
2. Redis主从配置
2.1 redis命令
# 查看主从关系
redis> info replication
# 设置客户端连接密码
redis> config set requirepass <password>
# 设置slave连接master密码
redis> config set masterauth <password>
# master断开slave节点
redis> slaveof no one
# master设置slave节点
redis> slaveof <host/ip> <port>
# 设置slave节点可读
redis> config set slave-read-only yes
2.2 Redis主从日志
主库日志
[2187] 17 Oct 18:22:58.931 * Slave ask for synchronization
[2187] 17 Oct 18:22:58.932 * Starting BGSAVE for SYNC
[2187] 17 Oct 18:22:58.988 * Background saving started by pid 11755
[11755] 17 Oct 18:23:04.036 * DB saved on disk
[11755] 17 Oct 18:23:04.067 * RDB: 6 MB of memory used by copy-on-write
[2187] 17 Oct 18:23:04.138 * Background saving terminated with success
[2187] 17 Oct 18:23:06.951 * Slave ask for synchronization
[2187] 17 Oct 18:23:06.951 * Starting BGSAVE for SYNC
[2187] 17 Oct 18:23:07.001 * Background saving started by pid 11756
[11756] 17 Oct 18:23:12.107 * DB saved on disk
[11756] 17 Oct 18:23:12.140 * RDB: 6 MB of memory used by copy-on-write
[2187] 17 Oct 18:23:12.256 * Background saving terminated with success
[2187] 17 Oct 18:23:20.865 * Synchronization with slave succeeded
从库日志
[2723] 17 Oct 18:22:58.930 * Connecting to MASTER...
[2723] 17 Oct 18:22:58.930 * MASTER <-> SLAVE sync started
[2723] 17 Oct 18:22:58.930 * Non blocking connect for SYNC fired the event.
[2723] 17 Oct 18:22:58.931 * Master replied to PING, replication can continue...
[2723] 17 Oct 18:23:02.760 * MASTER MODE enabled (user request)
[2723] 17 Oct 18:23:02.831 * 10 changes in 300 seconds. Saving...
[2723] 17 Oct 18:23:02.832 * Background saving started by pid 5537
[5537] 17 Oct 18:23:06.044 * DB saved on disk
[5537] 17 Oct 18:23:06.044 * RDB: 490 MB of memory used by copy-on-write
[2723] 17 Oct 18:23:06.050 * Background saving terminated with success
[2723] 17 Oct 18:23:06.762 * SLAVE OF 192.168.248.156:6379 enabled (user request)
[2723] 17 Oct 18:23:06.950 * Connecting to MASTER...
[2723] 17 Oct 18:23:06.950 * MASTER <-> SLAVE sync started
[2723] 17 Oct 18:23:06.950 * Non blocking connect for SYNC fired the event.
[2723] 17 Oct 18:23:06.950 * Master replied to PING, replication can continue...
[2723] 17 Oct 18:23:12.257 * MASTER <-> SLAVE sync: receiving 258965547 bytes from master
[2723] 17 Oct 18:23:20.951 * MASTER <-> SLAVE sync: Loading DB in memory
[2723] 17 Oct 18:23:24.547 * MASTER <-> SLAVE sync: Finished with success
[2723] 17 Oct 18:23:24.558 * Background append only file rewriting started by pid 5564
[5564] 17 Oct 18:23:26.327 * SYNC append only file rewrite performed
[5564] 17 Oct 18:23:26.335 * AOF rewrite: 10 MB of memory used by copy-on-write
[2723] 17 Oct 18:23:26.358 * Background AOF rewrite terminated with success
[2723] 17 Oct 18:23:26.358 * Parent diff successfully flushed to the rewritten AOF (74 bytes)
[2723] 17 Oct 18:23:26.358 * Background AOF rewrite finished successfully