redis配置

一.主从复制配置

1.配置环境
server1:主(172.25.14.1)
server2:从(172.25.14.2)
1.sever1和server2源码安装redis

[root@server1 ~]# tar zxf redis-4.0.8.tar.gz 
[root@server1 ~]# cd redis-4.0.8
[root@server1 redis-4.0.8]# yum install gcc -y
[root@server1 redis-4.0.8]# make
[root@server1 redis-4.0.8]# make install
[root@server1 redis-4.0.8]# cd utils/
[root@server1 utils]# ./install_server.sh ##启动redis
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!

redis配置_第1张图片
redis配置_第2张图片
redis配置_第3张图片
2.master机配置

[root@server1 utils]# vim /etc/redis/6379.conf
 70 bind 0.0.0.0

这里写图片描述

[root@server1 utils]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@server1 utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@server1 utils]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      7296/redis-server 0 
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      891/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1505/master         
tcp        0      0 172.25.14.1:22              172.25.14.250:59458         ESTABLISHED 1555/sshd           
tcp        0      0 127.0.0.1:6379              127.0.0.1:43122             TIME_WAIT   -                   
tcp        0      0 :::22                       :::*                        LISTEN      891/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      1505/master         

3.配置slave端

[root@server2 utils]# vim /etc/redis/6379.conf
 70 bind 0.0.0.0
[root@server2 utils]# vim /etc/redis/6379.conf
 284 slaveof 172.25.14.1 6379
[root@server2 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

4、测试:
master:

[root@server1 utils]# redis-cli
127.0.0.1:6379> set name westos
OK
127.0.0.1:6379> get name
"westos"

slave:

[root@server2 utils]# redis-cli
127.0.0.1:6379> get name
"westos"

在master

127.0.0.1:6379> set name linux
OK
127.0.0.1:6379> get name
"linux"

在slave

127.0.0.1:6379> get name
"linux"

二、master-slave自动切换配置

配置环境:
server1:master
server2:slave
server3:slave
在上个实验的前提下将server3设置成slave,配置方法与server2一致。
在master机

[root@server1 ~]# cd redis-4.0.8
[root@server1 redis-4.0.8]# cp sentinel.conf /etc/redis/
[root@server1 redis-4.0.8]# cd /etc/redis/
[root@server1 redis]# vim sentinel.conf 
 15 bind 0.0.0.0
 16 #
 17 protected-mode no
 98 sentinel monitor mymaster 172.25.14.3 6379 2
 106 sentinel down-after-milliseconds mymaster 10000
 131 sentinel config-epoch mymaster 1

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

[root@server1 redis]# scp sentinel.conf server2:/etc/redis/
sentinel.conf                                                          100% 7590     7.4KB/s   00:00    
[root@server1 redis]# scp sentinel.conf server3:/etc/redis/
sentinel.conf                                                          100% 7590     7.4KB/s   00:00  

三台主机都开启成功后会有以下内容
在三台主机上操作

redis-server /etc/redis/sentinel.conf --sentinel

redis配置_第4张图片
测试:
将master down掉以后
会自动选举新的master出来(可能需要一两分钟):
另开一个shell
server1:

[root@server1 ~]# redis-cli
127.0.0.1:6379> shutdown
[root@server1 redis]# vim sentinel.conf ##查看文件可以看到新写入的内容
sentinel leader-epoch mymaster 1
sentinel known-slave mymaster 172.25.14.1 6379
sentinel known-slave mymaster 172.25.14.2 6379
sentinel known-sentinel mymaster 172.25.14.3 26379 a5e357ee96f1ae04e9ff4831720ef7c07ee09449
sentinel known-sentinel mymaster 172.25.14.2 26379 82bd07db8aa027ffcb2e076e5eb820e59094e9a5
sentinel current-epoch 1

查看server2和server3看谁是新的master

[root@server1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@server1 ~]# cd /etc/redis/
[root@server1 redis]# vim sentinel.conf
[root@server1 redis]# redis-cli -h 172.25.14.3
172.25.14.3:6379> get name
"linux"
172.25.14.3:6379> set name westos
OK
172.25.14.3:6379> get name
"westos"
172.25.14.3:6379> 
[root@server1 redis]# redis-cli
127.0.0.1:6379> get name
"westos"
127.0.0.1:6379> 

重新选出master后,配置文件会自动修改
这里写图片描述
此时修改down掉的redis的配置文件:6379.conf,重新指向新的master,并重启服务

你可能感兴趣的:(redis配置)