mysql 哨兵模式_redis配置主从+sentine哨兵模式
环境:
OS:Red Hat Linux As 5
DB:redis-4.0.6
-------------------------------------主库安装-------------------------------
1.安装
[root@pxc01 soft]#tar –zxvf redis-4.0.6.tar.gz
[root@pxc01 soft]# cp -R ./redis-4.0.6 /opt/
[root@pxc01 opt]# cd redis-4.0.6
[root@pxc01 redis-4.0.6]# make
[root@pxc01 redis-4.0.6]# make test
继续安装
[root@pxc01 redis-4.0.6]# cd src
[root@pxc01 src]# make install
2.创建相应目录
[root@pxc01 opt]# cd redis-4.0.6/
[root@pxc01 redis-4.0.6]# mkdir conf ##配置文件目录
[root@pxc01 redis-4.0.6]# mkdir log ##日志文件目录
[root@pxc01 redis-4.0.6]# mkdir run ##运行的pid文件目录
[root@pxc01 redis-4.0.6]# mkdir rdb ##快照和AOF文件目录
3.主库配置文件(8379.conf)
daemonize yes
pidfile/opt/single_redis-4.0.6/run/redis-8379.pid
port8379
tcp-backlog 511
timeout0
tcp-keepalive 0
loglevel notice
logfile"/opt/single_redis-4.0.6/log/redis-8379.log"
databases 16
save900 1
save300 10
save60 10000
stop-writes-on-bgsave-erroryes
rdbcompression yes
rdbchecksum yes
dbfilenamedump-8379.rdb
dir/opt/single_redis-4.0.6/rdb
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-syncno
repl-diskless-sync-delay5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename"redis-8379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size64mb
aof-load-truncated yes
lua-time-limit5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries512
hash-max-ziplist-value64
list-max-ziplist-entries512
list-max-ziplist-value64
set-max-intset-entries512
zset-max-ziplist-entries128
zset-max-ziplist-value64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limitnormal 0 0 0
client-output-buffer-limitslave 256mb 64mb 60
client-output-buffer-limitpubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated byCONFIGREWRITE
#masterauth"richinfo123"
#requirepass"richinfo123"
protected-mode no
4.启动主库
/opt/single_redis-4.0.6/src/redis-server /opt/single_redis-4.0.6/conf/8379.conf
-------------------------------------从库安装-------------------------------
1.安装
跟主库安装一样
2.从库配置文件
从库的配置文件就是在主库配置文件的基础上加上如下2行
slaveof 192.168.56.91 8379 ##表示从那个主库同步
slave-read-only yes ##从库设置为只读
daemonize yes
pidfile/opt/single_redis_slave/run/redis-8379.pid
port8379
tcp-backlog 511
timeout0
tcp-keepalive 0
loglevel notice
logfile"/opt/single_redis_slave/log/redis-8379.log"
databases 16
save900 1
save300 10
save60 10000
stop-writes-on-bgsave-erroryes
rdbcompression yes
rdbchecksum yes
dbfilenamedump-8379.rdb
dir/opt/single_redis_slave/rdb
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-syncno
repl-diskless-sync-delay5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename"redis-8379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size64mb
aof-load-truncated yes
lua-time-limit5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries512
hash-max-ziplist-value64
list-max-ziplist-entries512
list-max-ziplist-value64
set-max-intset-entries512
zset-max-ziplist-entries128
zset-max-ziplist-value64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limitnormal 0 0 0
client-output-buffer-limitslave 256mb 64mb 60
client-output-buffer-limitpubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated byCONFIGREWRITE
#masterauth"richinfo123"
#requirepass"richinfo123"
protected-mode no
slaveof 192.168.56.91 8379
3.启动从库
/opt/single_redis_slave/src/redis-server /opt/single_redis_slave/conf/8379.conf
4.查看从库的同步日志
21636:S 23 Jan 04:40:33.028 * Ready to accept connections
21636:S 23 Jan 04:40:33.028 * Connecting to MASTER 192.168.56.91:8379
21636:S 23 Jan 04:40:33.028 * MASTER SLAVE sync started
21636:S 23 Jan 04:40:33.030 * Non blocking connect for SYNC fired the event.
21636:S 23 Jan 04:40:33.031 * Master replied to PING, replication can continue...
21636:S 23 Jan 04:40:33.032 * Partial resynchronization not possible (no cached master)
21636:S 23 Jan 04:40:33.038 * Full resync from master: 4bfec2e8871336c8c86bb37a8b16905d57d5278b:0
21636:S 23 Jan 04:40:33.743 * MASTER SLAVE sync: receiving 4469862 bytes from master
21636:S 23 Jan 04:40:33.772 * MASTER SLAVE sync: Flushing old data
21636:S 23 Jan 04:40:33.785 * MASTER SLAVE sync: Loading DB in memory
21636:S 23 Jan 04:40:34.032 * MASTER SLAVE sync: Finished with success
21636:S 23 Jan 04:40:34.033 * Background append only file rewriting started by pid 21640
21636:S 23 Jan 04:40:34.387 * AOF rewrite child asks to stop sending diffs.
21640:C 23 Jan 04:40:34.387 * Parent agreed to stop sending diffs. Finalizing AOF...
21640:C 23 Jan 04:40:34.387 * Concatenating 0.00 MB of AOF diff received from parent.
21640:C 23 Jan 04:40:34.388 * SYNC append only file rewrite performed
21640:C 23 Jan 04:40:34.389 * AOF rewrite: 10 MB of memory used by copy-on-write
21636:S 23 Jan 04:40:34.439 * Background AOF rewrite terminated with success
21636:S 23 Jan 04:40:34.439 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
21636:S 23 Jan 04:40:34.439 * Background AOF rewrite finished successfully
从输出日志看出,从库启动后,会跟主库进行数据同步,把主库的数据同步过来,主库运行一段时间后,从库也会把主库的历史数据同步过来。
4.验证
主库的数据大小
[root@pxc01 src]# ./redis-cli -h 192.168.56.91 -p 8379
192.168.56.91:8379> dbsize
(integer) 300000
从库的数据大小
[root@pxc02 src]# ./redis-cli -h 192.168.56.92 -p 8379
192.168.56.92:8379> dbsize
(integer) 300000
5.故障切换
当主库当掉之后,可以执行如下命令将从库升级为主库
192.168.56.92:8379> slaveof no one
OK
等主库好了之后可以执行如下命令将当期的主库设置为从库
192.168.56.92:8379> slaveof 192.168.56.91 8379
OK
主库宕机到恢复完成的时间里,之前从库升级主库写入的数据,重新建立主从关系后,这些数据会被覆盖,因为从新建立了主从关系,从库重新同步一份之前主库的数据过来。
-----------------------------Sentine哨兵模式安装-----------------------
创建好主从之后,我们接下部署sentine哨兵模式监控主从的状态,哨兵模式的作用就是监控主从状态,发现主库宕机后,从备库中挑选一个出来当选主库,对外部应用继续提高访问服务。
1.安装
安装步骤跟主从安装redis一样,这里省略
2.配置文件
sentinel.conf
port26379
dir"/tmp"
sentinel myid 3154c22b52f5fdca833d7a972bb0104e11e63b82
sentinelmonitormymaster 192.168.56.91 8379 1
sentinelconfig-epoch mymaster 1
sentinel leader-epoch mymaster 1
sentinel down-after-millisecondsmymaster15000
logfile"/opt/redis_sentinel/log/sentinel.log"
protected-mode no
关键的参数:
port 端口号,哨兵程序运行的端口号
sentinel monitor mymaster 192.168.56.91 8379 1
mymaster:哨兵监控redis组叫mymaster,这个名字可以任意命名,符合命名要求即可
192.168.56.91 8379:这个是哨兵监控的主库的ip和端口号
1:表示仲裁值,这里表示有1个哨兵发现主库无法访问的时候,就可以从从库中选举新的主库,在多个哨兵的环境下,这个值可以根据需要设置,比如2,3等,意味着至少需要有2、3个哨兵都发现主库无法访问的情况下才重新选举主库。
sentinel down-after-millisecondsmymaster15000
----------这个是sentinel连接master的超时时间,超过这个时间就认为master挂了,实现自动切换。这个默认是30秒,这个时间得调节好,大了会在真正出现故障的时候切换时间会长,小了有时候master由于持久化数据,繁忙不响应,会导致自动切换,实际只是瞬间不可用,现在认为设置为15秒为宜.
3.启动
/opt/redis/src/redis-sentinel /opt/redis/conf/sentinel.conf
4.登陆哨兵查看主从情况
./redis-cli -h 192.168.56.93 -p 26379
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.56.91:8379,slaves=1,sentinels=1
5.外部应用同步哨兵访问集群
通常情况下,我们通过JedisSentinelPool创建跟集群的连接,这样的话,主从关系改变了,我们的应用程序也不用修改连接,但是主从改变的过程中,外部应用不能访问.
packagecom.hxl;
import java.util.HashSet;
import java.util.Set;
importredis.clients.jedis.Jedis;
importredis.clients.jedis.JedisSentinelPool;
public classSentineTest{
public static voidmain(String[]args) {
Setsentinels= new HashSet();
StringhostAndPort1= "192.168.56.93:26379";
sentinels.add(hostAndPort1);
StringclusterName= "mymaster" ;
JedisSentinelPool redisSentinelJedisPool= newJedisSentinelPool(clusterName,sentinels);
Jedis jedis= null;
try {
jedis=redisSentinelJedisPool.getResource();
jedis.set("test4", "aaa");
System.out.println(jedis.get("test4"));
//System.out.println(jedis.get("test2"));
} catch (Exceptione) {
e.printStackTrace();
} finally {
jedis.close();
}
redisSentinelJedisPool.close();
}
}
String hostAndPort1 = "192.168.56.93:26379" ##这里是指定哨兵所在的机器ip和占用的端口;
String clusterName = "mymaster" ; ##这里要跟服务器设置的一致.
通常情况下,我们会部署多个哨兵,避免一个哨兵宕机了导致外部应用无法访问,部署多个哨兵的部署跟部署一个哨兵的步骤一致,只是配置文件中仲裁值根据需要设定。
--The End --
mysql 哨兵模式_redis配置主从+sentine哨兵模式相关推荐
- mysql 哨兵模式_Redis讲解主从复制和哨兵模式
推荐(免费):redis 文章目录主从复制 命令 配置 复制原理 全量复制 增量复制 测试 嵌套主从 哨兵模式 配置哨兵 测试 主从复制 主从复制指的是把一台Redis服务器的数据复制到其他Redis ...
- Mysql服务器线上配置主从同步
我们一般在线上搭建MYSQL都会部署一套主从同步方案: 当master(主)库的数据发生变化的时候,变化会实时的同步到slave(从)库. 主从复制的过程: Mysql同步过程的第一部分就是maste ...
- ntp协议客户端服务器模式,思科配置NTP客户端服务器模式
思科配置NTP客户端服务器模式 内容精选 换一换 该操作指导安装工程师安装MRS集群所有服务(不包含Flume)的客户端.MRS针对不同服务提供了Shell脚本,供开发维护人员在不同场景下登录其对应的 ...
- 一文读懂Redis的四种模式,单机、主从、哨兵、集群(*)
前言: redis有多种模式:单机模式.主从模式.哨兵模式.集群模式 1.单机模式 安装一个redis,启动起来,业务调用即可. 单机在很多场景也是有使用的,例如在一个并非必须保证高可用的情况下. 优 ...
- atlas mysql 安装_Atlas安装配置
准备环境 192.168.1.1(Altas) 192.168.1.2(MySQL主) 192.168.1.3(MySQL从) 一.配置主从数据库访问连接 #依次设置主库,从库允许Atlas访问连接 ...
- Mysql主从和redis集群哪个好_Redis的三种模式:主从、哨兵、集群
前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.普遍用于目前主流的分布式架构系统中,关于redis的详细 ...
- jedispool redis哨兵_通过java哨兵JedisSentinelPool代码示例连接对配置的redis哨兵主从模式进行测试验证...
一.前言 本文章通过关于java的jedis(2.6.0)的redis客户端连接驱动包,对配置的redis哨兵+主从读写模式配置进行示例代码验证,详细参见具体配置步骤&示例代码说明部分. 二. ...
- StackExchange.Redis客户端读写主从配置,以及哨兵配置
今天简单分享一下StackExchange.Redis客户端中配置主从分离以及哨兵的配置. 关于哨兵如果有不了解的朋友,可以看我之前的一篇分享,当然主从复制文章也可以找到.http://www.cnb ...
- redis的三大模式主从,哨兵和集群
一.前言 二.redis主从复制 1.主从复制的作用: 2.主从复制的流程 3.搭建主从复制 3.1.搭建环境 3.2.安装redis 3.3.主服务器配置查看以下行 3.4.从服务器配置查看以下行 ...
最新文章
- 【mybatis】xml中定义变量
- 从头学习linux C 冒泡法排序
- CryEngine新引擎正式开放源代码,但仅限开发游戏
- HTML基础整理(From表单)
- 记录用NoteExpress下载文献全文成功
- 微信公众号支付java_微信公众号支付开发全过程(java版)
- mysql 备份数据库
- 强迫症狂喜!PC端相册还能智能分类!
- 从数据库中读取经纬度,在google map 上进行标注(一)
- 初探DirectShow
- 高德地图js点击标记放大地图的操作
- “status“: 400, “error“: “Bad Request“,
- 大学生在线问答系统(UML建模)包括需求,分析,设计,实现
- Ratingbar的功能和用法
- 主流的知乎口碑营销方式有哪些
- MySQL数据表的结构和设计及三范式
- 基于遗传算法的供水泵站节能降耗优化
- Object Pascal中String类型的内幕探讨 (转)
- Even Array
- 唯美有创意的思维导图模板分享以及绘制该思维导图方法介绍