Linux 运维:Redis 服务

Redis 键值数据库 主从复制

一、安装redis

1、源码安装(2和3一致,以主机3为例)
[root@server3 ~]# cd redis/
[root@server3 redis]# tar zxf redis-4.0.1.tar.gz 
[root@server3 redis]# cd redis-4.0.1
[root@server3 redis-4.0.1]# yum install -y gcc
[root@server3 redis-4.0.1]# make && make install
2、启动脚本
[root@server3 redis-4.0.1]# cd utils/
[root@server3 utils]# ./install_server.sh   ##Enter即可,默认安装
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
3、master端(server2主机)配置
[root@server2 utils]# vim /etc/redis/6379.conf
  70 bind 0.0.0.0
[root@server2 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
4、slave端(server3主机)配置
[root@server3 utils]# vim /etc/redis/6379.conf
  70 bind 0.0.0.0
 282 # slaveof  
 283 slaveof 172.25.120.2 6379
[root@server3 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

二、测试 redis 主从复制

1、master端
[root@server2 utils]# redis-cli 
127.0.0.1:6379> set name test
OK
127.0.0.1:6379> set name hello
OK
127.0.0.1:6379> get name
"hello"
2、slave端
[root@server3 utils]# redis-cli 
127.0.0.1:6379> get name
"test"
127.0.0.1:6379> get name
"hello"
127.0.0.1:6379> monitor ##常链接
OK
1531360765.494202 [0 172.25.120.2:6379] "PING"

三、master-slave自动切换

1、配置文件(需要3台主机,1主2从)
  • 新增1台主机,配置成slave,同server3
  • 3台主机配置相同,以server4为例
[root@server4 ~]# cd redis/redis-4.0.1
[root@server4 redis-4.0.1]# vim sentinel.conf
 17 protected-mode no
 21 port 26379
 50 dir /tmp
 69 sentinel monitor mymaster 172.25.120.2 6379 2
 98 sentinel down-after-milliseconds mymaster 5000
 [root@server4 redis-4.0.1]# cp sentinel.conf /etc/redis/
 ##3台主机修改ok后执行以下操作
 [root@server2 ~]# redis-server /etc/redis/sentinel.conf --sentinel
2、查看redis的状态
  • master
[root@server4 ~]# redis-server /etc/redis/sentinel.conf --sentinel
738:X 12 Jul 10:17:27.535 # +monitor master mymaster 172.25.120.2 6379 quorum 2
4738:X 12 Jul 10:17:27.536 * +slave slave 172.25.120.3:6379 172.25.120.3 6379 @ mymaster 172.25.120.2 6379
4738:X 12 Jul 10:17:27.891 * +slave slave 172.25.120.4:6379 172.25.120.4 6379 @ mymaster 172.25.120.2 6379

Linux 运维:Redis 服务_第1张图片

3、测试
  • master down
[root@server2 ~]# redis-cli 
127.0.0.1:6379> shutdown
not connected> 
  • slave 自动选举4为 master
5377:X 10 Jul 17:25:44.092 # +switch-master mymaster 172.25.12.2 6379 172.25.12.4 6379
5377:X 10 Jul 17:25:44.093 * +slave slave 172.25.12.3:6379 172.25.12.3 6379 @ mymaster 172.25.12.4 6379
5377:X 10 Jul 17:25:44.093 * +slave slave 172.25.12.2:6379 172.25.12.2 6379 @ mymaster 172.25.12.4 6379
5377:X 10 Jul 17:25:49.148 # +sdown slave 172.25.12.2:6379 172.25.12.2 6379 @ mymaster 172.25.12.4 6379

##选举后,配置文件 /etc/redis/sentinel.conf 自动修改

Linux 运维:Redis 服务_第2张图片

四、redis的复制模式

设置:当matser切成slave时,必须有一台主机跟自身数据相同,才会清空数据

[root@server2 6379]# cd /proc/sys/vm/
[root@server2 vm]# cat overcommit_memory 
0
[root@server2 vm]# echo 1 > overcommit_memory
[root@server2 ~]# cat /proc/sys/vm/overcommit_memory 
1
1、默认是rdb模式(一个文件)
[root@server2 ~]# cd /var/lib/redis/6379/
[root@server2 6379]# ls
dump.rdb
2、修改配置文件,打开aof模式
  • AOF:mater所有的操作镜像
[root@server2 6379]# vim /etc/redis/6379.conf
 673 appendonly yes
 674 
 675 # The name of the append only file (default: "appendonly.aof")
 676 
 677 appendfilename "appendonly.aof"
 [root@server2 6379]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@server2 6379]# ls
appendonly.aof  dump.rdb
[root@server2 6379]# cat appendonly.aof 

Linux 运维:Redis 服务_第3张图片

你可能感兴趣的:(linux运维)