Redis—运维

关键词 Redis key-value 数据库 运维

一、安装

1、yum安装

1、安装EPEL

1.yum localinstall http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2、安装redis

1.yum install redis

2、包安装

官方链接:https://redis.io/download

1.Download, extract and compile Redis with:
2.$ wget http://download.redis.io/releases/redis-4.0.2.tar.gz
3.$ tar xzf redis-4.0.2.tar.gz
4.$ cd redis-4.0.2
5.$ make
6.The binaries that are now compiled are available in the src directory. Run Redis with:
7.$ src/redis-server
8.You can interact with Redis using the built-in client:
9.$ src/redis-cli
10.redis> set foo bar
11.OK
12.redis> get foo
13."bar"

二、基础操作

1、启动关闭重启

1.[root@centos-0 lcr]# systemctl start redis.service
2.[root@centos-0 lcr]# systemctl stop redis.service
3.[root@centos-0 lcr]# systemctl start redis.service
4.[root@centos-0 lcr]# systemctl restart redis.service

2、查看状态

1.systemctl status redis.service
1.[root@centos-0 lcr]# systemctl status redis.service
2.● redis.service - Redis persistent key-value database
3. Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
4. Drop-In: /etc/systemd/system/redis.service.d
5. └─limit.conf
6. Active: active (running) since 二 2017-09-26 21:33:11 CST; 19s ago
7. Process: 4734 ExecStop=/usr/libexec/redis-shutdown (code=exited, status=0/SUCCESS)
8. Main PID: 4754 (redis-server)
9. CGroup: /system.slice/redis.service
10. └─4754 /usr/bin/redis-server 127.0.0.1:6379
11.
12.926 21:33:11 centos-0 systemd[1]: Started Redis persistent key-value da...e.
13.926 21:33:11 centos-0 systemd[1]: Starting Redis persistent key-value d.....
14.Hint: Some lines were ellipsized, use -l to show in full.

3、开机启动

1.[root@centos-0 lcr]# systemctl enable redis.service
2.Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
1.[root@centos-0 lcr]# systemctl disable redis.service
2.Removed symlink /etc/systemd/system/multi-user.target.wants/redis.service.

三、基本命令

1、登陆&退出redis

1.[root@centos-0 lcr]# redis-cli
2.127.0.0.1:6379> quit
1.[root@centos-0 lcr]# redis-cli -h 127.0.0.1 -p 6379
2.127.0.0.1:6379>

2、set操作

1.set key value
1.127.0.0.1:6379> set key value [EX seconds] [PX milliseconds] [NX|XX]
1.127.0.0.1:6379> set k1 1234567890
2.OK

3、get操作

1.get key
1.127.0.0.1:6379> get k1
2."1234567890"

4、查找所有key

1.keys *
1.127.0.0.1:6379> keys *
2.1) "k1"
1.127.0.0.1:6379> key *
2.(error) ERR unknown command 'key'
3.127.0.0.1:6379> keys*
4.(error) ERR unknown command 'keys*'

5、判断key是否存在

1.exists key
1.127.0.0.1:6379> exists k1
2.(integer) 1
3.127.0.0.1:6379> exists 123
4.(integer) 0

6、删除指定key

1.del key
1.127.0.0.1:6379> del k1
2.(integer) 1

7、获取key的类型

1.type key
1.127.0.0.1:6379> set k2 lcr
2.OK
3.127.0.0.1:6379> type k2
4.string

8、查看当前redis的信息

