# -- redis.conf #修改redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf #使用后台方式运行
sed -i 's/logfile ""/logfile "/opt/redis/logs/redis.log"/g' redis.conf #配置日志路径
# no AOF and RDB for master #配置数据同步策略,master节点注释关闭掉dump数据同步
sed -i 's/save 900 1/# save 900 1/g' redis.conf
sed -i 's/save 300 10/# save 300 10/g' redis.conf
sed -i 's/save 60 10000/# save 60 10000/g' redis.conf
sed -i 's/appendonly no/appendonly yes/g' redis.conf #打开aof同步
sed -i 's/dir \.\//dir "/opt/redis/data/"/g' redis.conf #配置数据文件存放路径
sed -i 's/slowlog-log-slower-than 10000/slowlog-log-slower-than 5000/g' redis.conf
sed -i 's/slowlog-max-len 128/slowlog-max-len 1000/g' redis.conf
# -- sentinel.conf #修改sentinel.conf文件
echo 'daemonize yes' >> sentinel.conf #后台方式运行sentinel
echo 'logfile /opt/redis/logs/sentinel.log' >> sentinel.conf
sed -i 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.135.40.118 6379 2/g' sentinel.conf #配置哨兵模式
4、修改slave机器配置文件,两台slave配置一样:
# -- redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf
sed -i 's/logfile ""/logfile "/opt/redis/logs/redis.log"/g' redis.conf
sed -i 's/# save ""/save ""/g' redis.conf
sed -i 's/appendonly no/appendonly yes/g' redis.conf
sed -i 's/dir \.\//dir "/opt/redis/data/"/g'redis.conf
# SLOWLOG GET or SLOWLOG GET number
sed -i 's/slowlog-log-slower-than 10000/slowlog-log-slower-than 5000/g' redis.conf
sed -i 's/slowlog-max-len 128/slowlog-max-len 1000/g' redis.conf
echo "slaveof 10.135.40.118 6379" >> redis.conf #指定master节点
# -- sentinel.conf
echo 'daemonize yes' >> sentinel.conf
echo 'logfile /opt/redis/logs/sentinel.log' >> sentinel.conf
sed -i 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.135.40.118 6379 2/g' sentinel.conf
5、配置文件修改完成之后启动redis服务,从master开始启动,所有节点的启动方式和命令都是一样的。
启动redis服务和sentinel服务:
[root@idxs21-108 redis]# /opt/redis/bin/redis-server /opt/redis/conf/redis.conf
[root@idxs21-108 redis]# /opt/redis/bin/redis-sentinel /opt/redis/conf/sentinel.conf
查看服务进程状态:
[root@idxs21-108 redis]# ps -ef | grep redis
root 10024 9807 0 17:14 pts/0 00:00:00 grep redis
root 68378 1 0 Jul28 ? 01:30:09 /opt/redis/bin/redis-sentinel *:26379
root 68379 1 0 Jul28 ? 00:35:16 /opt/redis/bin/redis-server *:6379
6、测试
1>.测试数据同步
主redis写入数据:
[root@idxs40-118 redis]# redis-cli -p 6379
127.0.0.1:6379> set name abc
OK
127.0.0.1:6379> get name
"abc"
127.0.0.1:6379>
从redis读取数据:
[root@idxs40-117 redis]# redis-cli -p 6379
127.0.0.1:6379> get name
"abc"
127.0.0.1:6379>
从redis读取数据:
[root@idxs40-119 redis]# redis-cli -p 6379
127.0.0.1:6379> get name
"abc"
127.0.0.1:6379>
2>.redis默认是读写分离的,只有master能写,slave只能读:
1 [root@idxs40-117 ~]# redis-cli -p 6379 2 127.0.0.1:6579> set name 123 3 (error) READONLY You can't write against a read only slave.
这里可看到,slave节点默认只有读的权限,无法进行写入操作。
7、sentinel一些命令介绍 要使用sentinel的命令,我们需要用redis-cli命令进入到sentinel: