安装配置基础redis
安装redis
下载安装包:wget http://download.redis.io/releases/redis-5.0.0.tar.gz
解压安装包:
tar –zxvf redis-5.0.0.tar.gz
安装编译环境
yum install gcc-c++
编译(在解压的目录中执行):
make
安装(在解压的目录中执行):
make install
注意:尽量使用root用户,如果需要使用其他用户,使用sudo命令,如果出现没有sudo权限, 先使用root账户配置文件/etc/sudoers,添加用户权限
- 修改配置,让主机能够访问
- 修改配置
redis-6379.config
- 修改配置
bind 192.168.31.130
port 6379
daemonize yes
logfile "log-6379.log"
dir /redis/data
这里一般使用单独的配置文件,并放到单独的文件夹下
- 开启linux的端口
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
主从复制
模拟master和slave服务
我们这里直接使用配置文件来模拟多个服务,配置不同的端口
6381 master
6382 slave1
6383 slave2
- 修改master
vim redis-6381.conf
bind 192.168.31.130
port 6381
dir /redis/data
dbfilename "dump-6381.rdb"
appendfilename "appendonly-6381.aof"
- 复制两份,并修改端口
sed "s/6381/6382/g" redis-6381.conf > redis-6382.conf sed "s/6381/6383/g" redis-6381.conf > redis-6383.conf
启动并设置为主从
先启动master
redis-server /redis/conf/redis-6381.conf
方式一:启动slave,在客户端发送命令指定master
启动slave 1
redis-server /redis/conf/redis-6382.conf
在slave客户端指定master
slaveof 192.168.31.130 6381
方式二:启动slave时在启动命令后参数指定master
- 启动slave2并指定master
redis-server /redis/conf/redis-6383.conf --slaveof 192.168.31.130 6381
方式三:在配置文件中指定(通用)
slaveof masterip masterport
#指定master
slaveof 192.168.31.130 6381
哨兵
配置哨兵 sentinel.conf
- 拷贝并配置26401端口的配置 sentinel-26401.conf
port 26401
dir /redis/data
sentinel monitor mymaster 127.0.0.1 6401 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 20000
sentinel deny-scripts-reconfig yes
- 复制其他
sed "s/26401/26402/g" sentinel-26401.conf > sentinel-26402.conf
sed "s/26401/26403/g" sentinel-26401.conf > sentinel-26403.conf
配置redis
配置master redis-6401.conf
port 6401 dir /redis/data dbfilename "dump-6401.rdb"
配置slave1 redis-6402.conf
port 6402 dir /redis/data dbfilename "dump-6402.rdb" slaveof 127.0.0.1 6401
配置slave2
sed "s/6402/6403/g" redis-6402.conf > redis-6403.conf
启动服务
- 启动redis master和slave
- 启动哨兵
redis-sentinel /redis/conf/sentinel-26401.conf redis-sentinel /redis/conf/sentinel-26402.conf redis-sentinel /redis/conf/sentinel-26403.conf
集群3主3从
基础配置
master redis-6501.conf
bind 192.168.31.130
port 6501
dir /redis/data
dbfilename "dump-6501.rdb"
cluster-enabled yes
cluster-config-file "cluster-6501.conf"
cluster-node-timeout 5000
复制六份
sed "s/6501/6502/g" redis-6501.conf > redis-6502.conf
sed "s/6501/6503/g" redis-6501.conf > redis-6503.conf
sed "s/6501/6504/g" redis-6501.conf > redis-6504.conf
sed "s/6501/6505/g" redis-6501.conf > redis-6505.conf
sed "s/6501/6506/g" redis-6501.conf > redis-6506.conf
启动集群
redis-server /redis/conf/redis-6501.conf
redis-server /redis/conf/redis-6502.conf
redis-server /redis/conf/redis-6503.conf
redis-server /redis/conf/redis-6504.conf
redis-server /redis/conf/redis-6505.conf
redis-server /redis/conf/redis-6506.conf
redis-cli --cluster create 192.168.31.130:6501 192.168.31.130:6502 192.168.31.130:6503 192.168.31.130:6504 192.168.31.130:6505 192.168.31.130:6506 --cluster-replicas 1