1.info
1.127.0.0.1:6379> info
2.# Server
3.redis_version:3.2.10
4.redis_git_sha1:00000000
5.redis_git_dirty:0
6.redis_build_id:c8b45a0ec7dc67c6
7.redis_mode:standalone
8.os:Linux 3.10.0-514.26.2.el7.x86_64 x86_64
9.arch_bits:64
10.multiplexing_api:epoll
11.gcc_version:4.8.5
12.process_id:4754
13.run_id:235927ceee428801fa3175f6d7d7dbf5c6c08ed7
14.tcp_port:6379
15.uptime_in_seconds:2010
16.uptime_in_days:0
17.hz:10
18.lru_clock:13262577
19.executable:/usr/bin/redis-server
20.config_file:/etc/redis.conf
21.
22.# Clients
23.connected_clients:1
24.client_longest_output_list:0
25.client_biggest_input_buf:0
26.blocked_clients:0
27.
28.# Memory
29.used_memory:813680
30.used_memory_human:794.61K
31.used_memory_rss:6041600
32.used_memory_rss_human:5.76M
33.used_memory_peak:813680
34.used_memory_peak_human:794.61K
35.total_system_memory:3958308864
36.total_system_memory_human:3.69G
37.used_memory_lua:37888
38.used_memory_lua_human:37.00K
39.maxmemory:0
40.maxmemory_human:0B
41.maxmemory_policy:noeviction
42.mem_fragmentation_ratio:7.43
43.mem_allocator:jemalloc-3.6.0
44.
45.# Persistence
46.loading:0
47.rdb_changes_since_last_save:1
48.rdb_bgsave_in_progress:0
49.rdb_last_save_time:1506434597
50.rdb_last_bgsave_status:ok
51.rdb_last_bgsave_time_sec:0
52.rdb_current_bgsave_time_sec:-1
53.aof_enabled:0
54.aof_rewrite_in_progress:0
55.aof_rewrite_scheduled:0
56.aof_last_rewrite_time_sec:-1
57.aof_current_rewrite_time_sec:-1
58.aof_last_bgrewrite_status:ok
59.aof_last_write_status:ok
60.
61.# Stats
62.total_connections_received:2
63.total_commands_processed:12
64.instantaneous_ops_per_sec:0
65.total_net_input_bytes:339
66.total_net_output_bytes:19995
67.instantaneous_input_kbps:0.00
68.instantaneous_output_kbps:0.00
69.rejected_connections:0
70.sync_full:0
71.sync_partial_ok:0
72.sync_partial_err:0
73.expired_keys:0
74.evicted_keys:0
75.keyspace_hits:3
76.keyspace_misses:0
77.pubsub_channels:0
78.pubsub_patterns:0
79.latest_fork_usec:1061
80.migrate_cached_sockets:0
81.
82.# Replication
83.role:master
84.connected_slaves:0
85.master_repl_offset:0
86.repl_backlog_active:0
87.repl_backlog_size:1048576
88.repl_backlog_first_byte_offset:0
89.repl_backlog_histlen:0
90.
91.# CPU
92.used_cpu_sys:1.23
93.used_cpu_user:0.67
94.used_cpu_sys_children:0.01
95.used_cpu_user_children:0.00
96.
97.# Cluster
98.cluster_enabled:0
99.
100.# Keyspace
101.db0:keys=1,expires=0,avg_ttl=0

9、选择不同的db,redis中的db并没有很强的区分

1.select 1

10、清除所有的key

1.flushall

四、Redis配置

1、查看配置

