分享Redis主从配置的详细步骤

发布时间:2020-02-20编辑:脚本学堂
本文介绍下,配置redis主从的详细步骤,包括redis的安装、redis主服务器设置、redis从服务器设置、redis启动脚本等内容,有需要的朋友参考下。

为大家介绍redis主从配置的具体方法,从安装到配置,到启动脚本,很全面的。
是一篇不错的有关redsi主从配置的文章。

1,创建目录
 

复制代码 代码示例:
# mkdir -p /usr/local/redis/{etc,bin,var}
# mkdir -p /data/logs/redis /data/dbcache

2,编译安装redis
 

复制代码 代码示例:
# tar -xvzf redis-2.4.13.tar.gz
# cd redis-2.4.13
# make
# cd src && cp redis-server redis-cli redis-benchmark /usr/local/redis/bin
# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
# /sbin/sysctl -p

3,主服务器设置【192.168.1.100】:
 

复制代码 代码示例:
# vim /usr/local/redis/etc/redis.conf
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
bind 192.168.1.100
unixsocket /usr/local/redis/var/redis.sock
unixsocketperm 755
timeout 300
loglevel verbose
logfile /data/logs/redis/redis.log
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data/dbcache
# slaveof <masterip> <masterport>
# masterauth <master-password>
# repl-ping-slave-period 10
# repl-timeout 60
requirepass redis123
# rename-command CONFIG ""
maxclients 0
# maxmemory <bytes>
# maxmemory-policy volatile-lru
# maxmemory-samples 3
appendonly no
appendfilename appendonly.aof
appendfsync always
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
vm-enabled no
vm-swap-file /data/dbcache/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 512
hash-max-zipmap-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
activerehashing yes

4,从服务器设置【192.168.1.200】:
 

复制代码 代码示例:
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
bind 192.168.1.200
unixsocket /usr/local/redis/var/redis.sock
unixsocketperm 755
timeout 300
loglevel verbose
logfile /data/logs/redis/redis.log
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data/dbcache
# slaveof <masterip> <masterport>
# masterauth <master-password>
# repl-ping-slave-period 10
# repl-timeout 60
# rename-command CONFIG ""
maxclients 0
# maxmemory <bytes>
# maxmemory-policy volatile-lru
# maxmemory-samples 3
appendonly no
appendfilename appendonly.aof
appendfsync always
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
vm-enabled no
vm-swap-file /data/dbcache/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 512
hash-max-zipmap-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
activerehashing yes
slave-serve-stale-data yes
slaveof 192.168.1.100 6379
masterauth redis123

测试:
启动服务:
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
主服务器上执行:
/usr/local/redis/bin/redis-cli -h 192.168.1.100 -a redis123 set test 123456
从服务器上执行:
/usr/local/redis/bin/redis-cli -h 192.168.1.200 get test

6,启动脚本
 

复制代码 代码示例:

# vim /etc/init.d/redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 85 15
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /usr/local/redis/etc/redis.conf
# pidfile:     /usr/local/redis/var/redis.pid
# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/redis/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/usr/local/redis/etc/redis.conf"
lockfile="/var/lock/subsys/redis"
start() {
        [ -x $redis ] || exit 5
        [ -f $REDIS_CONF_FILE ] || exit 6
        echo -n $"Starting $prog: "
        $redis $REDIS_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
}

stop() {
        echo -n $"Stopping $prog: "
        /sbin/killproc $prog -QUIT

        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
}
restart() {
        stop
        sleep 1
        start
}
reload() {
        echo -n $"Reloading $prog: "
        /sbin/killproc $redis -HUP

        RETVAL=$?
        echo
}

case "$1" in
start)
    $1
    ;;
stop)
    $1
    ;;
restart)
    $1
    ;;
reload)
    $1
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|reload}"
    exit 2
esac

7,启动服务
 

复制代码 代码示例:
# chmod 700 /etc/init.d/redis
# chkconfig --add redis
# service redis start
 

测试是否已启动
# /usr/local/redis/bin/redis-cli ping
性能测试
# /usr/local/redis/bin/redis-benchmark
关闭服务
# /usr/local/redis/bin/redis-cli -p 6379 shutdown
强制刷新数据到磁盘【Redis默认是异步写入磁盘的】
# /usr/local/redis/bin/redis-cli -p6379 save

8,phpredis扩展安装
 

复制代码 代码示例:
# svn checkout http://phpredis.googlecode.com/svn/trunk/phpredis-read-only
# /usr/local/php/bin/phpize
# ./configure--with-php-config=/usr/local/php/bin/php-config
# make
# make install