Redis同步更新简介
Redis是一个开源的使用ANSI C编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,它通常被称为数据结构服务器,因为值可以是字符串、哈希表、列表、集合和有序集合。
Redis提供了两种主要的数据同步方式:主从复制(Master-Slave Replication)和哨兵模式(Sentinel),主从复制是一种异步的复制方式,而哨兵模式则是一种自动故障转移的方式,本文将重点介绍如何使用主从复制来实现Redis的同步更新。
配置主从复制
1、安装Redis
首先需要在客户端机器上安装Redis,可以从官网下载源码包编译安装,也可以使用包管理器进行安装,以Ubuntu为例,可以使用以下命令安装:
sudo apt-get update sudo apt-get install redis-server
2、配置主节点
在主节点的配置文件中,需要设置以下参数:
bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/lib/redis slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no slave-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no luatime-limit 5000 slowlog-log-slower-than 100000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" disable keys notifications for now (we'll reenable later if needed)"$@" enable keys notifications for the specified events: kx,ex,axm,e2b,xe2b,lru,lru_crawler,je (note that je is a reserved event name, you should not use it in your own scripts) repl-diskless-sync no disable sync when running with diskless deployment (useful when doing development) slave-priority default set slave priority to default (not recommended) masterauth password yourpassword enable security features now (note that this will disable all encryption features that have been implemented in Redis) bind 127.0.0.1 bind to address 127.0.0.1 instead of localhost (useful for containers) protected-mode no disable protection against issues like TCP port scans and DNS queries on bind address (useful for containers) port 6389 change port to something else than the default one of 6379 cluster-enabled yes enable cluster mode (disabled by default as cluster requires more configuration steps) cluster-config-file nodes.conf configure the nodes file used by our cluster (this file is automatically generated at startup) cluster-node-timeout 5000 set timeout for slave connection reconnection attempts appendonly yes enable append only file appendfilename "appendonly.aof" specify a path for the append only file save "900 1 hour" change the amount of time given to save rdb files (default to 300 seconds) save "300 10 minutes" change the amount of time given to save aof files (default to 60 seconds) rdbcompression yes enable compression on saving RDB files rdbchecksum yes enable checksum on saving RDB files dbfilename dump.rdb specify a different filename for dump.rdb (default is dump.rdb) dir /var/lib/redis specify where to store the database files (default is /var/lib/redis) slaveof no disable automatic failover to master when slave is down slaveof <ip> <port> force master IP and port to be used by slave instead of autodiscovery masterauth password yourpassword set a password for master authentification requirepass yourpassword set a password for user "youruser" masterauth2 password yourpassword2 set a password for user "youruser2" masterclientid yourclientid set a unique id for this master replname mymaster set a unique id for this replica repldomain mymaster.example.com set the domain name used by this replica replcachesize 64mb set the max amount of memory used by replication cache replignorepattern * do not notify about changes in keys starting with this pattern replpingsecondaries yes enable secondary servers to send their state to master via Ping command slavepriority 100 set the priority of this slave server within the replication group slavereadonly yes read only mode for this slave server repldisabletcpnodelay no disable TCP nodelay optimizations while reading data from master replstreamingclient backlog_buffer_size 512mb set the size of the buffer used by streaming client for backlog messages replstreamingclient backlog_buffer_max_messages 1gig set the maximum number of backlog messages that can be buffered by streaming client replstreamingclient backlog_buffer_max_seconds 604800 set the maximum time (in seconds) between flushes of backlog buffer by streaming client replstreamingclient backlog_buffer_max_bytes 1gb set the maximum total size (in bytes) of the backlog buffer by streaming client repl_disable_tcp_nodelay no disable TCP nodelay optimizations while reading data from master repl_disable_tcp_nodelay_on_resume yes enable TCP nodelay optimizations while reading data from master after slave has resumed replication
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/267983.html