1.CONFIG GET CONFIG_SETTING_NAME
1.127.0.0.1:6379> CONFIG GET loglevel
2.1) "loglevel"
3.2) "notice"
1.127.0.0.1:6379> CONFIG GET *
2. 1) "dbfilename"
3. 2) "dump.rdb"
4. 3) "requirepass"
5. 4) ""
6. 5) "masterauth"
7. 6) ""
8. 7) "unixsocket"
9. 8) ""
10. 9) "logfile"
11. 10) ""
12. 11) "pidfile"
13. 12) ""
14. 13) "slave-announce-ip"
15. 14) ""
16. 15) "maxmemory"
17. 16) "0"
18. 17) "maxmemory-samples"
19. 18) "5"
20. 19) "timeout"
21. 20) "0"
22. 21) "auto-aof-rewrite-percentage"
23. 22) "100"
24. 23) "auto-aof-rewrite-min-size"
25. 24) "67108864"
26. 25) "hash-max-ziplist-entries"
27. 26) "512"
28. 27) "hash-max-ziplist-value"
29. 28) "64"
30. 29) "list-max-ziplist-size"
31. 30) "-2"
32. 31) "list-compress-depth"
33. 32) "0"
34. 33) "set-max-intset-entries"
35. 34) "512"
36. 35) "zset-max-ziplist-entries"
37. 36) "128"
38. 37) "zset-max-ziplist-value"
39. 38) "64"
40. 39) "hll-sparse-max-bytes"
41. 40) "3000"
42. 41) "lua-time-limit"
43. 42) "5000"
44. 43) "slowlog-log-slower-than"
45. 44) "10000"
46. 45) "latency-monitor-threshold"
47. 46) "0"
48. 47) "slowlog-max-len"
49. 48) "128"
50. 49) "port"
51. 50) "6379"
52. 51) "tcp-backlog"
53. 52) "511"
54. 53) "databases"
55. 54) "16"
56. 55) "repl-ping-slave-period"
57. 56) "10"
58. 57) "repl-timeout"
59. 58) "60"
60. 59) "repl-backlog-size"
61. 60) "1048576"
62. 61) "repl-backlog-ttl"
63. 62) "3600"
64. 63) "maxclients"
65. 64) "10000"
66. 65) "watchdog-period"
67. 66) "0"
68. 67) "slave-priority"
69. 68) "100"
70. 69) "slave-announce-port"
71. 70) "0"
72. 71) "min-slaves-to-write"
73. 72) "0"
74. 73) "min-slaves-max-lag"
75. 74) "10"
76. 75) "hz"
77. 76) "10"
78. 77) "cluster-node-timeout"
79. 78) "15000"
80. 79) "cluster-migration-barrier"
81. 80) "1"
82. 81) "cluster-slave-validity-factor"
83. 82) "10"
84. 83) "repl-diskless-sync-delay"
85. 84) "5"
86. 85) "tcp-keepalive"
87. 86) "300"
88. 87) "cluster-require-full-coverage"
89. 88) "yes"
90. 89) "no-appendfsync-on-rewrite"
91. 90) "no"
92. 91) "slave-serve-stale-data"
93. 92) "yes"
94. 93) "slave-read-only"
95. 94) "yes"
96. 95) "stop-writes-on-bgsave-error"
97. 96) "yes"
98. 97) "daemonize"
99. 98) "no"
100. 99) "rdbcompression"
101.100) "yes"
102.101) "rdbchecksum"
103.102) "yes"
104.103) "activerehashing"
105.104) "yes"
106.105) "protected-mode"
107.106) "yes"
108.107) "repl-disable-tcp-nodelay"
109.108) "no"
110.109) "repl-diskless-sync"
111.110) "no"
112.111) "aof-rewrite-incremental-fsync"
113.112) "yes"
114.113) "aof-load-truncated"
115.114) "yes"
116.115) "maxmemory-policy"
117.116) "noeviction"
118.117) "loglevel"
119.118) "notice"
120.119) "supervised"
121.120) "no"
122.121) "appendfsync"
123.122) "everysec"
124.123) "syslog-facility"
125.124) "local0"
126.125) "appendonly"
127.126) "no"
128.127) "dir"
129.128) "/root"
130.129) "save"
131.130) "3600 1 300 100 60 10000"
132.131) "client-output-buffer-limit"
133.132) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
134.133) "unixsocketperm"
135.134) "0"
136.135) "slaveof"
137.136) ""
138.137) "notify-keyspace-events"
139.138) ""
140.139) "bind"
141.140) ""
142.127.0.0.1:6379>

2、编辑配置

1.redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
1.127.0.0.1:6379> CONFIG SET loglevel "notice"
2.OK
3.127.0.0.1:6379> CONFIG GET loglevel
4.1) "loglevel"
5.2) "notice"
6.127.0.0.1:6379>

3、参数说明

redis.conf 配置项说明如下:

  1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
    daemonize no
  2. 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
    pidfile /var/run/redis.pid
  3. 指定Redis监听端口,默认端口为6379,作者在自己的一篇博文中解释了为什么选用6379作为默认端口,因为6379在手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字
    port 6379
  4. 绑定的主机地址
    bind 127.0.0.1
    5.当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
    timeout 300
  5. 指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
    loglevel verbose
  6. 日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给/dev/null
    logfile stdout
  7. 设置数据库的数量,默认数据库为0,可以使用SELECT 命令在连接上指定数据库id
    databases 16
  8. 指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
    save <seconds> <changes>
    Redis默认配置文件中提供了三个条件:
    save 900 1
    save 300 10
    save 60 10000
    分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。
  9. 指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
    rdbcompression yes
  10. 指定本地数据库文件名,默认值为dump.rdb
    dbfilename dump.rdb
  11. 指定本地数据库存放目录
    dir ./
  12. 设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
    slaveof
  13. 当master服务设置了密码保护时,slav服务连接master的密码
    masterauth
  14. 设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH 命令提供密码,默认关闭
    requirepass foobared
  15. 设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息
    maxclients 128
  16. 指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
    maxmemory
  17. 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
    appendonly no
  18. 指定更新日志文件名,默认为appendonly.aof
    appendfilename appendonly.aof
  19. 指定更新日志条件,共有3个可选值:
    no:表示等操作系统进行数据缓存同步到磁盘(快)
    always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
    everysec:表示每秒同步一次(折衷,默认值)
    appendfsync everysec
  20. 指定是否启用虚拟内存机制,默认值为no,简单的介绍一下,VM机制将数据分页存放,由Redis将访问量较少的页即冷数据swap到磁盘上,访问多的页面由磁盘自动换出到内存中(在后面的文章我会仔细分析Redis的VM机制)
    vm-enabled no
  21. 虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
    vm-swap-file /tmp/redis.swap
  22. 将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0
    vm-max-memory 0
  23. Redis swap文件分成了很多的page,一个对象可以保存在多个page上面,但一个page上不能被多个对象共享,vm-page-size是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page大小最好设置为32或者64bytes;如果存储很大大对象,则可以使用更大的page,如果不 确定,就使用默认值
    vm-page-size 32
  24. 设置swap文件中的page数量,由于页表(一种表示页面空闲或使用的bitmap)是在放在内存中的,,在磁盘上每8个pages将消耗1byte的内存。
    vm-pages 134217728
  25. 设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为4
    vm-max-threads 4
  26. 设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启
    glueoutputbuf yes
  27. 指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
    hash-max-zipmap-entries 64
    hash-max-zipmap-value 512
  28. 指定是否激活重置哈希,默认为开启(后面在介绍Redis的哈希算法时具体介绍)
    activerehashing yes
  29. 指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件
    include /path/to/local.conf

