TypechoJoeTheme

IT技术分享

统计

Redis主从(master / slave)在CetOS 7 下的安装与配置

2016-07-24
/
0 评论
/
570 阅读
/
正在检测是否收录...
07/24

安装必要包

yum install gcc-g++

下载redis

wget http://download.redis.io/releases/redis-3.2.3.tar.gz
tar xzf redis-3.2.3.tar.gz

编译源码

cd redis-3.2.3
make MALLOC=libc  #如果不加参数,linux下会报错

配置redis

1、创建目录

mkdir /home/redis/log
mkdir /home/redis/pid
mkdir /home/redis/db
mkdir /home/redis/bin
mkdir /home/redis/conf
mkdir /home/redis/script

2、拷贝文件

将编译好的src下的文件拷贝到/home/redis/bin目录下,需要拷贝的文件包括

①、mkreleasehdr.sh
②、redis-benchmark
③、redis-check-aof
④、redis-check-rdb
⑤、redis-cli
⑥、redis-sentinel
⑦、redis-server
⑧、redis-trib.rb

3、修改配置文件

将编译好的redis.conf复制到/home/redis/conf下,编辑

daemonize yes #是否把redis-server启动在后台,默认是“否”。若改成yes,会生成一个pid文件。
pidfile /home/redis/pid/redis.pid #redis-server的pid文件
port 6379 #redis-server的端口号
tcp-backlog 511
timeout 600
tcp-keepalive 0
loglevel notice #日志级别,有四种,debug,verbose,notice,warning。
logfile /home/redis/log/redis.log #日志的输出文件,默认是标准输出。例如:logfile /tmp/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb #磁盘上保存数据库文件的位置
dir /home/redis/db
slave-serve-stale-data yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

3、创建启动脚本

在/home/redis/script目录下创建redis-server.sh,并写入以下脚本

#!/bin/sh
# chkconfig:        2345 60 40
# Description:      Start and Stop redis
# Provides:         redis
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
PATH=/home/redis/bin
REDISPORT=6379
EXEC=/home/redis/bin/redis-server
REDIS_CLI=/home/redis/bin/redis-cli
PIDFILE=/home/redis/pid/redis.pid
CONF="/home/redis/conf/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 server..."
        $REDIS_CLI -p $REDISPORT SHUTDOWN
        while [ -x ${PIDFILE} ]
        do
            echo "Waiting for Redis to shutdown ..."
            sleep 1
        done
        echo "Redis is stopped"
    fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac

4、创建链接脚本

在/home/redis/script目录下创建client.sh,并写入以下脚本

 cd /home/redis/script
 ./bin/redis-cli -h ip -p port #ip换成自己的ip,port换成6379

启动redis

cd /home/redis/script
sh redis-server start

关闭redis

方法一

ps -ef | grep redis

找到进程号后kill掉

方法二

cd /home/redis/script
sh redis-server stop

测试

cd /home/redis/script
./client.sh
set ds dunso
get ds

如果能得到dunso,说明写入成功

安装slave

以安装在/home/redis-slave/目录下为例

1、按照上面的方法重新安装一套

2、修改配置文件

在redis.conf中增加

slaveof ip port #ip填写master的ip,端口填写master的端口
slave-read-only yes #表示slave只读不写,也是推荐设置

测试slave

首先启动slave,然后

cd /home/redis-slave/script
./client.sh
get ds

如果能够得到dunso,说明已经同步成功。

朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/2452/(转载时请注明本文出处及文章链接)