Ubuntu 搭建redis主从复制加哨兵模式

a 夏天 提交于 2019-12-30 15:15:07

步骤:

1.下载redis安装包或是上传一个下载好的压缩包,这里用的是redis-5.0.7

wget http://download.redis.io/releases/redis-5.0.7.tar.gz

2.解压编译

tar -zxvf redis-5.0.7.tar.gz

3.编译安装

切换到redis的目录下

cd redis-5.0.7

4.安装make

sudo apt-get install ubuntu-make

sudo apt-get install make

5.安装gcc

sudo apt-get install gcc

6.编译安装

make

make install  prefix=/home/redis

7.创建目录

cd /home/redis

mkdir conf

mkdir log

mkdir data

mkdir temp

8.拷贝文件redis.conf和sentinel.conf

cp /usr/local/redis-5.0.7/redis.conf  /home/redis/conf/redis6379.conf

cp /usr/local/redis-5.0.7/redis.conf  /home/redis/conf/redis6380.conf

cp /usr/local/redis-5.0.7/redis.conf  /home/redis/conf/redis6381.conf

cp /usr/local/redis-5.0.7/sentinel.conf /home/redis/conf/sentinel26379.conf

cp /usr/local/redis-5.0.7/sentinel.conf /home/redis/conf/sentinel26380.conf

cp /usr/local/redis-5.0.7/sentinel.conf /home/redis/conf/sentinel26381.conf

9.配置redis

主 6379

protected-mode yes
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile /var/log/redis/redis_6379.log
dir /usr/local/redis-cluster/6379/data
slave-read-only yes

#设置访问登录密码 
requirepass 123456
#从服务设置了密码需要加上
masterauth 123456

从slave 6380

protected-mode yes
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
logfile /var/log/redis/redis_6380.log
dir /usr/local/redis-cluster/6380/data
slaveof 127.0.0.1 6379
#若主服务设置了密码需要加上 ,在设置哨兵时主从之间连接需要
masterauth 123456
#从服务密码设置
requirepass 123456

从slave 6381

protected-mode yes
port 6381
daemonize yes
pidfile /var/run/redis_6381.pid
logfile /var/log/redis/redis_6381.log
dir /usr/local/redis-cluster/6381/data
slaveof 127.0.0.1 6379
#若主服务设置了密码需要加上,在设置哨兵时主从之间连接需要
masterauth 123456
#从服务密码设置
requirepass 123456

master既可以读,也可以写,而 从服务器是只可以读,不可写的.

10.哨兵配置

主 26379

protected-mode no

port 26379

dir "/usr/local/redis/temp/6379"

sentinel monitor redis1 127.0.0.1 6379 2

#若主服务设置了密码需要加上注意顺序必须在sentinel monitor下面

sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds redis1 10000

sentinel failover-timeout redis1 60000

从 26380

protected-mode no

port 26380

dir "/usr/local/redis/temp/6380"

sentinel monitor redis1 127.0.0.1 6380 2

sentinel down-after-milliseconds redis1 10000

sentinel failover-timeout redis1 60000

从 26381

protected-mode no

port 26381

dir "/usr/local/redis/temp/6381"

sentinel monitor redis1 127.0.0.1 6381 2

sentinel down-after-milliseconds redis1 10000

sentinel failover-timeout redis1 60000

11.启动redis 和sentinel

./redis-server /home/redis/conf/redis6379.conf

./redis-server /home/redis/conf/redis6380.conf

./redis-server /home/redis/conf/redis6381.conf

./redis-sentinel /home/redis/conf/sentinel26379.conf

./redis-sentinel /home/redis/conf/sentinel26380.conf

./redis-sentinel /home/redis/conf/sentinel26381.conf

12.查看状态

redis-cli -h 127.0.0.1 -p 26379 -a 123456 info Sentinel

 

 13.查看进程

ps aux | grep redis

 

 14.可以通过redis客户端进行测试

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!