守护进程(Daemon Process),也就是通常说的 Daemon 进程(精灵进程),是 Linux 中的后台服务进程。它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。
守护进程是个特殊的孤儿进程,这种进程脱离终端,为什么要脱离终端呢?之所以脱离于终端是为了避免进程被任何终端所产生的信息所打断,其在执行过程中的信息也不在任何终端上显示。
由于在 linux 中,每一个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端就称为这些进程的控制终端,当控制终端被关闭时,相应的进程都会自动关闭。

五、开启远程连接服务

1、查看redis的进程状态

1.ps -ef | grep redis
1.[root@centos-0 lcr]# ps -ef | grep redis
2.redis 4754 1 0 21:33 ? 00:00:02 /usr/bin/redis-server 127.0.0.1:6379
3.root 5042 4197 0 21:44 pts/0 00:00:00 redis-cli -h 127.0.0.1 -p 6379
4.root 5596 5553 0 22:13 pts/1 00:00:00 grep --color=auto redis

/usr/bin/redis-server 127.0.0.1:6379显示只能本机访问

2、开启远程连接

1、编辑参数

1.vim /etc/redis.conf
1.################################## NETWORK #####################################
2.
3.# By default, if no "bind" configuration directive is specified, Redis listens
4.# for connections from all the network interfaces available on the server.
5.# It is possible to listen to just one or multiple selected interfaces using
6.# the "bind" configuration directive, followed by one or more IP addresses.
7.#
8.# Examples:
9.#
10.# bind 192.168.1.100 10.0.0.1
11.# bind 127.0.0.1 ::1
12.#
13.# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
14.# internet, binding to all the interfaces is dangerous and will expose the
15.# instance to everybody on the internet. So by default we uncomment the
16.# following bind directive, that will force Redis to listen only into
17.# the IPv4 lookback interface address (this means Redis will be able to
18.# accept connections only from clients running into the same computer it
19.# is running).
20.#
21.# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
22.# JUST COMMENT THE FOLLOWING LINE.
23.# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24.bind 127.0.0.1
25.
26.# Protected mode is a layer of security protection, in order to avoid that
27.# Redis instances left open on the internet are accessed and exploited.
28.#
29.# When protected mode is on and if:
30.#
31.# 1) The server is not binding explicitly to a set of addresses using the
32.# "bind" directive.
33.# 2) No password is configured.
34.#
35.# The server only accepts connections from clients connecting from the
36.# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
37.# sockets.
38.#
39.# By default protected mode is enabled. You should disable it only if
40.# you are sure you want clients from other hosts to connect to Redis
41.# even if no authentication is configured, nor a specific set of interfaces
42.# are explicitly listed using the "bind" directive.
43.protected-mode yes
44.
45.# Accept connections on the specified port, default is 6379 (IANA #815344).
46.# If port 0 is specified Redis will not listen on a TCP socket.
47.port 6379
48.
49.# TCP listen() backlog.
50.#
51.# In high requests-per-second environments you need an high backlog in order
52.# to avoid slow clients connections issues. Note that the Linux kernel
53.# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
54.# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
55.# in order to get the desired effect.
56.tcp-backlog 511
57.
58.# Unix socket.
59.#
60.# Specify the path for the Unix socket that will be used to listen for
61.# incoming connections. There is no default, so Redis will not listen
62.# on a unix socket when not specified.
63.#
64.# unixsocket /tmp/redis.sock
65.# unixsocketperm 700
66.
67.# Close the connection after a client is idle for N seconds (0 to disable)
68.timeout 0
69.
70.# TCP keepalive.
71.#
72.# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
73.# of communication. This is useful for two reasons:
74.#
75.# 1) Detect dead peers.
76.# 2) Take the connection alive from the point of view of network
77.# equipment in the middle.
78.#
79.# On Linux, the specified value (in seconds) is the period used to send ACKs.
80.# Note that to close the connection the double of the time is needed.
81.# On other kernels the period depends on the kernel configuration.
82.#
83.# A reasonable value for this option is 300 seconds, which is the new
84.# Redis default starting with Redis 3.2.1.
85.tcp-keepalive 300

