欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Centos7 Redis主从搭建配置的实现

程序员文章站 2022-03-10 21:02:39
一、环境介绍 redis—master   172.18.8.19 redis—slave   172.18.8.20 二...

一、环境介绍

redis—master   172.18.8.19
redis—slave   172.18.8.20

二、redis主的配置

#创建redis数据目录
mkdir -p /data0/redis_trade

#redis主配置文件
root># cat redis_6379.conf |grep -ev "^$|^#"
bind 172.18.8.19
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis_6379.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /data0/redis_trade
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass allwelltokok
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
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-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
rename-command flushall zyzv6fobdwflw2nx
rename-command eval s9uhpkepsvujmm
rename-command flushdb d60fpvdjuip7gy6l
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

三、redis从配置

root># cat redis_6379.conf |grep -ev "^$|^#"
bind 172.18.8.20
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis_6379.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /data0/redis_trade
slaveof 172.18.8.19 6379   -----从库比主库多这2行配置参数
masterauth allwelltokok   -----从库比主库多这2行配置参数
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass allwelltokok
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
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-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
rename-command flushall zyzv6fobdwflw2nx
rename-command eval s9uhpkepsvujmm
rename-command flushdb d60fpvdjuip7gy6l
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

四、redis启动脚本

root># cat /etc/init.d/redis_6379 
#!/bin/sh
#
# simple redis init.d script conceived to work on linux systems
# as it does use of the /proc filesystem.
# chkconfig:  2345 90 10

source /etc/init.d/functions
redisport=6379
exec=/usr/local/bin/redis-server
cliexec=/usr/local/bin/redis-cli
 
pidfile=/var/run/redis_${redisport}.pid
conf="/usr/local/redis/etc/redis_${redisport}.conf"
auth="allwelltokok"
bind_ip='172.18.8.19'
 
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 ..."
    $cliexec -h $bind_ip -a $auth -p $redisport shutdown 
    sleep 1
    while [ -x /proc/${pid} ]
    do
      echo "waiting for redis to shutdown ..."
      sleep 1
    done
      echo "redis stopped"
  fi
}
 
restart(){
  stop
  start
 
}
status(){
 
  ps -ef|grep redis-server|grep -v grep >/dev/null 2>&1
 
  if [ $? -eq 0 ];then
 
    echo "redis server is running"
 
  else
    echo "redis server is stopped"
 
  fi
   
 
}
 
 
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
     
  restart)
    restart
    ;;
     
  status)
    status
    ;;   
  *)
   
   echo "usage: /etc/init.d/redis {start|stop|status|start}" >&2 
   exit 1 
esac

五、启动服务

root># /etc/init.d/redis_6379 start  

查看日志

root># tail -100f /var/log/redis_6379.log
5563:s 29 jun 22:14:23.236 * increased maximum number of open files to 10032 (it was originally set to 1024).
        _._                         
      _.-``__ ''-._                       
   _.-``  `. `_. ''-._      redis 3.2.12 (00000000/0) 64 bit
 .-`` .-```. ```\/  _.,_ ''-._                  
 (  '   ,    .-` | `,  )   running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|   port: 6379
 |  `-._  `._  /   _.-'  |   pid: 5563
 `-._  `-._ `-./ _.-'  _.-'                  
 |`-._`-._  `-.__.-'  _.-'_.-'|                 
 |  `-._`-._    _.-'_.-'  |      http://redis.io    
 `-._  `-._`-.__.-'_.-'  _.-'                  
 |`-._`-._  `-.__.-'  _.-'_.-'|                 
 |  `-._`-._    _.-'_.-'  |                 
 `-._  `-._`-.__.-'_.-'  _.-'                  
   `-._  `-.__.-'  _.-'                    
     `-._    _.-'                      
       `-.__.-'                        

5563:s 29 jun 22:14:23.237 # server started, redis version 3.2.12
5563:s 29 jun 22:14:23.237 * the server is now ready to accept connections on port 6379
5563:s 29 jun 22:14:23.237 * connecting to master 172.18.8.19:6379
5563:s 29 jun 22:14:23.237 * master <-> slave sync started
5563:s 29 jun 22:14:23.237 * non blocking connect for sync fired the event.
5563:s 29 jun 22:14:23.238 * master replied to ping, replication can continue...
5563:s 29 jun 22:14:23.238 * partial resynchronization not possible (no cached master)
5563:s 29 jun 22:14:23.239 * full resync from master: c9f303069f87253011bf39369366732a2e88b389:1
5563:s 29 jun 22:14:23.304 * master <-> slave sync: receiving 77 bytes from master
5563:s 29 jun 22:14:23.305 * master <-> slave sync: flushing old data
5563:s 29 jun 22:14:23.305 * master <-> slave sync: loading db in memory
5563:s 29 jun 22:14:23.305 * master <-> slave sync: finished with success
5563:s 29 jun 22:14:23.305 * background append only file rewriting started by pid 5567
5563:s 29 jun 22:14:23.329 * aof rewrite child asks to stop sending diffs.
5567:c 29 jun 22:14:23.329 * parent agreed to stop sending diffs. finalizing aof...
5567:c 29 jun 22:14:23.329 * concatenating 0.00 mb of aof diff received from parent.
5567:c 29 jun 22:14:23.329 * sync append only file rewrite performed
5567:c 29 jun 22:14:23.330 * aof rewrite: 0 mb of memory used by copy-on-write
5563:s 29 jun 22:14:23.337 * background aof rewrite terminated with success
5563:s 29 jun 22:14:23.337 * residual parent diff successfully flushed to the rewritten aof (0.00 mb)
5563:s 29 jun 22:14:23.337 * background aof rewrite finished successfully  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。