Redis配置主从复制
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.操作环境介绍
1>.操作系统环境
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# uname -r 3.10.0-957.el7.x86_64 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# uname -m x86_64 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# free -h total used free shared buff/cache available Mem: 3.7G 102M 3.3G 11M 292M 3.3G Swap: 2.0G 0B 2.0G [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
2>.安装Redis(3台配置一样的虚拟机启动成功后,都需要安装Redis服务)
[root@node101.yinzhengjie.org.cn ~]# yum -y install epel-release Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:7-11 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================== Installing: epel-release noarch 7-11 extras 15 k Transaction Summary ============================================================================================================================================== Install 1 Package Total download size: 15 k Installed size: 24 k Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. epel-release-7-11.noarch.rpm | 15 kB 00:00:02 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : epel-release-7-11.noarch 1/1 Verifying : epel-release-7-11.noarch 1/1 Installed: epel-release.noarch 0:7-11 Complete! [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# yum -y install epel-release
[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# yum info redis Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile epel/x86_64/metalink | 5.8 kB 00:00:00 * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com epel | 4.7 kB 00:00:00 (1/3): epel/x86_64/group_gz | 88 kB 00:00:00 epel/x86_64/updateinfo FAILED https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/repodata/27797f54681404f3261395d766df370206f7d92cd3e1551a698663a6317d5c5a-updateinfo.xml.bz2: [Errno 14] HTTPS Error 404 - Not Found Trying other mirror. To address this issue please refer to the below wiki article https://wiki.centos.org/yum-errors If above article doesn't help to resolve this issue please use https://bugs.centos.org/. (2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00 (3/3): epel/x86_64/primary_db | 6.6 MB 00:00:06 Available Packages Name : redis Arch : x86_64 Version : 3.2.12 Release : 2.el7 Size : 544 k Repo : epel/x86_64 Summary : A persistent key-value database URL : http://redis.io License : BSD Description : Redis is an advanced key-value store. It is often referred to as a data : structure server since keys can contain strings, hashes, lists, sets and : sorted sets. : : You can run atomic operations on these types, like appending to a string; : incrementing the value in a hash; pushing to a list; computing set : intersection, union and difference; or getting the member with highest : ranking in a sorted set. : : In order to achieve its outstanding performance, Redis works with an : in-memory dataset. Depending on your use case, you can persist it either : by dumping the dataset to disk every once in a while, or by appending : each command to a log. : : Redis also supports trivial-to-setup master-slave replication, with very : fast non-blocking first synchronization, auto-reconnection on net split : and so forth. : : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a : limited time-to-live, and configuration settings to make Redis behave like : a cache. : : You can use Redis from most programming languages also. [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# yum -y install redis Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package redis.x86_64 0:3.2.12-2.el7 will be installed --> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.el7.x86_64 --> Running transaction check ---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================== Installing: redis x86_64 3.2.12-2.el7 epel 544 k Installing for dependencies: jemalloc x86_64 3.6.0-1.el7 epel 105 k Transaction Summary ============================================================================================================================================== Install 1 Package (+1 Dependent package) Total download size: 648 k Installed size: 1.7 M Downloading packages: warning: /var/cache/yum/x86_64/7/epel/packages/jemalloc-3.6.0-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY Public key for jemalloc-3.6.0-1.el7.x86_64.rpm is not installed (1/2): jemalloc-3.6.0-1.el7.x86_64.rpm | 105 kB 00:00:00 (2/2): redis-3.2.12-2.el7.x86_64.rpm | 544 kB 00:00:00 ---------------------------------------------------------------------------------------------------------------------------------------------- Total 3.4 MB/s | 648 kB 00:00:00 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 Importing GPG key 0x352C64E5: Userid : "Fedora EPEL (7) <epel@fedoraproject.org>" Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5 Package : epel-release-7-11.noarch (@extras) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : jemalloc-3.6.0-1.el7.x86_64 1/2 Installing : redis-3.2.12-2.el7.x86_64 2/2 Verifying : redis-3.2.12-2.el7.x86_64 1/2 Verifying : jemalloc-3.6.0-1.el7.x86_64 2/2 Installed: redis.x86_64 0:3.2.12-2.el7 Dependency Installed: jemalloc.x86_64 0:3.6.0-1.el7 Complete! [root@node101.yinzhengjie.org.cn ~]#
3>.修改时区
[root@node102.yinzhengjie.org.cn ~]# date -R Thu, 04 Apr 2019 22:46:20 -0700 [root@node102.yinzhengjie.org.cn ~]# rm -f /etc/localtime [root@node102.yinzhengjie.org.cn ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@node102.yinzhengjie.org.cn ~]# date -R Fri, 05 Apr 2019 13:54:09 +0800 [root@node102.yinzhengjie.org.cn ~]#
二.基于修改配置文件的方式实现Redis的主从复制
1>.配置主机说明
master 节点:node101.yinzhengjie.org.cn
slave 节点:node102.yinzhengjie.org.cn
2>.查看master节点的配置文件
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf bind node101.yinzhengjie.org.cn #指定当前的主机 protected-mode yes port 6379 #默认的Redis端口号 tcp-backlog 511 unixsocket /tmp/redis.sock timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.log 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-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass yinzhengjie 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 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 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 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
3>.查看slave节点的配置文件
[root@node102.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf bind node102.yinzhengjie.org.cn #绑定地址 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.log 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 slaveof node101.yinzhengjie.org.cn 6379 #指定master地址 masterauth yinzhengjie #指定连接master是需要认证的字符串! 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 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 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 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 [root@node102.yinzhengjie.org.cn ~]#
4>.启动master节点和slave节点
[root@node101.yinzhengjie.org.cn ~]# systemctl start redis [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# systemctl status redis ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2019-04-06 13:18:01 CST; 1h 13min ago Main PID: 9451 (redis-server) CGroup: /system.slice/redis.service └─9451 /usr/bin/redis-server node101.yinzhengjie.org.cn:6379 Apr 06 13:18:01 node101.yinzhengjie.org.cn systemd[1]: Starting Redis persistent key-value database... Apr 06 13:18:01 node101.yinzhengjie.org.cn systemd[1]: Started Redis persistent key-value database. [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl start redis [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# systemctl status redis ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2019-04-06 14:27:01 CST; 4min 43s ago Main PID: 9552 (redis-server) CGroup: /system.slice/redis.service └─9552 /usr/bin/redis-server 127.0.0.1:6379 Apr 06 14:27:01 node102.yinzhengjie.org.cn systemd[1]: Starting Redis persistent key-value database... Apr 06 14:27:01 node102.yinzhengjie.org.cn systemd[1]: Started Redis persistent key-value database. [root@node102.yinzhengjie.org.cn ~]#
5>.验证是否复制实现Redis主从同步
[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> INFO # Server #服务器端的相应信息 redis_version:3.2.12 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:7897e7d0e13773f redis_mode:standalone os:Linux 3.10.0-957.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll gcc_version:4.8.5 process_id:9451 run_id:514f95dd0a54449ec221b662da02caa65f74353e tcp_port:6379 uptime_in_seconds:5029 uptime_in_days:0 hz:10 lru_clock:11029038 executable:/usr/bin/redis-server config_file:/etc/redis.conf # Clients #客户单的相应详细 connected_clients:1 #表示当前连接进来的客户端个数 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory #当前服务器的内存分配和使用状况 used_memory:1883752 used_memory_human:1.80M used_memory_rss:2912256 used_memory_rss_human:2.78M used_memory_peak:1883752 used_memory_peak_human:1.80M total_system_memory:3954184192 total_system_memory_human:3.68G used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:1.55 mem_allocator:jemalloc-3.6.0 # Persistence · #当前启动的持久化是哪一种,以及是如何进行持久化的 loading:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1554532778 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 aof_enabled:0 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok # Stats #统计数据 total_connections_received:8 total_commands_processed:899 instantaneous_ops_per_sec:1 total_net_input_bytes:32127 total_net_output_bytes:44978 instantaneous_input_kbps:0.04 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:4 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:128 migrate_cached_sockets:0 # Replication #记录主从复制中当前的信息 role:master #当前节点的角色为master connected_slaves:1 #从节点连接进来的个数 slave0:ip=172.30.1.102,port=6379,state=online,offset=1233,lag=0 #记录第一个从节点的相应信息 master_repl_offset:1233 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:1232 # CPU #查看CPU相关信息 used_cpu_sys:3.71 used_cpu_user:1.10 used_cpu_sys_children:0.00 used_cpu_user_children:0.00 # Cluster #查看Cluster信息,如果没有穷Cluster功能的话,那么其值默认为0. cluster_enabled:0 # Keyspace #当前服务器键使用的空间 db0:keys=9,expires=0,avg_ttl=0 #表示在第0个数据库这种,使用了9个KEYS。 node101.yinzhengjie.org.cn:6379>
[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> KEYS * 1) "age" 2) "count" 3) "colors2" 4) "weekdays" 5) "color" 6) "name" 7) "liangshan" 8) "stu1" 9) "colors1" node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> exit [root@node101.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn node102.yinzhengjie.org.cn:6379> node102.yinzhengjie.org.cn:6379> KEYS * 1) "weekdays" 2) "count" 3) "colors2" 4) "colors1" 5) "age" 6) "liangshan" 7) "color" 8) "stu1" 9) "name" node102.yinzhengjie.org.cn:6379> node102.yinzhengjie.org.cn:6379> exit [root@node102.yinzhengjie.org.cn ~]#
node101.yinzhengjie.org.cn:6379> INFO Replication # Replication role:master connected_slaves:2 slave0:ip=172.30.1.102,port=6379,state=online,offset=3445,lag=1 slave1:ip=172.30.1.103,port=6379,state=online,offset=3445,lag=1 master_repl_offset:3459 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:3458 node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
三.基于CONFIG命令的方式实现Redis的主从复制
1>.配置主机说明
master 节点:node101.yinzhengjie.org.cn
slave 节点:node103.yinzhengjie.org.cn
2>.Server相关的命令
node101.yinzhengjie.org.cn:6379> HELP CLIENT GETNAME CLIENT GETNAME - summary: Get the current connection name since: 2.6.9 group: server node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CLIENT KILL CLIENT KILL [ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [ADDR ip:port] [SKIPME yes/no] summary: Kill the connection of a client since: 2.4.0 group: server node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CLIENT LIST CLIENT LIST - summary: Get the list of client connections since: 2.4.0 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CLIENT REPLY CLIENT REPLY ON|OFF|SKIP summary: Instruct the server whether to reply to commands since: 3.2 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CLIENT SETNAME CLIENT SETNAME connection-name summary: Set the current connection name since: 2.6.9 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP SHUTDOWN SHUTDOWN [NOSAVE|SAVE] summary: Synchronously save the dataset to disk and then shut down the server since: 1.0.0 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> CLIENT LIST id=8 addr=172.30.1.102:39874 fd=6 name= age=929 idle=1 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=replconf id=11 addr=172.30.1.101:42052 fd=7 name= age=37 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> INFO stats # Stats total_connections_received:32 total_commands_processed:3308 instantaneous_ops_per_sec:1 total_net_input_bytes:122495 total_net_output_bytes:96507 instantaneous_input_kbps:0.07 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:5 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:144 migrate_cached_sockets:0 node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> CONFIG RESETSTAT OK node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> INFO stats # Stats total_connections_received:0 total_commands_processed:4 instantaneous_ops_per_sec:2 total_net_input_bytes:136 total_net_output_bytes:5 instantaneous_input_kbps:0.07 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 node101.yinzhengjie.org.cn:6379>
[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> CONFIG SET requirepass yinzhengjie #配置后,马上就生效啦,因此下面执行的所有相关命令均无效!需要验证时方能使用 OK node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> CONFIG REWRITE #由于上面设置了认证的字符串,当前终端目前还没有进行认证,因此不可执行其他操作 (error) NOAUTH Authentication required. node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> AUTH yinzhengjie #在命令行中进行认证,认证成功后,为我们就可以执行相应的操作啦~ OK node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> CONFIG REWRITE OK node103.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379> HELP CONFIG GET CONFIG GET parameter summary: Get the value of a configuration parameter since: 2.0.0 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CONFIG RESETSTAT CONFIG RESETSTAT - summary: Reset the stats returned by INFO since: 2.0.0 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CONFIG REWRITE CONFIG REWRITE - summary: Rewrite the configuration file with the in memory configuration since: 2.8.0 group: server node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> HELP CONFIG SET CONFIG SET parameter value summary: Set a configuration parameter to the given value since: 2.0.0 group: server node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
3>.使用CONFIG命令配置Redis主从同步(node103.yinzhengjie.org.cn)
[root@node103.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf bind node103.yinzhengjie.org.cn protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.log 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-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 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 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 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 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# systemctl start redis [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]# systemctl status redis ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2019-04-06 00:06:16 PDT; 2s ago Main PID: 9567 (redis-server) CGroup: /system.slice/redis.service └─9567 /usr/bin/redis-server node103.yinzhengjie.org.cn:6379 Apr 06 00:06:16 node103.yinzhengjie.org.cn systemd[1]: Starting Redis persistent key-value database... Apr 06 00:06:16 node103.yinzhengjie.org.cn systemd[1]: Started Redis persistent key-value database. [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> KEYS * #启动集群后,我们还没有网当前节点创建测试数据,因此我们查看KEYS时发现是空的。 (empty list or set) node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> SLAVEOF node101.yinzhengjie.org.cn 6379 #指定master的地址 OK node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> CONFIG SET masterauth yinzhengjie #指定master的验证信息 OK node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> KEYS * #再次查看KEYS,发现是有数据的,原因是我们同步数据成功啦! 1) "age" 2) "colors1" 3) "stu1" 4) "name" 5) "color" 6) "colors2" 7) "count" 8) "liangshan" 9) "weekdays" node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> CONFIG REWRITE #上面的配置虽然说实现了主从复制,但是确实是临时生效的,因为重启后,配置就丢失了,想要永久保存配置,那么我们可以使用该命令将配置写入到Redis的配置文件中。 OK node103.yinzhengjie.org.cn:6379> node103.yinzhengjie.org.cn:6379> exit [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]# tail -3 /etc/redis.conf #我们查看Redis的最后三行,发现我们成功的将数据写入啦! # Generated by CONFIG REWRITE slaveof node101.yinzhengjie.org.cn 6379 masterauth "yinzhengjie" [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
4>.在master节点上查看相应的信息
node101.yinzhengjie.org.cn:6379> INFO Replication # Replication role:master connected_slaves:2 slave0:ip=172.30.1.102,port=6379,state=online,offset=3963,lag=1 slave1:ip=172.30.1.103,port=6379,state=online,offset=3963,lag=1 master_repl_offset:3963 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:3962 node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
node101.yinzhengjie.org.cn:6379> INFO stats # Stats total_connections_received:32 total_commands_processed:3308 instantaneous_ops_per_sec:1 total_net_input_bytes:122495 total_net_output_bytes:96507 instantaneous_input_kbps:0.07 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:5 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:144 migrate_cached_sockets:0 node101.yinzhengjie.org.cn:6379> node101.yinzhengjie.org.cn:6379>
四.主从复制相关参数说明
1>.slaveof
通过执行 SLAVEOF host port 命令,可以将当前服务器转变为指定服务器的从属服务器(slave server)。
2>.masterauth
用于指定slaveof主机连接时的认证密码。
3>.slave-serve-stale-data yes
当一个slave失去和master的连接,或者同步正在进行中,slave的行为可以有两种表现:
1>.如果 slave-serve-stale-data 设置为 "yes" (默认值),slave会继续响应客户端请求,可能是正常数据,或者是过时了的数据,也可能是还没获得值的空数据。
2>. 如果 slave-serve-stale-data 设置为 "no",slave会回复"正在从master同步(SYNC with master in progress)"来处理各种请求,除了 INFO 和 SLAVEOF 命令。
4>.slave-read-only yes
你可以配置salve实例是否接受写操作。可写的slave实例可能对存储临时数据比较有用(因为写入salve的数据在同master同步之后将很容易被删除)。
5>.repl-diskless-sync no
其有三个值,分别为:no,Disk-backed,Diskless
新的从节点或某较长时间未能与主节点进行同步的从节点重新与主节点通信,需要做“full synchronization”,此时其同步方式有两种style: Disk-backend:
主节点创建快照文件与磁盘中,而后将其发送给从节点。 Diskless:
主节点占新创建快照后直接通过网络套接字文件发送给从节点,为了实现并行复制,通常需要在复制启动前延迟一个时间段。
6>.repl-diskless-sync-delay 5
无磁盘diskless方式在进行数据传递之前会有一个时间的延迟,以便slave端能够进行到待传送的目标队列中,这个时间默认是5秒。
7>.repl-ping-slave-period 10
主从关系建立后,redis主会以repl-ping-slave-period
为周期,向redis从发送PING
指令。
8>.repl-timeout 60
slave会每隔repl-ping-slave-period(默认10秒)ping一次master,如果超过repl-timeout(默认 60秒)都没有收到响应,就会认为Master挂了。
9>.repl-diskable-tcp-nodelay no
如果选择“ yes” ,Redis将使用一个较小的数字 TCP数据包和更少的带宽将数据发送到 slave,但是这可能导致数据发送到 slave端会有延迟 ,如果是 Linux kernel的默认配置,会达到 40毫秒 ,如果选择 "no",则发送数据到 slave端的延迟会降低,但将使用更多的带宽用于复制 。
10>.repl-backlog-size lmb
设置backlog的大小,backlog是一个缓冲区,在slave端失连时存放要同步到slave的数据,因此当一个slave要重连时,经常是不需要完全同步的,执行局部同步就足够了。backlog设置的越大,slave可以失连的时间就越长。
11>.slave-priority 100
复制集群中,主节点故障时,sentinel应用场景中的主节点选举时使用优先级;数字越小优先级越高,但0表示不参与选举。
12>.min-slaves-to-write 3
主节点仅允许其能够通信的从节点数量大于等于此处的值时接收写操作。
13>.min-slaves-max-lag 10
从节点延迟时长超出此处指定的时长时,主节点会拒绝写入操作。