如下修改

Alt text

2、重启redis

1.[root@centos-0 lcr]# systemctl restart redis.service

3、连接测试

在另外一台安装了redis的机上测试访问

1.redis-cli -h <ip> -p <port>
1.[root@centos-1 ~]# redis-cli -h 192.168.121.20 -p 6379
2.192.168.121.20:6379> keys *
3.1) "k2"
4.192.168.121.20:6379> get k2
5."lcr"

六、常用工具

1、redis desktop manageer

下载链接:https://redisdesktop.com/download

七、Redis的主从配置

1、环境:

matser:192.168.121.20
slave: 192.168.121.21
redis: Redis server v=3.2.10 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=c8b45a0ec7dc67c6

2、编辑配置

1、master和slave上

1.vim /etc/redis.conf
1.#bind 127.0.0.1
1.protected-mode no
1.daemonize yes
1.appendonly yes

2、slave上

添加master信息

1.slaveof 192.168.121.20 6379

Alt text

3、重启redis

master跟slave均重启

1.systemctl restart redis.service

问题修复
修改之后。redis无法启动

1.[root@centos-0 lcr]# systemctl status redis.service
2.● redis.service - Redis persistent key-value database
3. Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
4. Drop-In: /etc/systemd/system/redis.service.d
5. └─limit.conf
6. Active: failed (Result: exit-code) since 二 2017-09-26 23:23:17 CST; 37s ago
7. Process: 4344 ExecStop=/usr/libexec/redis-shutdown (code=exited, status=1/FAILURE)
8. Process: 4339 ExecStart=/usr/bin/redis-server /etc/redis.conf --daemonize no (code=exited, status=1/FAILURE)
9. Main PID: 4339 (code=exited, status=1/FAILURE)
10.
11.926 23:23:17 centos-0 systemd[1]: Started Redis persistent key-value database.
12.926 23:23:17 centos-0 systemd[1]: Starting Redis persistent key-value database...
13.926 23:23:17 centos-0 systemd[1]: redis.service: main process exited, code=exited, status=1/FAILURE
14.926 23:23:17 centos-0 redis-shutdown[4344]: Could not connect to Redis at 192.168.121.21:6379: No route to host
15.926 23:23:17 centos-0 systemd[1]: redis.service: control process exited, code=exited status=1
16.926 23:23:17 centos-0 systemd[1]: Unit redis.service entered failed state.
17.926 23:23:17 centos-0 systemd[1]: redis.service failed.
18.

4、查看连接状态

1.info replication
1.[root@centos-0 lcr]# redis-cli
2.127.0.0.1:6379> info replication
3.# Replication
4.role:master
5.connected_slaves:0
6.master_repl_offset:0
7.repl_backlog_active:0
8.repl_backlog_size:1048576
9.repl_backlog_first_byte_offset:0
10.repl_backlog_histlen:0

connected_slaves:0表明未有slave连接上


slave机尝试连接master

1.[root@centos-1 db]# redis-cli -h 192.168.121.20 -p 6379
2.Could not connect to Redis at 192.168.121.20:6379: No route to host
3.Could not connect to Redis at 192.168.121.20:6379: No route to host

连不上

master上设置防火墙

1.[root@centos-0 lcr]# iptables -I INPUT -s 192.168.121.0/24 -j ACCEPT
2.[root@centos-0 lcr]# iptables -I OUTPUT -s 192.168.121.0/24 -j ACCEPT

添加局域网访问允许


再次查看连接状况

1.127.0.0.1:6379> info replication
2.# Replication
3.role:master
4.connected_slaves:1
5.slave0:ip=192.168.121.21,port=6379,state=online,offset=197,lag=0
6.master_repl_offset:197
7.repl_backlog_active:1
8.repl_backlog_size:1048576
9.repl_backlog_first_byte_offset:2
10.repl_backlog_histlen:196

connected_slaves:1
slave0:ip=192.168.121.21,port=6379,state=online,offset=197,lag=0

表示从服务器已经连上

在从服务器上查看key

1.[root@centos-1 db]# redis-cli
2.127.0.0.1:6379> keys *
3.1) "k1"
4.2) "k2"

master上的数据已经同步过来